Comments (3)
This has been using eval since the very beginning I think - I didn't do the initial implementation, but it is AFAIK not that easy to properly rewrite this to static code because gettext allows a lot of kind of weird things in there that are a bit hard to support.
See this list: http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
You can add brackets, spaces etc. at will, technically. Which makes it pretty tricky/error prone to try to solve this with a regex or something like this, I fear...
I recently made a kind of soft fork of this addon: https://github.com/fabscale/ember-gettext as the current architecture is not very well suited for the newer Embroider build system - ran into lots of problems there that were kind of hard to solve without some basic architectural changes. ember-gettext is mostly drop in from an application-code perspective (meaning the usage of the service & helpers are more or less the same), but the build/generation is rather different. For the pluralization, it uses the browsers built-in Intl.PluralRules
- see here.
However, that is also not without it's problems, as the browser rules don't necessarily always match the gettext rules - as in theory you could define any plural rule for any locale, e.g. I could have a de-translation with a plural rule nplurals=2; plural=(n!=1);
, which is not really the correct rule for de, but could technically be defined. Using the Intl stuff should work fine for 98% of cases, though - I added tests for all the common languages I could think of and that should all be working OK. It's a bit of a trade off, I guess - using eval
is probably the most safe & exact implementation of the gettext protocol.
from ember-l10n.
Basically ember-gettext does not use eval at all anymore, but a different approach.
Moving it to eval-time is not really possible because as of now this is done based on the string content of the loaded translation file, which is a JSON file. So it's not really possible to do that.
Mulling this over, IMHO the best way to get this working without eval in the current implementation of this addon is to allow to configure the plural methods yourself and to ignore the plural config of the JSON file, thus optionally skipping the eval.
You could realize this yourself realtively easily with the current codebase by putting this in the extended services/l10n.js
:
_pluralFactory(pluralForm) {
// Manually map the pluralForm string to a method
// for example:
switch (pluralForm) {
case 'nplurals=2; plural=(n != 1);':
return pluralFormMethod1;
case '....':
return pluralFormMethod2;
}
function pluralFormMethod1(n) {
// this is basically the pluralForm string
let nplurals=2;
let plural=(n != 1);
// this is always the same, but could also be streamlined for each type of plural
switch (typeof plural) {
case 'boolean':
plural = plural ? 1 : 0;
break;
case 'number':
plural = plural;
break;
default:
plural = 0;
}
var max = nplurals - 1;
if (plural > max) {
plural = 0;
}
return {
plural: plural,
nplurals: nplurals
};
}
I haven't tested this, but I think that should work!
from ember-l10n.
Thanks for the rundown @mydea!
Do I understand right that in ember-gettext
the eval
stage is moved to build/compile time? Using eval
during the build process is fine, we'd like to remove it from the browser execution context.
from ember-l10n.
Related Issues (20)
- Some strings are not extracted even after upgrading to 5.2.0 HOT 6
- Add node tests for CLI functionality HOT 1
- Validate translations
- Validation seems to be buggy HOT 1
- Remove excluded.pot functionality HOT 1
- Remove comments from generated JSON files
- Ensure quotes are not changed
- Use ember-fetch instead of ember-ajax for fetching locale files HOT 1
- Add changes command
- Bring back TypeScript support for 3.x HOT 2
- Does not work with fastboot HOT 3
- tVar usage? HOT 1
- Sync command doesn't seems to work HOT 1
- Error while extracting: duplicate message definition HOT 4
- Add override option to ignore extract error HOT 2
- Upgrade to shelljs 0.8.4
- Question HOT 5
- Extract command is not picking up new strings.
- Build problems after updating ember-l10n from 4.x to 5.x HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-l10n.