How to work with Visual Studio resource files for localization in Single Page Applications
One of the most time saving tools in VS is the plugin ResXManager, which is an awesome assistant on managing the translations for a Desktop- or ASP.NET-App, which uses XML-based RESX files.
Mostly very localization is based on key/value pairs, defined in separate files for every language provided.
For a current project I use jquery-lang, because it provides the switch of the apps UI language without reloading and it is easy to implement. Thanks Rob Evans for your work…
The definition of “tokens” in one JSON file for each language is quite easy:
The usage also:
<div lang="en" data-lang-token="my-test">
Using RESX and convert to JSON on build
Having this, the most time consuming work is to enter the translations to the localization files. If you have hundreds of them, it is hard to keep the 2, 3 or more language files in sync. You need a helper…
And here comes ResXManager to the rescue, if you work with VS … but it needs a conversation from RESX to the JSON format jquery-lang uses and this a task, which can be done on building the JS app, by using a task runner like Grunt.
As there was no Grunt plugin/task out there to fit my needs, I have created grunt-resource2json (GitHub, NPM). The configuration in the gruntfile.js is like:
It takes one RESX file (input) and converts it to a JSON file (output) in an array of files.
The heavy work in the plugin is done by the library xml2js, which transforms the complete XML of the RESX file into a JSON object in one call. All I had to do, was to write all DATA nodes in a loop into the jquery-lang given structure and save it as JSON.
Currently supported is the format for jquery-lang only, but it would be awesome, if you fork the code on GitHub and send me a Pull Request with the implementation of your needed format.
You can interact with this article (applause, criticism, whatever) by mention it in one of your posts, which will be shown here as a Webmention, or by leaving a good old comment with your GitHub account.
No Webmentions yet...
In case your blog software can't send Webmentions, you can use this form to submit me a mention of this article...