VueJS Plugin for advanced localization of web applications using typescript
Should be installed locally in your project source code:
npm install vue-ts-locale --save
Inside your VueJS application you have to register the VueLocale
plugin:
import VueLocale from "vue-ts-locale";
Vue.use(VueLocale,
{
language: SELECTED_LANGUAGE,
currency: SELECTED_CURRENCY,
messages: MESSAGE_TEXTS
})
While these are typical examples of values:
SELECTED_LANGUAGE
: "de"
, "en"
, "fr"
, ... (any valid language identifier)SELECTED_CURRENCY
: "EUR"
, "USD"
, ... (any valid currency from CLDR data)MESSAGE_TEXTS
: { key : value, ...}
Depending on whether your clients support the Intl
API + all relevant locales (prominent exceptions right now are NodeJS, Safari on Mac and Safari on iOS) the amount of data and polyfills to load differs.
import "intl";
import "intl/locale-data/jsonp/en.js";
import "intl/locale-data/jsonp/de.js";
import "intl/locale-data/jsonp/fr.js";
import "intl/locale-data/jsonp/es.js";
The data loaded here contains information on how to format dates (+ calendar data) and numbers (+ currencies).
You should pass the matching locale data structure with relevant messages e.g. Dutch.
let messages =
{
"my-message-identifier": "Hallo wereld!",
"my-html-identifier": "Hallo <b>wereld</b>!",
"my-personal-identifier": "Hallo {name}!",
...
}
{{ "my-message-identifier" | format-message }}
{{{ "my-html-identifier" | format-message }}}
{{ $formatMessage("my-message-identifier") }}
{{{ $formatMessage("my-html-identifier") }}}
{{{ $formatMessage("my-personal-identifier", { name : screenName }) }}}
{{ 3.14159 | format-number }}
=> "3,14159"
{{ 3.14159 | format-number 2 }}
=> "3,14"
{{ 3.14159 | format-number 0 }}
=> "3"
{{ 0.641322 | format-percent }}
=> "64%"
{{ 0.641322 | format-percent 2 }}
=> "64,13%"
{{ 21.37 | format-currency }}
=> "21 €"
{{ 21.37 | format-currency-precise }}
=> "21,37 €"
{{ new Date | format-date }}
=> 12.2.2016
{{ new Date | format-time }}
=> 14:23 Uhr
{{ new Date - (1000 * 60 * 10) | format-relative }}
=> vor 10 Minuten
It is possible to use the format methods directly in TS code, but only after the plugin is initialised
import { formatMessage } from "vue-ts-locale";
formatMessage("my-message-identifier");
This plugin is based on the work of Sebastian Werner https://github.com/sebastian-software/vue-locale