Giter VIP home page Giter VIP logo

Comments (4)

jamesarosen avatar jamesarosen commented on September 22, 2024

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

{{t 'currentLanguage'}}

from ember-i18n.

belgoros avatar belgoros commented on September 22, 2024

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.

jamesarosen avatar jamesarosen commented on September 22, 2024

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

Currently translating to {{localeId}}

from ember-i18n.

belgoros avatar belgoros commented on September 22, 2024

@jamesarosen Thank you very much, didn't know that.

from ember-i18n.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.