UNPKG

3.98 kBMarkdownView Raw
1BabelFish - i18n for node.js
2============================
3
4[![Build Status](https://secure.travis-ci.org/nodeca/babelfish.png)][1]
5
6Internationalisation with easy syntax for node.js. Classic solutions use multiple phrases
7for plurals. But we define plurals inline - that's more compact, and easier to maintain.
8Also, phrases are grouped into nested scopes, like in Ruby.
9
10We support all pluralisation rules from [unicode CLDR][2], version [2.0.1][3].
11
12
13## Phrases Syntax
14
15- `#{varname}` Echoes value of variable
16- `((Singular|Plural1|Plural2)):count` Plural form
17
18example:
19
20 А у меня в кармане #{nails_count} ((гвоздь|гвоздя|гвоздей)):nails_count
21
22You can also omit anchor variable for plurals, by default it will be `count`.
23Thus following variants are equal:
24
25- `I have #{count} ((nail|nails))`
26- `I have #{count} ((nail|nails)):count`
27
28
29#### Escape chars
30
31If you need `#{`, `((`, `|` or `))` somewhere in text, where it can be considered
32as markup part - just escape them with `\`.
33
34
35#### Example with YAML
36
37As BabelFish supports scopes, it's really fun and nice to store translations in
38YAML files:
39
40 ---
41 ru-RU:
42 profile: Профиль
43 forums: Форумы
44 apps:
45 forums:
46 new_topic: Новая тема
47 last_post:
48 title : Последнее сообщение
49 by : от
50 demo:
51 apples: "На столе лежит #{apples.count} ((яблоко|яблока|яблок)):apples.count"
52
53
54## Usage
55
56``` javascript
57// Create new instance of BabelFish with default language/locale: 'en-GB'
58var i18n = require('babelfish').create('en-GB');
59
60
61// Fill in some phrases
62i18n.addPhrase('en-GB', 'demo.hello', 'Hello, #{user.name}.');
63i18n.addPhrase('en-GB', 'demo.conv.wazup', 'Whats up?');
64i18n.addPhrase('en-GB', 'demo.conv.alright', 'Alright, man!');
65
66i18n.addPhrase('ru-RU', 'demo.hello', 'Привет, #{user.name}.');
67i18n.addPhrase('ru-RU', 'demo.conv.wazup', 'Как дела?');
68
69i18n.addPhrase('uk-UA', 'demo.hello', 'Здоровенькі були, #{user.name}.');
70
71
72// Set locale fallback so we use most appropriate translation
73i18n.setFallback('uk-UA', 'ru-RU');
74
75
76// Translate
77var params = {user: {name: 'ixti'}};
78
79i18n.t('ru-RU', 'demo.hello', params); // -> 'Привет, ixti.'
80i18n.t('ru-RU', 'demo.conv.wazup'); // -> 'Как дела?'
81i18n.t('ru-RU', 'demo.conv.alright'); // -> 'Alright, man!'
82
83i18n.t('uk-UA', 'demo.hello', params); // -> 'Здоровенькі були, ixti.'
84i18n.t('uk-UA', 'demo.conv.wazup'); // -> 'Как дела?'
85i18n.t('uk-UA', 'demo.conv.alright'); // -> 'Alright, man!'
86
87
88// You may want to get "compiled" translations to export them into browser.
89i18n.getCompiledData('ru-RU', 'demo');
90// -> { hello : { type: 'function', translation: [Function] },
91// conv : { wazup : { type: 'string', translation: 'Как дела?' },
92// alright : { type: 'string', translation: 'Alright, man!' } } }
93```
94
95**NOTICE**
96`BabelFish#getCompiledData` just exports an object with strings/functions.
97You are responsible to serialize it and then inject into browser runtime.
98Assuming that you have serialized data and it's available on browser as
99`i18nData`, you can do following to inject them into i18n (on browser):
100
101```
102<script type="text/javascript" src="/assets/babelfish-runtime.js"></script>
103<script type="text/javascript" src="/assets/i18n.ru-RU.js"></script>
104<script type="text/javascript">
105 var i18n = new BabelFish('en-GB');
106
107 // We assume `i18n.ru-RU.js` exports `i18nData` global variable
108 i18n._storage['ru-RU'] = i18nData;
109</script>
110```
111
112
113## License
114
115View the [LICENSE][4] file (MIT).
116
117
118[1]: http://travis-ci.org/nodeca/babelfish
119[2]: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
120[3]: http://cldr.unicode.org/index/downloads
121[4]: https://github.com/nodeca/babelfish.tools/blob/master/LICENSE