Skip to content

Commit

Permalink
Merge pull request #5 from beck/doug/locale-bug
Browse files Browse the repository at this point in the history
Fix issue with locale discovery
  • Loading branch information
beck committed May 19, 2014
2 parents 6c68532 + 4ad9ce3 commit a690f86
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -94,7 +94,7 @@ module.exports = function(grunt) {
testAppCatalogs: {
options: {
template: '<%= xgettext.options.potFile %>',
languages: ['pt-br', 'fr'], // fancy, changes to locales on create
languages: ['pt-br', 'fr', 'zu'], // fancy, lang > locales on create
localeDir: 'test/fixtures/app/locale',
}
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -106,6 +106,7 @@ Run `grunt && open coverage.html`

## Release History

* 0.3.0 - fix issue with locale discovery
* 0.2.0 - quote translated text when rendering javascript
* 0.1.0 - render non-language defaults to destination
* 0.0.2 - add coverage reporting
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "grunt-static-i18n",
"version": "0.2.0",
"version": "0.3.0",
"description": "Grunt plugin to translate static assets.",
"homepage": "https://github.com/beck/grunt-static-i18n",
"repository": "https://github.com/beck/grunt-static-i18n",
Expand Down
20 changes: 14 additions & 6 deletions tasks/statici18n.js
Expand Up @@ -15,7 +15,7 @@ module.exports = function statici18n(grunt) {
var Gettext = require('node-gettext');

var gt = new Gettext();
var options, locales;
var locales;

var save = function(file, content, lang) {
lang = lang || '';
Expand All @@ -36,7 +36,9 @@ module.exports = function statici18n(grunt) {

var loadTranslations = function() {
locales.forEach(function readPo(lang) {
var po = path.join(options.localeDir,lang,'LC_MESSAGES','messages.po');
var po = path.join(
statici18n.options.localeDir, lang, 'LC_MESSAGES', 'messages.po'
);
if (!grunt.file.exists(po)){
grunt.log.warn('Translations not found: ' + po);
return;
Expand All @@ -49,14 +51,20 @@ module.exports = function statici18n(grunt) {
var getLocales = function() {
var locales = grunt.file.expand({
filter: 'isDirectory',
cwd: options.localeDir
cwd: statici18n.options.localeDir
}, '*');

if (!locales.length) {
grunt.fail.warn('Unable to find any languages in locale directory.');
}
locales.pop('template');

var index = locales.indexOf('template');
if (index > -1) {
locales.splice(index, 1);
}
return locales;
};
statici18n.getLocales = getLocales;

var gettext = function(msgid) {
var text = gt.gettext(msgid);
Expand All @@ -70,7 +78,7 @@ module.exports = function statici18n(grunt) {

var compileTemplate = function(filepath) {
var compiled;
_.templateSettings = options.template;
_.templateSettings = statici18n.options.template;
try {
return compiled = _.template(grunt.file.read(filepath));
} catch (e) {
Expand Down Expand Up @@ -115,7 +123,7 @@ module.exports = function statici18n(grunt) {
statici18n.exists = exists;

var plugin = function() {
options = this.options({
statici18n.options = this.options({
localeDir: 'locale',
template: {
interpolate: /(_\((?:'[^']+?'|"[^"]+")\))/g,
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/app/locale/zu/LC_MESSAGES/messages.po
@@ -0,0 +1,13 @@
msgid ""
msgstr ""
"Project-Id-Version: grunt-static-i 18n\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: zu\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ASCII\n"
"Content-Transfer-Encoding: 8bit\n"

# Generated by grunt-xgettext on Mon May 19 2014 14:33:07 GMT-0400 (EDT)
msgid "Hello World"
msgstr "Sawubona Mhlaba"
31 changes: 31 additions & 0 deletions test/statici18n-spec.js
Expand Up @@ -54,6 +54,27 @@ describe('save', function() {

});

describe('get locales', function() {

after(function() {
grunt.fail.warn.restore();
});

it('returns all locales found in fixture', function() {
var localeDir = path.join(__dirname, 'fixtures', 'app', 'locale');
statici18n.options = { 'localeDir': localeDir };
assert.deepEqual(['fr', 'pt_BR', 'zu'], statici18n.getLocales());
});

it('failes when no locales are found', function() {
sinon.stub(grunt.fail, 'warn');
statici18n.options = { 'localeDir': 'nonexistent directory' };
statici18n.getLocales();
sinon.assert.calledOnce(grunt.fail.warn);
});

});

describe('static i18n', function() {

before(function(done) {
Expand All @@ -70,6 +91,7 @@ describe('static i18n', function() {
var i18n = path.join(__dirname, 'fixtures', 'app', 'i18n');
var fr = path.join(i18n, 'fr', 'static', 'data.json');
var pt = path.join(i18n, 'pt_BR', 'static', 'data.json');
var zu = path.join(i18n, 'zu', 'static', 'data.json');
var def = path.join(i18n, 'static', 'data.json');

it('should create a file for each language', function() {
Expand All @@ -93,13 +115,18 @@ describe('static i18n', function() {
assert.equal('["Olá mundo"]\n', grunt.file.read(pt));
});

it('should translate zulu', function() {
assert.equal('["Sawubona Mhlaba"]\n', grunt.file.read(zu));
});

});

describe('of javascript files', function() {

var i18n = path.join(__dirname, 'fixtures', 'app', 'i18n');
var fr = path.join(i18n, 'fr', 'static', 'script.js');
var pt = path.join(i18n, 'pt_BR', 'static', 'script.js');
var zu = path.join(i18n, 'zu', 'static', 'script.js');
var def = path.join(i18n, 'static', 'script.js');

it('should create a file for each language', function() {
Expand All @@ -123,6 +150,10 @@ describe('static i18n', function() {
assert.equal('"Olá mundo";\n', grunt.file.read(pt));
});

it('should translate zulu', function() {
assert.equal('"Sawubona Mhlaba";\n', grunt.file.read(zu));
});

});

});

0 comments on commit a690f86

Please sign in to comment.