You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Use Glimmer component for <GetText>
* Avoid using observers for helpers
* Refactor to ES6 classes
* Refactor to use config from config/environment.js instead of extending service
Copy file name to clipboardExpand all lines: README.md
+76-85Lines changed: 76 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,28 @@ Compatibility
24
24
25
25
## Configuration
26
26
27
-
You should activate fingerprinting for your translation files, to ensure they can be properly cached.
27
+
You configure ember-l10n in your `config/environment.js`:
28
+
29
+
```js
30
+
ENV['ember-l10n'] = {
31
+
// This is required to be set
32
+
locales: ['en', 'de'],
33
+
34
+
// These are optional (defaults listed)
35
+
defaultLocale:'en',
36
+
autoInitialize:false,
37
+
defaultPluralForm:'nplurals=2; plural=(n != 1);',
38
+
jsonPath:'/assets/locales'
39
+
}
40
+
```
41
+
42
+
*`locales`: The available locales for your application.
43
+
*`defaultLocale`: The default locale to use, if no other matching locale is found.
44
+
*`autoInitialize`: If you set this to true, the browser locale will be detected on initialization and automatically be set.
45
+
*`defaultPluralForm`: Overwrite this if your default locale has a different plural form.
46
+
*`jsonPath`: The folder where the JSON files containing the translations can be found.
47
+
48
+
You should also activate fingerprinting for your translation files, to ensure they can be properly cached.
28
49
To do so, you need to add this to your `ember-cli-build.js`:
29
50
30
51
```js
@@ -71,7 +92,7 @@ be used for translations message ids from JS source:
71
92
72
93
`tVar()` works exactly the same as `t()`, but it will be ignored by the gettext parser. This is useful if your message ids are variables, for example: `l10n.t(myProperty)` would create a `myProperty` entry in your po-file when gettext is run. So in this case, `l10n.tVar(myProperty)` should be used instead.
73
94
74
-
Furthermore, there's an auto initialization feature (default: true), which detects user's locale according to system preferences. If the user's locale is supported in `availableLocales`, the corresponding translations are loaded. If the user's locale is not supported, the default locale will be used instead (default: "en"). Please use the following method to change locales:
95
+
Furthermore, there's an auto initialization feature (default: false), which detects user's locale according to system preferences. If the user's locale is supported , the corresponding translations are loaded. If the user's locale is not supported, the default locale will be used instead (default: "en"). Please use the following method to change locales:
75
96
76
97
*`setLocale(locale)`
77
98
@@ -80,71 +101,11 @@ The following utility methods are also available:
80
101
*`getLocale()`
81
102
*`hasLocale(locale)`
82
103
*`detectLocale()`
83
-
84
-
To configure the path of the JSON files (depending on the path configured via convertor's `-o` option) use the `jsonPath` property (default: "/assets/locales").
85
-
86
-
When installing via `ember install ember-l10n`, an `l10n` service will be created for you under `app/services/l10n.js`.
87
-
There, you can configure (and overwrite) all service properties/methods:
88
-
89
-
```js
90
-
import { computed } from'@ember/object';
91
-
importL10nfrom'ember-l10n/services/l10n';
92
-
93
-
exportdefaultL10n.extend({
94
-
/**
95
-
* Defines available locales as hash map, where key corresponds
96
-
* to ISO_639-1 country codes and value can be any truthy value.
97
-
* By default, it's used to translate the language codes, which
98
-
* could be used for a language drop down. Adjust the hash map
99
-
* for each new language being added your translatable project.
100
-
*
101
-
* @property availableLocales
102
-
* @type{object}
103
-
* @public
104
-
*/
105
-
availableLocales:computed('locale', function() {
106
-
return {
107
-
'en':'English'
108
-
};
109
-
}),
110
-
111
-
/**
112
-
* Flag indicating if service should try to detect user langugage
113
-
* from browser settings and load/set the corresponding JSON file.
114
-
*
115
-
* @property autoInitialize
116
-
* @type{boolean}
117
-
* @public
118
-
*/
119
-
autoInitialize:true,
120
-
});
121
-
```
122
-
123
-
You can create an initializer to inject the l10n-service everywhere with the following blueprint:
124
-
125
-
```bash
126
-
ember g ember-l10n-initializer my-l10n-initializer
For handling your translations within your handlebar templates you can use `t` and `n` helper:
108
+
For handling your translations within your templates you can use `t` and `n` helper:
148
109
149
110
###### Singular translations:
150
111
@@ -190,8 +151,8 @@ Please note: Both contextual helpers also accept placeholders just as their non-
190
151
If you have complex message ids, which should contain "dynamic" placeholders, which can also be replaced with components (such as a `link-to`), you can use the `get-text` component.
191
152
192
153
```hbs
193
-
{{#get-text
194
-
message=(t "My translation with {{dynamicLink 'optional link text'}} and {{staticLink}}.") as |text placeholder|}}
154
+
<GetText
155
+
@message={{t "My translation with {{dynamicLink 'optional link text'}} and {{staticLink}}."}} as |text placeholder|>
195
156
{{!-- You can omit the if helper if you have only one placeholder --}}
196
157
{{~#if (eq placeholder 'dynamicLink')}}
197
158
{{~#link-to 'my-route'}}
@@ -201,29 +162,14 @@ If you have complex message ids, which should contain "dynamic" placeholders, wh
201
162
{{~#if (eq placeholder 'staticLink')}}
202
163
<a href="http://www.google.com">Google</a>
203
164
{{~/if~}}
204
-
{{/get-text}}
165
+
</GetText>
205
166
```
206
167
207
-
Please note: If your message id contains HTML, you have to set `unescapeText=true` on the component. Be sure to use this option only in combination with safe strings and don't make use of it when dealing with user generated inputs (XSS)!
168
+
Please note: If your message id contains HTML, you have to set `@unescapeText={{true}}` on the component. Be sure to use this option only in combination with safe strings and don't make use of it when dealing with user generated inputs (XSS)!
208
169
209
170
### Testing
210
171
211
-
In acceptance tests, ember-l10n should work without any further work. In integration tests, you can use the provided test helpers to provide easy to use `{{t}}`,`{{tVar}}` and `{{n}}` helpers:
0 commit comments