6.4.30 • Published 1 month ago

vue-intl v6.4.30

Weekly downloads
933
License
ISC
Repository
github
Last release
1 month ago

vue-intl

Build Status codecov

Vue Plugin for FormatJS Internalization and Localization

Install

npm install vue-intl

Usage

// assuming CommonJS
var Vue = require('vue');
var VueIntl = require('vue-intl');

// use globally
Vue.use(VueIntl);

N.B. The underlying suite, FormatJS, that the VueIntl plugin relies on, requires either a browser that supports the Intl API, or has the Intl Polyfill available. As such, it is necessary for cross browser support to load the Intl polyfill (or preferably to load it if needed).

See the FormatJS Documentation for more details.

Global API Methods

setLocale

Set the current locale for the page.

Vue.setLocale('fr');

Alternatively, use a more specific locale code.

Vue.setLocale('en-GB');

registerMessages

Set an object containing messages for a particular locale.

Vue.registerMessages('fr', {
    example_message_id: "La plume de ma tante est sur le bureau de mon oncle."
});

This message will now be available when the locale is set to 'fr'.

registerFormats

Create custom formats, see FormatJS main documentation for details.

Vue.registerFormats('fr', {
    number: {
        eur: { style: 'currency', currency: 'EUR' }
    }
});

This format will now be available when the locale is set to 'fr'.

Instance Methods

These methods are for actually performing localization and translation within the context of a Vue component.

The methods are set on the Vue instance prototype, so are available locally, with access to local variables.

$formatDate

This will format dates to the locale appropriate format.

<p v-html="$formatDate(now)"></p>

Where now is a Javascript Date object or a Date coercable Number or String.

Will output the following

<p>11-05-2016</p>

(if the locale is set to 'fr').

The method can also accept a second argument of an options object - the options follow the Intl.DateTimeFormat API.

<p v-html="$formatDate(now, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })"></p>

Will output the following

<p>Mittwoch, 11. Mai 2016</p>

(if the locale is set to 'de-DE').

Additionally, the method accepts an optional format argument that provides some commonly used date formats. These are described in the FormatJS main documentation.

$formatTime

This will format times to the locale appropriate format.

<p v-html="$formatTime(now, {format: 'short'})"></p>

These formats are described in the FormatJS main documentation.

Where now is a Javascript Date object or a Date coercable Number or String.

Will output the following

<p>19 h 00</p>

(if the locale is set to 'fr').

The other options follow the Intl.DateTimeFormat API.

$formatRelative

This will render date-times relative to page load time or to an inserted now option to the locale appropriate format.

<p v-html="$formatRelative(two_days_ago)"></p>

These formats are described in the FormatJS main documentation.

Will output the following

<p>2 days ago</p>

(if the locale is set to 'en-US').

The other options follow the Intl.DateTimeFormat API.

$formatNumber

This will set numbers to the locale appropriate format.

<p v-html="$formatNumber(number_of_things)"></p>

These formats are described in the FormatJS main documentation.

Will output the following

<p>17</p>

(if the locale is set to 'en-US').

<p v-html="$formatNumber(pct_of_things, {style: 'percent'})"></p>

Will output the following

<p>12%</p>

(if the locale is set to 'en-US').

The other options follow the Intl.NumberFormat API.

$formatPlural

This will format according to plural rules for the locale appropriate format.

<p v-html="$formatPlural(number_of_things, {style: 'cardinal')"></p>

These formats are described in the FormatJS main documentation.

Will output the following

<p>17</p>

(if the locale is set to 'fr-FR').

<p v-html="$formatPlural(number_of_things, {style: 'ordinal')"></p>

These formats are described in the FormatJS main documentation.

Will output the following

<p>17eme</p>

(if the locale is set to 'fr-FR').

$formatMessage

This will translate messages according to the locale appropriate format, it will also apply any pluralization rules in the message.

Messages are specified using ICU message syntax.

<p v-html="$formatMessage({id: 'example_message_id', defaultMessage: 'It\'s my cat\'s {year, selectordinal,
    one {#st}
    two {#nd}
    few {#rd}
    other {#th}
} birthday!'}, {year: year})"></p>

These formats are described in the FormatJS main documentation.

Will output the following

<p>It's my cat's 7th birthday!</p>

(if the locale is set to 'en').

$formatHTMLMessage

Identical to $formatMessage, except that it will escape HTML specific strings to render HTML directly in the message.

Loading additional locale data

By default, only the en specific locale data is included in vue-intl. In order to load locale data for other locales, for example for proper pluralization, ordinals, and relative time formatting, you must load the relevant locale data. Ideally, you would do this dynamically, depending on the locale that is currently in use (as all locale data for all locales is in excess of 1MB).

To use a specific locale, load the data from the relevant file:

import esLocaleData from 'vue-intl/locale-data/es';
import { addLocaleData } from 'vue-intl';

addLocaleData(esLocaleData);
6.4.30

1 month ago

6.4.29

1 month ago

6.4.28

3 months ago

6.4.27

4 months ago

6.4.26

4 months ago

6.4.21

6 months ago

6.4.20

7 months ago

6.4.23

6 months ago

6.4.22

6 months ago

6.4.25

6 months ago

6.4.24

6 months ago

6.4.18

8 months ago

6.4.17

8 months ago

6.4.19

8 months ago

6.4.16

11 months ago

6.4.15

11 months ago

6.4.7

1 year ago

6.4.6

1 year ago

6.4.9

1 year ago

6.4.8

1 year ago

6.4.10

1 year ago

6.4.12

1 year ago

6.4.11

1 year ago

6.4.14

1 year ago

6.4.13

1 year ago

6.4.2

1 year ago

6.4.5

1 year ago

6.4.4

1 year ago

6.4.1

2 years ago

6.4.0

2 years ago

6.3.0

2 years ago

6.3.2

2 years ago

6.3.1

2 years ago

6.2.10

2 years ago

6.2.12

2 years ago

6.2.5

2 years ago

6.2.4

2 years ago

6.2.7

2 years ago

6.2.6

2 years ago

6.2.9

2 years ago

6.2.8

2 years ago

6.2.2

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

6.1.6

2 years ago

6.1.0

2 years ago

6.1.2

2 years ago

6.1.1

2 years ago

6.1.4

2 years ago

6.1.3

2 years ago

6.0.20

2 years ago

6.0.19

2 years ago

6.1.5

2 years ago

6.0.16

2 years ago

6.0.15

2 years ago

6.0.18

2 years ago

6.0.17

2 years ago

6.0.14

3 years ago

6.0.13

3 years ago

6.0.12

3 years ago

6.0.11

3 years ago

6.0.10

3 years ago

6.0.9

3 years ago

6.0.8

3 years ago

6.0.7

3 years ago

6.0.6

3 years ago

6.0.5

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

6.0.3

3 years ago

6.0.2

3 years ago

6.0.4

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.5

3 years ago

4.2.4

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.2.7

3 years ago

4.2.6

3 years ago

4.1.27

3 years ago

4.1.25

3 years ago

4.1.26

3 years ago

4.1.23

3 years ago

4.1.24

3 years ago

4.1.21

3 years ago

4.1.22

3 years ago

4.1.19

3 years ago

4.1.20

3 years ago

4.1.18

3 years ago

4.1.17

3 years ago

4.1.16

3 years ago

4.1.15

3 years ago

4.1.13

3 years ago

4.1.14

3 years ago

4.1.12

3 years ago

4.1.11

3 years ago

4.1.10

3 years ago

4.1.9

3 years ago

4.1.8

3 years ago

4.1.7

3 years ago

4.1.6

3 years ago

4.1.5

3 years ago

4.1.4

3 years ago

4.1.3

3 years ago

4.1.2

3 years ago

4.1.0

3 years ago

4.1.1

3 years ago

4.0.0

3 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

7 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago