grunt-resxtoi18n
v0.1.8
Published
Convert resx files to requireJS i18n resource files.
Downloads
5
Readme
grunt-resxtoi18n
Convert ASP.NET resx files to requireJS i18n resource files. The structure of ASP.NET resx resources is typically MyResource.resx for the main (default) resource file and then e.g. MyResource.no.resx for the norwegian resource file, MyResource.da.resx for the danish and so on. RequireJS i18n files is normally saved in a structured hierarchy with the main file at the root and then specific language files in sub directories below. Like (for the sample above): nls/myresources.js nls/nb-no/myresources.js nls/da-dk/myresources.js
Sample contents of main file:
define({
'nb-no': true,
'da-dk': true,
root:{"WindowTitle_Contacts":"Contacts","PageTitle_Address":"Address"}
});
Sample contents of language specific file:
define(
{"WindowTitle_Contacts":"Kontakter"}
);
Getting Started
This plugin requires Grunt ~0.4.5
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-resxtoi18n --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-resxtoi18n');
The "resxtoi18n" task
Overview
In your project's Gruntfile, add a section named resxtoi18n
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
resxtoi18n: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
},
},
});
Options
options.requireSyntax
Type: Boolean
Default value: false
If set to true the output will be created with requireJS syntax (using define). If set to false, the output is a plain json conversion of the resx file.
options.languageCodes
Type: Object
Default value: undefined
Only to be specified when requireSyntax is true. Defines how the language code from the resx file should be translated into the i18n notation. If the languagecodes should be used as they are (no conversion), specify an empty object (languageCodes: {}). As long as languageCodes are specified the requireJs hierarchy will be created as in the sample. E.g languageCodes: {no:'nb-no'} would cause a Common.no.resx to be converted into dest/nb-no/common.js and the attribute 'nb-no': true to be added to the main i18n file.
options.useLowerCaseLang
Type: Boolean
Default value: false
Only to be specified when requireSyntax is true. Ensure that languageCodes and destination directories are in lowercase.
options.preDefinedLanguageCodes
Type: Array
Default value: undefined
Only to be specified when requireSyntax is true. Defines language codes that are manually handled and that should be added to the main i18n file. E:g preDefinedLanguageCodes = ['nn'] would cause attribute "'nn': true" to be added to the main file.
options.sorted
Type: Boolean
Default value: false
If set to false the resources in the resulting js files will be in the same order as the original resx file. If set to true the resources will be sorted on the key value.
options.breakLines
Type: Boolean
Default value: false
If set to true the a line break will be inserted between each resource, making it easier to read.
options.extendWithBaseTranslation
Type: Boolean
Default value: false
If set to false any missing resources in sub language files will still be missing in the result files. If set to true the value from the base resx will be inserted.
options.matchPattern
Type: String
Default value: undefined
A regular expression which is used to match against resource keys.
The "resxtoi18n" task
Overview
In your project's Gruntfile, add a section named resxtoi18n
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
resxtoi18n: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
},
},
});
Usage Examples
grunt.initConfig({
resxtoi18n: {
options: {
requireSyntax: true,
languageCodes: {
no: 'nb-no',
da: 'da-dk'
}
}
common: {
src: '../App_GlobalResources/Common.resx',
dest: '/jsbuild/i18n/nls/',
}
default: {
src: '../App_GlobalResources/Default.resx',
dest: '/jsbuild/i18n/nls/',
}
},
},
});
src
Please note that the source file should point to the main resx file. This language resources from this file will be used as a fallback, when a specific translation does not exist.
dest
Destination top directory
Release History
0.1.0 First version, based on resxtojson by Eric Beragg 0.1.1 Added option sorted 0.1.2 Added option breakLines 0.1.3 Added option extendWithBaseTranslation 0.1.4 Documentation changes 0.1.5 Sorting do no longer use localeCompare. No compares binary lowercase values. 0.1.6 Allow grunt 1.0. 0.1.7 Added option preDefinedLanguageCodes