ember-i18n
Advanced tools
Comparing version 3.0.0-beta.2 to 3.0.0-beta.4
{ | ||
"name": "ember-i18n", | ||
"version": "3.0.0-beta.2", | ||
"version": "3.0.0-beta.4", | ||
"description": "I18n support for Ember.js", | ||
@@ -5,0 +5,0 @@ "main": [ "lib/i18n.js" ], |
@@ -14,2 +14,8 @@ ## 3.0.0 (In Beta) | ||
## 2.9.1, 2.2.3, 2.1.1 (2014-12-24) | ||
* Handlebars-less template compiler escapes interpolations | ||
by default. Use triple-stache syntax or | ||
`Ember.Handlebars.SafeString` for HTML-safe interpolations. | ||
## 2.9.0 2014-10-20 | ||
@@ -16,0 +22,0 @@ |
@@ -1,6 +0,5 @@ | ||
(function(window) { | ||
(function() { | ||
var I18n, assert, findTemplate, get, set, lookupKey, | ||
PlainHandlebars, EmHandlebars, keyExists; | ||
EmHandlebars, keyExists; | ||
PlainHandlebars = window.Handlebars; | ||
EmHandlebars = Ember.Handlebars; | ||
@@ -61,7 +60,13 @@ get = Ember.get; | ||
var escapeExpression = EmHandlebars.Utils.escapeExpression; | ||
function compileTemplate(template) { | ||
return function(data) { | ||
return template.replace(/\{\{(.*?)\}\}/g, function(i, match) { | ||
return data[match]; | ||
}); | ||
return template | ||
.replace(/\{\{\{\s*(.*?)\s*\}\}\}/g, function(i, match) { | ||
// tripple curlies -> no-escaping | ||
return get(data, match); | ||
}).replace(/\{\{\s*(.*?)\s*\}\}/g, function(i, match) { | ||
return escapeExpression( get(data, match) ); | ||
}); | ||
}; | ||
@@ -108,13 +113,22 @@ } | ||
TranslateableProperties: Ember.Mixin.create({ | ||
init: function() { | ||
var result = this._super.apply(this, arguments); | ||
_translationObserver: function(sender, propWithSuffix) { | ||
var prop = propWithSuffix.replace(/Translation$/, ''); | ||
set(this, prop, I18n.t(this.get(propWithSuffix))); | ||
}, | ||
_addTranslationObservers: function() { | ||
eachTranslatedAttribute(this, function(attribute, translation) { | ||
this.addObserver(attribute + 'Translation', this, function(){ | ||
set(this, attribute, I18n.t(this.get(attribute + 'Translation'))); | ||
}); | ||
this.addObserver(attribute + 'Translation', this, this._translationObserver); | ||
set(this, attribute, translation); | ||
}); | ||
}.on('init'), | ||
return result; | ||
} | ||
_removeTranslationObservers: function (){ | ||
eachTranslatedAttribute(this, function(attribute) { | ||
var propWithSuffix = attribute + 'Translation'; | ||
if(this.hasObserverFor(propWithSuffix)) { | ||
this.removeObserver(propWithSuffix, this._translationObserver); | ||
} | ||
}); | ||
}.on('willDestroyElement','willClearRender') | ||
}), | ||
@@ -136,22 +150,5 @@ | ||
EmHandlebars.registerBoundHelper('t', function(key, options) { | ||
return new PlainHandlebars.SafeString(I18n.t(key, options.hash)); | ||
return new EmHandlebars.SafeString(I18n.t(key, options.hash)); | ||
}); | ||
var attrHelperFunction = function(options) { | ||
var attrs, result; | ||
attrs = options.hash; | ||
result = []; | ||
Ember.keys(attrs).forEach(function(property) { | ||
var translatedValue; | ||
translatedValue = I18n.t(attrs[property]); | ||
return result.push('%@="%@"'.fmt(property, translatedValue)); | ||
}); | ||
return new EmHandlebars.SafeString(result.join(' ')); | ||
}; | ||
EmHandlebars.registerHelper('translateAttr', attrHelperFunction); | ||
EmHandlebars.registerHelper('ta', attrHelperFunction); | ||
}).call(undefined, this); | ||
}).call(undefined); |
{ | ||
"name": "ember-i18n", | ||
"version": "3.0.0-beta.2", | ||
"version": "3.0.0-beta.4", | ||
"licence": "APLv2", | ||
@@ -25,5 +25,6 @@ "description": "Internationalization for Ember", | ||
"mocha-phantomjs": "~2.0.1", | ||
"jshint": "~2.0.1", | ||
"handlebars": "~1.0.11" | ||
"jshint": "~2.6", | ||
"handlebars": "~1.0.11", | ||
"uglify-js": "^2.4.16" | ||
} | ||
} |
@@ -21,7 +21,2 @@ ## Ember-I18n | ||
If you want to support inflection based on `count`, you will | ||
also need to include Ember-I18n's pluralization support (`lib/i18n-plurals.js`) | ||
*after* the Ember-I18n core (`lib/i18n.js`) itself and set `Ember.I18n.locale` | ||
to the current locale code (e.g. "de"). | ||
### Examples | ||
@@ -54,2 +49,25 @@ | ||
#### A translation based on a bound key: | ||
```html | ||
<h2>{{t title_i18n_key}}</h2> | ||
``` | ||
yields | ||
```html | ||
<h2> | ||
<script id="metamorph-28-start"></script> | ||
Add a user | ||
<script id="metamorph-28-end"></script> | ||
</h2> | ||
``` | ||
if `controller.title_i18n_key` is `'button.add_user.title'`. If | ||
it subsequently changes to `'user.edit.title'`, the HTML will | ||
become | ||
```html | ||
<h2> | ||
<script id="metamorph-28-start"></script> | ||
Edit User | ||
<script id="metamorph-28-end"></script> | ||
</h2> | ||
``` | ||
#### Set interpolated values directly: | ||
@@ -115,16 +133,2 @@ ```html | ||
#### Translate attributes on a plain tag: | ||
```html | ||
<a {{translateAttr title="button.add_user.title" data-disable-with="button.add_user.disabled"}}> | ||
{{t "button.add_user.text"}} | ||
</a> | ||
``` | ||
yields | ||
```html | ||
<a title="Add a user" data-disable-with="Saving..."> | ||
<script id="metamorph-28-start"></script> | ||
Add | ||
<script id="metamorph-28-end"></script> | ||
</a> | ||
``` | ||
#### Nested Translation Syntax: | ||
@@ -157,2 +161,39 @@ | ||
### Pluralization | ||
If you want to support inflection based on `count`, you will | ||
also need to include Ember-I18n's pluralization support (`lib/i18n-plurals.js`) | ||
*after* the Ember-I18n core (`lib/i18n.js`) itself and set `Ember.I18n.locale` | ||
to the current locale code (e.g. "de"). | ||
```javascript | ||
Em.I18n.locale = 'en'; | ||
``` | ||
Now whenever you pass the `count` option to the `t` function, template will be pluralized: | ||
```javascript | ||
Em.I18n.locale = 'en'; | ||
Em.I18n.translations = { | ||
'dog': { | ||
'one': 'a dog', | ||
'other': '{{count}} dogs' | ||
} | ||
}; | ||
Em.I18n.t('dog', { count: 1 }); // a dog | ||
Em.I18n.t('dog', { count: 2 }); // 2 dogs | ||
``` | ||
The suffixes 'one' and 'other' are appended automatically. | ||
Example using pluralization in the template: | ||
```html | ||
{{t 'dog' count=dogs.length}} // Assuming dogs property is an array | ||
``` | ||
Depending on the locale there could be up to 6 plural forms used, namely: 'zero', 'one', 'two', 'few', 'many', 'other'. | ||
### Limitations | ||
@@ -159,0 +200,0 @@ |
@@ -23,2 +23,6 @@ var fs = require('fs'), | ||
function useEmberHandlebarsCompiler() { | ||
return process.env.EMBER_TEMPLATE_COMPILER === 'true'; | ||
} | ||
module.exports = function buildSuite() { | ||
@@ -31,3 +35,4 @@ var template = fs.readFileSync('spec/suite.hdbs').toString(), | ||
jQueryVersion: versionFor('jquery'), | ||
handlebarsVersion: versionFor('handlebars') | ||
handlebarsVersion: versionFor('handlebars'), | ||
emberTemplateCompiler: useEmberHandlebarsCompiler() | ||
}; | ||
@@ -34,0 +39,0 @@ fs.writeFileSync(outputPath, compiledTemplate(templateData)); |
@@ -10,2 +10,5 @@ #!/usr/bin/env node | ||
"ember-1.0.1": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.0.1/ember.js", | ||
"ember-1.7.1": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.7.1/ember.js", | ||
"ember-1.9.1": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.9.1/ember.js", | ||
"ember-1.10.0": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.js", | ||
"ember-release": "http://builds.emberjs.com/release/ember.js", | ||
@@ -12,0 +15,0 @@ "ember-beta": "http://builds.emberjs.com/beta/ember.js", |
@@ -28,3 +28,7 @@ (function() { | ||
'foo.bar': 'A Foobar', | ||
'foo.bar.named': 'A Foobar named {{name}}', | ||
'foo.bar.named': 'A Foobar named <span>{{name}}</span>', | ||
'foo.bar.named.noEscape': 'A Foobar named <span>{{{link}}}</span>', | ||
'foo.bar.named.structured': 'A Foobar named {{contact.name}}', | ||
'foo.bar.named.whitespaced': 'A Foobar named {{ name }}', | ||
'foo.bar.named.noEscapeWhitespaced': 'A Foobar named {{{ name }}}', | ||
'foo.save.disabled': 'Saving Foo...', | ||
@@ -31,0 +35,0 @@ 'foos.zero': 'No Foos', |
@@ -8,4 +8,26 @@ describe('Ember.I18n.t', function() { | ||
expect(Ember.I18n.t('foo.bar.named', { | ||
name: '<Sue>' | ||
})).to.equal('A Foobar named <span><Sue></span>'); | ||
}); | ||
it('interpolates escaped', function() { | ||
expect(Ember.I18n.t('foo.bar.named.noEscape', { | ||
link: '<a href="#">Sue</a>' | ||
})).to.equal('A Foobar named <span><a href="#">Sue</a></span>'); | ||
}); | ||
it('interpolates structures correctly', function() { | ||
expect(Ember.I18n.t('foo.bar.named.structured', { | ||
contact: { name: 'Sue' } | ||
})).to.equal('A Foobar named Sue'); | ||
}); | ||
it('interpolates whitespaced values correctly', function() { | ||
expect(Ember.I18n.t('foo.bar.named.whitespaced', { | ||
name: 'Sue' | ||
})).to.equal('A Foobar named Sue'); | ||
expect(Ember.I18n.t('foo.bar.named.noEscapeWhitespaced', { | ||
name: 'Sue' | ||
})).to.equal('A Foobar named Sue'); | ||
}); | ||
@@ -12,0 +34,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
207
43233
7
26
722
4