BlueSpire/Durandal — pull request #527 by jbblanchet (2014-05-24 20:25:20+00:00)
Title: new i18n plugin

Hi,

I needed a way to internationalize our app. I looked into 2 existing modules:
- **requirejs i18n** plugin: tight and sweet, but no possibility of updating the culture without reloading the page, which is not ideal for a SPA.
- **i18next** (that you reference in the docs): it's fully featured, but it's doing a lot of stuff we already can do with the existing dependencies: loading and caching files (requirejs), ajax calls (jquery) and binding (knockout).

So this morning I decided to try my hand at a proof of concept for a lightweight plugin for Durandal.

Requirements:
- able to load and cache files from the server. **[Done]**
- able to change the culture without a page reload. **[Done]**
- strings available to the model. **[Done but could be cleaner]**
- strings available for text binding. **[Done]**
- no change to the core Durandal code. **[Ok]**
- Culture fallback (i.e. fr-ca goes to fr then to default language if not found). [TODO]
- Customizable resource locator (similar to the view locator). [TODO]
- Ability to turn i18n on or off for each module (so we do not attempt to load resources if not needed).[TODO]
- Be able to bind multiple properties (value, text, input mask, format, etc.) instead of only text. [TODO]
- Extension point for reporting not found resource files and resources. [TODO]
- Possibility of bundling multiple resource files/culture together. [TODO]
- Be able to access global resources for common tasks (like OK, cancel buttons, etc.) [TODO]
- Tests [TODO]
- Documentation [TODO]

To use, simply install the module in main.js:
``` javascript
app.configurePlugins({
i18n: {
culture: "fr-ca"
},
//[other plugins]
});
```

Add a translation file next to your viewmodel, with the culture separated by a ```.```:
```
app\viewModels\viewmodelx.fr-ca.js
```

And the i18n binding handler in your views:
``` html
Default text
```

I'm not suggesting you pull this little plugin just yet, there's a lot of work and tests still to do. I'm simply submitting the pull request in case you want to comment on the pertinence of such a plugin, submit requirements/enhancements or discuss the design decisions I've made so far.

I'm open to any ideas, suggestion or comments.

Thanks.