|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: requirejs-dplugins/i18n |
| 4 | +--- |
| 5 | + |
| 6 | +# requirejs-dplugins/i18n |
| 7 | + |
| 8 | +`requirejs-dplugins/i18n` is a [requirejs plugin](http://requirejs.org/docs/plugins.html) which provides support for |
| 9 | + localized string. |
| 10 | + |
| 11 | +##### Table of Contents |
| 12 | +[Creating an i18n bundle](#creating-an-i18n-bundle) |
| 13 | +[Using an i18n bundle](#using-an-i18n-bundle) |
| 14 | +[Building i18n bundles](#building-i18n-bundles) |
| 15 | + |
| 16 | +## Creating an i18n bundle |
| 17 | + |
| 18 | +Assume a `my` package, containing a `lamp` module. This module provides a string representing the color of the lamp. |
| 19 | +```js |
| 20 | +// my/lamp.js |
| 21 | +define([], function() { |
| 22 | + return { |
| 23 | + color: "red" |
| 24 | + } |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +### The root file |
| 29 | + |
| 30 | +To setup a new i18n bundle, you need to create a new root module in a `nls` directory. |
| 31 | +The root module is a hashmap of all the available locales for this bundle. This allows the plugin to load the right |
| 32 | +locale without having 404s. This module also contains a special `root` property containing the default strings. |
| 33 | + |
| 34 | +With the previous setup, to localize the color name in `my/lamp`, one will create a `my/nls/colors.js` file. |
| 35 | + |
| 36 | +```js |
| 37 | +// my/nls/colors.js |
| 38 | +define({ |
| 39 | + root: { |
| 40 | + // Default locale is en |
| 41 | + red: "red", |
| 42 | + green: "green", |
| 43 | + blue: "blue" |
| 44 | + } |
| 45 | + // Additional locales should be listed as siblings of the root property. |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +This leads to the following directory structure: |
| 50 | +``` |
| 51 | +└── my/ |
| 52 | + ├── nls/ |
| 53 | + │ └── colors.js |
| 54 | + └── lamp.js |
| 55 | +``` |
| 56 | + |
| 57 | +#### Notes: |
| 58 | +* `nls` is a mandatory marker used by the plugin to recognize i18n data and to infer the underlying directory tree. |
| 59 | +* If you rather keep your root files simple, you can add the default strings as a `root` locale like any other locale. |
| 60 | + |
| 61 | + |
| 62 | +### Add a locale |
| 63 | + |
| 64 | +The addition of a new locale is a three-steps process: |
| 65 | + |
| 66 | +1. Create a directory under `nls/` named after the lower-case locale tag. |
| 67 | +1. Create a file in this directory with the same name as the root module. This file should contain the localized string |
| 68 | +for the corresponding locale. |
| 69 | +1. Add the locale in the root hashmap. |
| 70 | + |
| 71 | +With the previous setup, following those steps to add the `fr` locale leads to: |
| 72 | + |
| 73 | +```js |
| 74 | +// my/nls/fr/colors.js |
| 75 | +define({ |
| 76 | + red: "rouge", |
| 77 | + green: "vert", |
| 78 | + blue: "bleu" |
| 79 | +}); |
| 80 | +``` |
| 81 | +```js |
| 82 | +// my/nls/colors.js |
| 83 | +define({ |
| 84 | + root: { |
| 85 | + // Default locale is en |
| 86 | + red: "red", |
| 87 | + green: "green", |
| 88 | + blue: "blue" |
| 89 | + }, |
| 90 | + // Additional locales should be listed as siblings of the root property. |
| 91 | + fr: true |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +And the following directory structure: |
| 96 | +``` |
| 97 | +└── my/ |
| 98 | + ├── nls/ |
| 99 | + │ ├── fr/ |
| 100 | + │ │ └── colors.js |
| 101 | + │ └── colors.js |
| 102 | + └── lamp.js |
| 103 | +``` |
| 104 | + |
| 105 | +#### Notes: |
| 106 | +* The plugin uses the browser's `navigator.language` or `navigator.userLanguage` property to determine the required |
| 107 | +locale. Then it selects the most suitable locale from the root bundle. The locale can also be set using |
| 108 | +requirejs config: |
| 109 | +``` |
| 110 | +require.config({ |
| 111 | + config: { |
| 112 | + // Set locale to fr |
| 113 | + "requirejs-dplugins/i18n": { |
| 114 | + locale: "fr" |
| 115 | + } |
| 116 | + } |
| 117 | +}); |
| 118 | +``` |
| 119 | + |
| 120 | +* If some strings are missing in a localized bundle, the plugin looks for those strings in less specific bundle until |
| 121 | +they are found. For instance, when processing a `fr-fr-paris` bundle, the plugin looks into: |
| 122 | +`fr-fr-paris` > `fr-fr` > `fr` > `root`. |
| 123 | + |
| 124 | + |
| 125 | +## Using an i18n bundle |
| 126 | + |
| 127 | +Once the `nls` bundle is setup, the plugin can be used to load the bundle. There are two different ways to load a bundle: |
| 128 | +* `requirejs-dplugins/i18n!./nls/bundle` loads the bundle for the user or config locale. |
| 129 | +* `requirejs-dplugins/i18n!./nls/locale/bundle` loads the bundle for the `locale` specified in the path. |
| 130 | + |
| 131 | +In the previous setup, if one want to display the color in a user's locale, `my/lamp` need to be updated to: |
| 132 | +```js |
| 133 | +// my/lamp.js |
| 134 | +define(["requirejs-plugins/i18n!./nls/colors"], function(colors) { |
| 135 | + return { |
| 136 | + color: colors.red |
| 137 | + } |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | +## Building i18n bundles |
| 143 | + |
| 144 | +This plugin is NOT compatible with r.js. The build tool recommended for an application using `requirejs-dplugins` is |
| 145 | +[grunt-amd-build](https://github.com/ibm-js/grunt-amd-build). |
| 146 | + |
| 147 | +### i18n layers |
| 148 | + |
| 149 | +During a build, all i18n bundles required by modules from a layer are concatenated in a per-locale layer, |
| 150 | +`nls/layername_locale.js`. |
| 151 | + |
| 152 | +In the previous setup, building a layer `my` containing `my/lamp` results in: |
| 153 | +``` |
| 154 | +└── my/ |
| 155 | + ├── nls/ |
| 156 | + │ ├── my_fr.js |
| 157 | + │ └── my_root.js |
| 158 | + └── my.js |
| 159 | +``` |
| 160 | + |
| 161 | +By default, the build creates a layer for all the available locales for each bundle. If only a subset of locales is |
| 162 | +needed, it can be specified as an array of locales using the `localesList` option. This option should be specified in |
| 163 | +the loader configuration of the build tool like this: |
| 164 | + |
| 165 | +```js |
| 166 | +{ |
| 167 | + config: { |
| 168 | + "requirejs-dplugins/i18n": { |
| 169 | + localesList: ["de", "en", "es", "fr", "it", "ja", "pt"] |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | + |
| 176 | +When an application is run from a layer, the i18n plugin automatically knows about the corresponding i18n layer. Hence, |
| 177 | +there is nothing to change in the application. |
| 178 | + |
| 179 | + |
| 180 | +### Layer configuration |
| 181 | + |
| 182 | +Layers can be used in various situation so the plugin offers three options to adapt to most use-cases: |
| 183 | + |
| 184 | +* `layerOnly` (default: false) |
| 185 | + |
| 186 | + If true, the plugin is only looking for i18n bundles in built i18n layers. |
| 187 | + |
| 188 | +* `enhanceLayer` (default: true) |
| 189 | + |
| 190 | + This option is ignored if `layerOnly` is true. |
| 191 | + |
| 192 | + This option defines the behaviour of the plugin when a bundle is retrieved from a layer with a less specific locale than requested (ie: bundle `en` from layer when `en-us` was requested). |
| 193 | + |
| 194 | + * If false, the plugin only uses the bundle from the layer. |
| 195 | + |
| 196 | + * If true, the plugin loads a more specific individual bundle if one exist. |
| 197 | + |
| 198 | +* `languagePack` (default: false) |
| 199 | + |
| 200 | + If true, this option gives the possibility to add new locales to the application just by adding the corresponding |
| 201 | + built nls layer. However, this results in more http requests (and some expected 404 in the console), so it |
| 202 | + should only be used in low latency environments (like cordova applications). |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +#### Use-case 1: The application is completely built and the build output is deployed |
| 207 | + |
| 208 | +In this situation, individual bundles are not deployed so the i18n plugin should not look for them. |
| 209 | + |
| 210 | +Hence the runtime configuration should be: |
| 211 | +``` |
| 212 | +requirejs.config({ |
| 213 | + config: { |
| 214 | + "requirejs-dplugins/i18n": { |
| 215 | + layerOnly: true |
| 216 | + } |
| 217 | + } |
| 218 | +}); |
| 219 | +``` |
| 220 | + |
| 221 | +#### Use-case 2: The application is partly built and use a mix of layers and individual bundles |
| 222 | + |
| 223 | +In this situation, some individual bundles are deployed but not those already included in a layer. |
| 224 | + |
| 225 | +Hence the runtime configuration should be: |
| 226 | +``` |
| 227 | +requirejs.config({ |
| 228 | + config: { |
| 229 | + "requirejs-dplugins/i18n": { |
| 230 | + enhanceLayer: false |
| 231 | + } |
| 232 | + } |
| 233 | +}); |
| 234 | +``` |
| 235 | + |
| 236 | +#### Use-case 3: The application is built and deployed in codorva |
| 237 | +In this situation, individual bundles are not deployed so the i18n plugin should not look for them. |
| 238 | +Runtime environment is low-latency so the `languagePack` option can be used to add support for additional language pack |
| 239 | +in the future. |
| 240 | + |
| 241 | +Hence the runtime configuration should be: |
| 242 | +``` |
| 243 | +requirejs.config({ |
| 244 | + config: { |
| 245 | + "requirejs-dplugins/i18n": { |
| 246 | + layerOnly: true, |
| 247 | + languagePack: true |
| 248 | + } |
| 249 | + } |
| 250 | +}); |
| 251 | +``` |
0 commit comments