项目作者: xpepermint

项目描述 :
Internationalization (i18n) and localization (l10n) library for Vue.js v2.
高级语言: JavaScript
项目地址: git://github.com/xpepermint/vue-translated.git
创建时间: 2016-12-04T21:51:46Z
项目社区:https://github.com/xpepermint/vue-translated

开源协议:

下载


NPM Version Dependency Status

vue-translated

Internationalization (i18n) and localization (l10n) library for Vue.js v2.

This plugin integrates the translated.js library into your Vue.js application.

This is a light weight open source package for Vue.js. The source code is available on GitHub where you can also find our issue tracker.

  • translated.js: Internationalization and localization for JavaScript and Node.js

Installation

Run the command below to install the package.

  1. $ npm install --save vue-translated translated

When used with a module system, you must explicitly install vue-translated via Vue.use().

  1. import Vue from 'vue';
  2. import VueTranslated from 'vue-translated';
  3. Vue.use(VueTranslated);

Getting Started

Initialize translated.js and define a user model.

  1. import {I18n} from 'translated';
  2. const i18n = new I18n({
  3. locale: 'en-US',
  4. messages: {
  5. hello: 'Hello, {name}.'
  6. }
  7. });

Attach the i18n instance to your Vue.js application.

  1. const app = new Vue({
  2. i18n, // injecting i18n into child components
  3. ...
  4. });

By passing the i18n instance to the root Vue instance as the i18n option, the $i18n property is injected into every child component.

Usage Example

This plugin registers i18n methods so you can use them directly.

  1. <template>
  2. <div>
  3. <p>{{ $formatMessage('hello', {name: 'John'}) }}</p>
  4. <p>{{ $formatNumber(1000.13, {format: 'integer'}) }}</p>
  5. <p>{{ $formatDate(Date.now(), {format: 'long'}) }}</p>
  6. <p>{{ $formatTime(Date.now(), {format: 'medium'}) }}</p>
  7. <p>{{ $formatRelativeTime(Date.now() - 10000) }}</p>
  8. </div>
  9. </template>

API

Please check translated.js API for details.

mv.$formatDate(value, options)

Converts a value into formatted date string.

Name Type Required Default Description
value Date, Integer Yes - Date object.
options Object No - Options which are passed directly into the Intl.DateTimeFormat constructor.
  1. <p>{{ $formatDate(Date.now(), {month: 'numeric', year: 'numeric'}) }</p>

mv.$formatMessage(key, vars)

Compiles a message into a formatted string.

Name Type Required Default Description
key String Yes - ICU message (supports number, date, plural, and select).
vars Object No - Data object.

You can use a plural argument to select sub-messages based on a numeric value, together with the plural rules for the specified language.

  1. <p>{{ $formatMessage(`Hey {name}!`, {name: 'John'}) }</p>

mv.$formatNumber(value, options)

Converts a value into formatted string.

Name Type Required Default Description
value Number Yes - Integer or decimal number.
options Object No - Options which are passed directly into the Intl.NumberFormat constructor.
  1. <p>{{ $formatNumber(1234.56, {maximumFractionDigits: 0}) }</p>

mv.$formatRelativeTime(value, options)

Converts a value into relative time (e.g. 3 days ago).

Name Type Required Default Description
value Date, Integer Yes - Date object.
options Object No - Options for customizing the output (units and style).
  1. <p>{{ $formatRelativeTime(Date.now()) }</p>

mv.$formatTime(value, options)

Converts a value into formatted time string.

Name Type Required Default Description
value Date, Integer Yes - Date object.
options Object No - Options which are passed directly into the Intl.DateTimeFormat constructor.
  1. <p>{{ $formatTime(Date.now(), {hour: 'numeric'}) }</p>

License (MIT)

  1. Copyright (c) 2016 Kristijan Sedlak <xpepermint@gmail.com>
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated documentation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. THE SOFTWARE.