mg-translate
Simple and lightweight (~3kb) angular translate library with format similar to yii or drupal
Installation
Install with Bower
$ bower install mg-translate
Install with NPM
$ npm install mg-translate
Examples
See a page on github.io as a library example
Using directive
<!-- Simple example -->Hello worldsample <!-- Translate an attribute --> <!-- Using plural rules -->{n} Day|{n} Days <!-- Using additional variables -->Hello, {user}
Using filter
{{ 'Welcome' | t }}
Using service
{ thistext = ;}
Load translations
Using provider config
angular ;
Using service
{ t; t;}
Load plural rules
Plural rule is just a function which gets a number and returns the corresponding index. You can get plural rules for required languages. on js-simple-plurals.
{ t;}
Load translation file
// app.config.js:angular ;
ru.json:
Load translations as a module config
{ 'use strict'; var TRANSLATIONS = 'Download' : 'Скачать' … ; angular ; /** @ngInject */ { tProvider; tProvider; } };
Just load this file as regular angular file
API Documentation
Provider tProvider
-
load(url:string) — Loads translation from a json file.
-
load(language:string, [category:string='app'], translations:Object) — Loads translations from Object.
-
load(language:string, '$plural', pluralRules:Function) — Loads plural rules.
-
language(language:string) — Sets an application language. If the language is not set the library tries to detect it automatically using a navigator object or a value from localStorage.lang. To disable autodetection you can set a false value.
-
language() — Returns an application language.
Service t
-
t(text:string, [null, category:string='app']) - Translates the text.
-
t(text:string, number:number, [category:string='app']) - Translates the text using plural rules. Or just replace {n} placeholder in text by the number.
-
t(text:string, values:Object, [category:string='app']) - Translates the text and replaces each placeholder by a corresponding value.
-
t.load(url:string):Promise — Loads translation from a json file
-
t.load(language:string, [category:string='app'], translations:Object) — Loads translations from Object.
-
t.load(language:string, '$plural', pluralRules:Function) — Loads plural rules.
-
t.language(language:string) — Sets an application language. If the language is not set the library tries to detect it automatically using a navigator object or a value from localStorage.lang. To disable autodetection you can set a false value.
-
t.language() — Returns an application language.
Filter t
The t filter function is the same as t() service function.
Directive t
The t directive can be used as an element or as an attribute. values and category parameters are the same as for t() service function.
Parameters
Name | Default | Description |
---|---|---|
t (optional) | 'html' | A list of attributes which must be translated, separated by comma. The html means an element content |
values (optional) | null | Number for plural rules or values to replace placeholders |
category (optional) | 'app' | Translations category |
License
Licensed under MIT.