Skip to content

Commit

Permalink
add 'locale' option
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjson committed May 3, 2015
1 parent e83c925 commit ffee6f2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
15 changes: 12 additions & 3 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ module.exports = function(html, options) {
if (!options.nativeLocale) {
throw new Error('s18n: missing required option `nativeLocale`.');
}
if (!options.locales) {
throw new Error('s18n: missing required option `locales`.');
if (!options.locales && !options.locale) {
throw new Error('s18n: missing required option `locale` or `locales`.');
}
if (options.locales && options.locale) {
throw new Error('s18n: both `locale` and `locales` options are defined.');
}

// strings must be exactly between delimiters in this set to be localized
Expand All @@ -17,7 +20,10 @@ module.exports = function(html, options) {
];

var localizations = {};

if (options.locale) {
options.locales = {};
options.locales.tmp = options.locale;
}
for (var id in options.locales) {
localizations[id] = html;
for (var hash in options.nativeLocale) {
Expand All @@ -34,5 +40,8 @@ module.exports = function(html, options) {
}
}
}
if(options.locale){
localizations = localizations.tmp;
}
return localizations;
};
47 changes: 44 additions & 3 deletions test/spec.localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('s18n()', function() {
});

it('should throw an error if nativeLocale option is undefined', function() {
var html = '<p>This is a test</p>';
var html = '<p>This is a test.</p>';
var accentedLocale = {
'3c82f755': 'Thís ís á tést.'
};
Expand All @@ -34,7 +34,7 @@ describe('s18n()', function() {
});

it('should throw an error if locales option is undefined', function() {
var html = '<p>This is a test</p>';
var html = '<p>This is a test.</p>';
var nativeLocale = {
'3c82f755': 'This is a test.'
};
Expand Down Expand Up @@ -70,7 +70,48 @@ describe('s18n()', function() {
});
});

it('should localize strings only in localizable places (`>*<`, `"*"`, `\'\'`)', function() {
it('should localize some html with only one locale', function() {
var html = '<p>This is a test.</p>';
var nativeLocale = {
'3c82f755': 'This is a test.'
};
var accentedLocale = {
'3c82f755': 'Thís ís á tést.'
};
var localizedHtml = s18n(html, {
nativeLocale: nativeLocale,
locale: accentedLocale
});
assert.equal(localizedHtml, '<p>Thís ís á tést.</p>');
});

it('should error if both `locale` and `locales` are set', function() {
var html = '<p>This is a test.</p>';
var nativeLocale = {
'3c82f755': 'This is a test.'
};
var accentedLocale = {
'3c82f755': 'Thís ís á tést.'
};
assert.throws(
function() {
s18n(html, {
nativeLocale: nativeLocale,
locale: accentedLocale,
locales: {
'de': accentedLocale,
'wat': accentedLocale
}
});
},
function(err) {
if ((err instanceof Error) && /`locale`/.test(err) && /`locales`/.test(err)) {
return true;
}
}, 'unexpected error message');
});

it('should localize strings only in localizable places (/>*</, /"*"/, /\'*\'/)', function() {
var html = '<test test="test" testattr=\'test\'>test</test>';
var nativeLocale = {
'098f6bcd': 'test'
Expand Down

0 comments on commit ffee6f2

Please sign in to comment.