ember-cli-i18n
Advanced tools
Comparing version 0.0.5 to 0.0.6
import Ember from 'ember'; | ||
import defaultservice from 'ember-cli-i18n/services/i18n'; | ||
var fmt = Ember.String.fmt; | ||
var get = Ember.get; | ||
var bind = Ember.run.bind; | ||
@@ -14,7 +13,5 @@ | ||
this.t = function(path, values) { | ||
var application = this.container.lookup('application:main'); | ||
var countryCode = application.localeStream.value(); | ||
var service = this.container.lookupFactory('service:i18n'); | ||
var result; | ||
var locale; | ||
var result; | ||
var rules; | ||
@@ -25,29 +22,17 @@ if (!Ember.isArray(values)) { | ||
if (countryCode) { | ||
locale = this.lookupLocale(countryCode); | ||
if (!service || service.lookupLocalePath) { | ||
service = defaultservice; | ||
} | ||
if (!locale) { | ||
countryCode = application.defaultLocale; | ||
locale = this.lookupLocale(countryCode); | ||
} | ||
path = read(path); | ||
result = get(locale, read(path)); | ||
locale = service.resolveLocale(this.container, this); | ||
result = service.getLocalizedPath(locale, path, this.container, this); | ||
result = service.applyPluralizationRules(result, locale, path, this.container, values, this); | ||
if (Ember.typeOf(result) === 'object') { | ||
rules = this.container.lookupFactory('ember-cli-i18n@rule:'+countryCode.split('-')[0]); | ||
var ruleResults = rules(values[0], result, path, countryCode); | ||
result = ruleResults.result; | ||
path = ruleResults.path; | ||
} | ||
Ember.assert('Missing translation for key "' + path + '".', result); | ||
Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(result) === 'string'); | ||
return fmt(result, readArray(values)); | ||
return service.fmt(result, readArray(values)); | ||
}; | ||
this.lookupLocale = function(countryCode) { | ||
return this.container.lookupFactory('locale:' + countryCode); | ||
}; | ||
} | ||
@@ -54,0 +39,0 @@ |
import Stream from 'ember-cli-i18n/utils/stream'; | ||
export default function tHelper() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var path = args.shift(); | ||
var options = args.pop(); | ||
export default function tHelper(params, hash, options, env) { | ||
var view = env.data.view; | ||
var path = params.shift(); | ||
var view = options.data.view; | ||
var container = view.container; | ||
@@ -14,8 +12,20 @@ var t = container.lookup('utils:t'); | ||
var stream = new Stream(function() { | ||
return t(path, args); | ||
return t(path, params); | ||
}); | ||
// bind any arguments that are Streams | ||
for (var i = 0, l = params.length; i < l; i++) { | ||
var param = params[i]; | ||
if(param && param.isStream){ | ||
param.subscribe(stream.notify, stream); | ||
}; | ||
} | ||
application.localeStream.subscribe(stream.notify, stream); | ||
if (path.isStream) { | ||
path.subscribe(stream.notify, stream); | ||
} | ||
return stream; | ||
} |
@@ -7,3 +7,3 @@ import Ember from 'ember'; | ||
export function initialize(container, application) { | ||
Ember.Handlebars.registerHelper('t', tHelper); | ||
Ember.HTMLBars._registerHelper('t', tHelper); | ||
@@ -10,0 +10,0 @@ application.localeStream = new Stream(function() { |
@@ -6,4 +6,4 @@ { | ||
"jquery": "^1.11.1", | ||
"ember": "1.9.0", | ||
"ember-data": "1.0.0-beta.12", | ||
"ember": "1.10.0", | ||
"ember-data": "1.0.0-beta.14.1", | ||
"ember-resolver": "~0.1.10", | ||
@@ -18,2 +18,2 @@ "loader.js": "stefanpenner/loader.js#1.0.1", | ||
} | ||
} | ||
} |
{ | ||
"name": "ember-cli-i18n", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Simple Internationalization support for ember-cli apps.", | ||
@@ -22,8 +22,7 @@ "directories": { | ||
"broccoli-asset-rev": "^1.0.0", | ||
"broccoli-ember-hbs-template-compiler": "^1.6.1", | ||
"ember-cli": "0.1.4", | ||
"ember-cli-htmlbars": "^0.5.1", | ||
"ember-cli-content-security-policy": "0.3.0", | ||
"ember-cli-dependency-checker": "0.0.6", | ||
"ember-cli-esnext": "0.1.1", | ||
"ember-cli-htmlbars": "^0.6.0", | ||
"ember-cli-ic-ajax": "0.1.1", | ||
@@ -30,0 +29,0 @@ "ember-cli-inject-live-reload": "^1.3.0", |
@@ -9,3 +9,3 @@ # Ember CLI i18n | ||
**Note: This release requires Ember 1.9. Will likely break with 1.10** | ||
**Note: This release requires Ember 1.10.** | ||
@@ -171,2 +171,45 @@ ## Install ## | ||
### Overriding the Locale Lookup Handler | ||
By default locales are attempted to be looked as modules in your | ||
project. However, you may wish to override how this is done. You can do that by overriding the locale lookup handler. Let's assume you have all of your locales stored in a single POJO. | ||
You'll first need to create a new file: `my-app/services/i18n.js` | ||
```javascript | ||
import service from 'ember-cli-i18n/services/i18n'; | ||
service.getLocalizedPath = function(locale, path) { | ||
return Locales[locale][path]; | ||
} | ||
export default service; | ||
``` | ||
The default service object that was imported has three functions that | ||
can be overridden and customized: | ||
#### `resolveLocale` | ||
* **Paramaters**: `container`, `scope` | ||
* **Returns**: locale code | ||
#### `getLocalizedPath` | ||
* **Paramaters**: `locale`, `path`, `container`, `scope` | ||
* **Returns**: string or object | ||
#### `applyPluralizationRules` | ||
* **Paramaters**: `result`, `locale`, `path`, `container`, `values`, | ||
`scope` | ||
* **Returns**: if `result` is a string, will skip rules and return | ||
`result`. If `result` is an `Object`, will assume pluralization needs | ||
to apply and formats `result` with proper pluralization rules based upon | ||
`values[0]` | ||
#### `fmt` | ||
* **Paramaters**: `result`, `values` | ||
* **Returns**: formatted string | ||
This function delegates to `Ember.String.fmt` by default. You can override | ||
## Authors ## | ||
@@ -173,0 +216,0 @@ |
@@ -6,40 +6,42 @@ /* globals requirejs, require */ | ||
var keys = Ember.keys; | ||
if (window.QUnit) { | ||
var keys = Ember.keys; | ||
var locales, defaultLocale; | ||
module('ember-cli-i18n', { | ||
setup: function() { | ||
var localRegExp = new RegExp(config.modulePrefix + '/locales/(.+)'); | ||
var match, moduleName; | ||
var locales, defaultLocale; | ||
module('ember-cli-i18n', { | ||
setup: function() { | ||
var localRegExp = new RegExp(config.modulePrefix + '/locales/(.+)'); | ||
var match, moduleName; | ||
locales = {}; | ||
locales = {}; | ||
for (moduleName in requirejs.entries) { | ||
if (match = moduleName.match(localRegExp)) { | ||
locales[match[1]] = require(moduleName)['default']; | ||
for (moduleName in requirejs.entries) { | ||
if (match = moduleName.match(localRegExp)) { | ||
locales[match[1]] = require(moduleName)['default']; | ||
} | ||
} | ||
defaultLocale = locales[config.APP.defaultLocale]; | ||
} | ||
}); | ||
defaultLocale = locales[config.APP.defaultLocale]; | ||
} | ||
}); | ||
test('locales all contain the same keys', function() { | ||
var knownLocales = keys(locales); | ||
if (knownLocales.length === 1) { | ||
expect(0); | ||
return; | ||
} | ||
test('locales all contain the same keys', function() { | ||
var knownLocales = keys(locales); | ||
if (knownLocales.length === 1) { | ||
expect(0); | ||
return; | ||
} | ||
for (var i = 0, l = knownLocales.length; i < l; i++) { | ||
var currentLocale = locales[knownLocales[i]]; | ||
for (var i = 0, l = knownLocales.length; i < l; i++) { | ||
var currentLocale = locales[knownLocales[i]]; | ||
if (currentLocale === defaultLocale) { | ||
continue; | ||
} | ||
if (currentLocale === defaultLocale) { | ||
continue; | ||
for (var translationKey in defaultLocale) { | ||
ok(currentLocale[translationKey], '`' + translationKey + '` should exist in the `' + knownLocales[i] + '` locale.'); | ||
} | ||
} | ||
for (var translationKey in defaultLocale) { | ||
ok(currentLocale[translationKey], '`' + translationKey + '` should exist in the `' + knownLocales[i] + '` locale.'); | ||
} | ||
} | ||
}); | ||
}); | ||
} |
@@ -8,3 +8,3 @@ export default { | ||
}, | ||
dependent_person: { | ||
dependentPerson: { | ||
one: 'There is %@ dependent person here', | ||
@@ -11,0 +11,0 @@ other: 'There are %@ dependent people here' |
@@ -8,3 +8,3 @@ export default { | ||
}, | ||
dependent_person: { | ||
dependentPerson: { | ||
one: 'es_There is @% dependent person here', | ||
@@ -11,0 +11,0 @@ other: 'es_There are @% dependent people here' |
import T from 'ember-cli-i18n/utils/t'; | ||
import Ember from 'ember'; | ||
var get = Ember.get; | ||
var container; | ||
@@ -29,3 +30,4 @@ var application; | ||
home: { | ||
title: 'Welcome' | ||
title: 'Welcome', | ||
namedTitle: 'Welcome %@' | ||
}, | ||
@@ -77,2 +79,3 @@ number: 'Number: %@1', | ||
var splitName = name.split(/[@|:]/); | ||
var module; | ||
if (splitName.length === 2) { | ||
@@ -84,3 +87,8 @@ splitName.unshift('dummy'); | ||
var module = require(splitName.join('/')); | ||
try { | ||
module = require(splitName.join('/')); | ||
} | ||
catch(e) { | ||
return null; | ||
} | ||
@@ -120,2 +128,8 @@ if (module && module['default']) { module = module['default']; } | ||
test('can lookup in a path and does not always assume pluralization', function() { | ||
application.defaultLocale = 'en'; | ||
equal(t('home.namedTitle', 'Brian'), 'Welcome Brian'); | ||
}); | ||
test('interpolation', function() { | ||
@@ -169,1 +183,59 @@ application.defaultLocale = 'en'; | ||
}); | ||
test('can override the locale lookup handler', function() { | ||
define('dummy/services/i18n', [], function() { | ||
return { | ||
getLocalizedPath: function(locale, path) { | ||
var translations = { | ||
'en': { | ||
'foo': 'bizbar' | ||
} | ||
}; | ||
return get(translations[locale], path); | ||
}, | ||
resolveLocale: function() { | ||
return 'en'; | ||
}, | ||
applyPluralizationRules: function(result) { | ||
return result; | ||
}, | ||
fmt: function(result) { | ||
return result; | ||
} | ||
}; | ||
}); | ||
application.defaultLocale = 'en'; | ||
equal(t('foo'), 'bizbar'); | ||
}); | ||
test('can override the format handler', function() { | ||
define('dummy/services/i18n', [], function() { | ||
return { | ||
getLocalizedPath: function(locale, path) { | ||
var translations = { | ||
'en': { | ||
'foo': 'bizbar' | ||
} | ||
}; | ||
return get(translations[locale], path); | ||
}, | ||
resolveLocale: function() { | ||
return 'en'; | ||
}, | ||
applyPluralizationRules: function(result) { | ||
return result; | ||
}, | ||
fmt: function(result) { | ||
return 'barbiz'; | ||
} | ||
}; | ||
}); | ||
application.defaultLocale = 'en'; | ||
equal(t('foo'), 'barbiz'); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
66654
13
101
1810
236
0