ngTranslation
Fast, Easy and Dynamic translation for AngularJS. v0.0.3
Table of contents:
Get Started
(1) You can install ng-translation using 3 different ways:
- clone & build this repository
- Bower: by running
$ bower install ng-translation
from your terminal - via npm: by running
$ npm install ng-translation
from your terminal
(2) Include ng-translation.js
(or ng-translation.min.js
) in your index.html
, after including Angular itself.
(3) Add 'ng-translation'
to your main module's list of dependencies.
When you're done, your setup should look similar to the following:
... ... ...
Example
Quick example:
JS:
/** * Directory structure * __ __ __ __ __ __ * | - dist | * | - assets | * | - static | * |__ __ __ __ __ __| *///AngularJS appangular ;
JSON: (one file for example)
HTML:
<!-- simple usage example --> {{ 'title' | translate }} {{ 'description.text' | translate }} {{ 'button' | translate }} <!-- directive example --> <!-- bind to model example --> User Details: {{ 'message' | translate: this }} Change user details: name: password:
To learn more, Read the documentation...
Configuration
ngTranlation configuration options, see below:
setBaseUrl
Set base url for static/languages files directory.
Aliases: setDirectory
angular ;
langsFiles
Set languages files as a key value pairs.
angular ;
langsValues
Set array of values as a languages files.
angular value en: foo: 'Hello' de: foo: 'Hallo' ;
addLangFile
Add a single language file.
angular ;
setFilesSuffix
Set global suffix to all files.
angular ;
fallbackLanguage
Set fallback language.
angular ;
API
The returns API, see below:
configuration
The expose configuration
angular ;
get
Get specific file by name.
Usage: ngTranslation.get({String})
Returns: file {Object}
angular ;
getAll
Get all files(the staticFilesContainer)
Usage: ngTranslation.getAll()
Returns: files {Object}
angular ;
getUsed
Get the current used file || fallback file
Usage: ngTranslation.getUsed()
Returns: file {Object}
angular ;
init
@private
function.
use
Use specific language.(prefered language)
Usage: ngTranslation.use({String})
Returns: {Boolean}
angular ;
ngTranslationFilter
There's a 4 ways to use the translate
filter.
- simple - pass a key, and get the value from the usedFile(prefered language,
.use
).
{{ 'message' | translate }}{{ 'message.nested' | translate }}<!-- note: 'key' is a property on the scope -->{{ key | translate }}
- from specific file - pass a key, and fileName(language), and get the value from
this
file.
{{ 'message' | translate: 'en' }}{{ 'message.nested' | translate: 'de' }}<!-- note: 'key' and `lang` are a properties on the scope -->{{ key | translate: lang }}
- interpolate - there's a situation, that you want to store an angular expression as a value.
e.g:'this is string that {{ foo }}, {{ bar.baz }} need to interpolate.'
Usage:{{ key | translate: object }}
$scopeuser = name: 'Ariel M.' $scopeproperty = 'value';
<!-- note: user is a property on the scope, so if the real value on the file is:`hello {{ name }}, wanna login?`the result will be: `hello Ariel M., wanna login ?` -->{{ 'user.message' | translate: user }}<!-- note: if you want to use directly properties on $scope, use the `this` keyword,(every $scope, have the own `this` property that point to him self) -->{{ 'message' | translate: this }}
- interpolate from other file - if you want the same interpolation behavior, but get the value
from specific file.
Usage:{{ key | translate: lang: object }}
{{ 'user.message' | translate: 'de': user }}<!-- note: 'key' and `lang` are a properties on the scope -->{{ key | translate: lang: this }}
ngTranslationDirective
There's a 2 ways to use the ngTranslate
directive.
- get the value from the usedFile(prefered language,
.use
).
<!-- note: 'key' is a property on the scope -->
- get the value from specific file(specific language).
<!-- note: `en` interpolate as a string --><!-- note: 'key' is a property on the scope -->