Comments (4)
If I understand correctly, you want a property that gives you the current locale -- represented in that locale. For example, "English"
, "Deutsch"
, "ру́сский"
, "日本語"
, "普通話"
. If that's the case, I would recommend something like
// locales/en/translations.js
currentLanguage: "English",
// locales/de/translations.js
currentLanguage: "Deutsch",
// locales/ru/translations.js
currentLanguage: "ру́сский",
// locales/jp/translations.js
currentLanguage: "日本語",
// locales/zh/translations.js
currentLanguage: "普通話",
and
from ember-i18n.
Not really :). I'm using ember-cli-bootstrap-datepicker. To be able to display the calendar by locale, as the owner of the add-on explains in the docs, I'll have to pass in a language String value like this:
{{! somewhere in template }}
{{bootstrap-datepicker value=expiresAt language="de"}}
That's why the current solution I came to is to use a CP in my component(it seems to work as needed):
# components/holiday-row.js
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
i18n: service('i18n'),
tagName: '',
currentLanguage: '',
init() {
this._super(...arguments);
this.currentLanguage = this.get('i18n').get('locale');
},
...
Then use it in the component template:
# templates/components/holiday-rwo.hbs
{{bootstrap-datepicker value=holiday.date class='form-control' autoclose=true language=currentLanguage}}
I'm setting the default locale as it is explained in setting the locale section:
# instance-initializers/i18n.js
export function initialize(applicationInstance) {
let i18n = applicationInstance.lookup('service:i18n');
let moment = applicationInstance.lookup('service:moment');
let locale = calculateLocale(i18n.get('locales'));
i18n.set('locale', locale);
moment.setLocale(locale);
}
function calculateLocale(locales) {
// whatever you do to pick a locale for the user:
const language = navigator.languages[0] || navigator.language || navigator.userLanguage;
return locales.includes(language.toLowerCase()) ? language : 'en-GB';
}
export default {
name: 'i18n',
initialize
};
Thank you.
from ember-i18n.
There's no built-in way to get the ID of the current locale from a helper, but it's easy to do with a component:
import Component from '@ember/component'
import { readOnly } from '@ember/object/computed'
import { inject as service } from '@ember/service'
export default Component.extend({
i18n: service(),
localeId: readOnly('i18n.locale')
})
and
from ember-i18n.
@jamesarosen Thank you very much, didn't know that.
from ember-i18n.
Related Issues (20)
- Adding a `scope` option HOT 3
- Compatibility with fastboot?
- helper creates Ember.Object.extend().create() HOT 3
- emberGetownerPolyfill issue in i18n service HOT 4
- Refactor test helpers
- Passing in multiple translation keys (perhaps to Safely concat them together?) HOT 11
- Setting locale as described fails HOT 2
- switching language locally, not globally HOT 2
- require in get-locales clashes with other locally defined requires HOT 10
- Quest: intent to deprecate HOT 18
- Issue using ember-i18n in qunit 4.2.1 and ember-cli version 2.18.2 HOT 12
- Pluralized do not behave always the same way HOT 7
- Call a translation key dynamically HOT 2
- Dynamic update of <html lang="en"> with FastBoot support HOT 2
- Interpolating quotes HOT 2
- ;import Service from "ember-i18n/service"; HOT 2
- Babel error HOT 1
- undefined is not an object (evaluating 'this.get('i18n').t') HOT 1
- _locale Computed property is not called more than once HOT 1
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-i18n.