grunt-resx-2-structured-json
v0.4.1
Published
Changing resx files to structured, nested json files
Downloads
1
Readme
grunt-resx-2-structured-json
Converts a set of localization files in the resx file format to a set of JSON dictionaries
Getting Started
This plugin requires Grunt ~0.4.0
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 git+https://github.com/jrnt30/grunt-resx2json.git --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-resx2json');
resx2json task
Run this task with the grunt resx2json
command.
Task targets, files and options may be specified according to the grunt Configuring tasks guide.
Options
namespaceFrom
Type: 'String'
Default: ''
In creating the output, each underlying folder from the 'namespaceFrom' root will be treated as a namespace for the sake of JSON generation.
For example:
Given a folder structure like so:
├── Localization/
│ ├── base-de.resx
│ ├── base-fr.resx
│ ├── base.resx
| ├── Home/
| | ├── stuff.resx
| | ├── stuff-de.resx
| | └── stuff-fr.resx
Where namespace from is set to Localization\
Then an object like so would be created:
{
//keys from base.resx would be here
Home: {
//keys from Home/stuff.resx would be here
}
}
defaultLocale
Type: String
Default: en
The locale that a file will be assumed to represent should the languagePattern not parse appropriately.
concat
Type: Boolean
Default: false
Set concat
to true
to have all of the different locales be concated into a single output file, with their respective locale
being the key and the value being the set of all corresponding values.
The default is false, which will then output a file per locale with the file format dest
-locale``ext
dest
Type: String
Default: dist/
The output folder for the processed files.
prefix
Type: String
Defaut: output
When concat
is false
, this serves as the base name for the output, which will have the pattern prefix
-locale``ext
.
When concat
is true
, the output will be in the format prefix``ext
.
ext
Type: 'String' Default: '.json'
The default extension for the output files to have.
languagePattern
Type: RegExp
Default: /^.+-(\w+).resx$/
A regular expression with a single capture group that extracts the appropriate locale
localeExtractor
Type: Function
Default: function(src, options){return dest}
A function given with arguments (src,options) that is responsible for providing the locale for the given file.
This is used by the plugin to group all the files of a single locale.
Usage examples
Src file declaration
In this example, simply using the common src
attribute from standard Grunt configuration to specify the set of Resx files to process. This will result in a different file per locale in the dest
location with ext
extension.
// Project configuration.
grunt.initConfig({
resx2json: {
src: ['templates/**/*.resx']
}
});
File Structure:
$ tree -I templates
.
├── baseLocalization/
│ ├── base-de.resx
│ ├── base-fr.resx
│ └── base.resx
└── extended/
├── extended-de.resx
├── extended-fr.resx
└── extended.resx
2 directories, 6 files
File Output Structure:
$ tree -I dist
.
└── dist/
|── output-de.json
|── output-en.json
└── output-fr.json
Custom prefix attribute
In this example, remove the prefix
attribute from standard Grunt configuration to customize file output to be locale``ext
// Project configuration.
grunt.initConfig({
resx2json: {
src: ['templates/**/*.resx'],
prefix: ''
}
});
File Structure:
$ tree -I templates
.
├── baseLocalization/
│ ├── base-de.resx
│ ├── base-fr.resx
│ └── base.resx
└── extended/
├── extended-de.resx
├── extended-fr.resx
└── extended.resx
2 directories, 6 files
File Output Structure:
$ tree -I dist
.
└── dist/
|── de.json
|── en.json
└── fr.json
Concat
In this example, running grunt resx2json
will result in a single json file, where there is a top level attribtue for each locale.
// Project configuration.
grunt.initConfig({
resx2json: {
src: ['templates/**/*.resx'],
options: {
concat: true
}
}
});
File Structure:
$ tree -I templates
.
├── baseLocalization/
│ ├── base-de.resx
│ ├── base-fr.resx
│ └── base.resx
└── extended/
├── extended-de.resx
├── extended-fr.resx
└── extended.resx
2 directories, 6 files
File Output Structure:
$ tree -I dist
.
└── dist/
└── output.json
Specifying custom locale pattern
In this example, we use a custom pattern to extract the locale of the files we are parsing. The will result in a file per parsed locale being generated.
// Project configuration.
grunt.initConfig({
resx2json: {
src: ['templates/**/*.resx'],
options: {
localePattern: /([a-z]{2,2})-.*$/
}
}
});
File Structure:
$ tree -I templates
.
├── baseLocalization/
│ ├── de-base.resx
│ ├── fr-base.resx
│ └── base.resx
└── extended/
├── de-extended.resx
├── fr-extended.resx
└── extended.resx
2 directories, 6 files
File Output Structure:
$ tree -I dist
.
└── dist/
|── output-de.json
|── output-en.json
└── output-fr.json