|
| 1 | +import Service from '@ember/service'; |
| 2 | +import { tracked } from '@glimmer/tracking'; |
| 3 | +import { inject as service } from '@ember/service'; |
| 4 | +import { action } from '@ember/object'; |
| 5 | +import { debug } from '@ember/debug'; |
| 6 | +import { task } from 'ember-concurrency'; |
| 7 | + |
| 8 | +export default class LanguageService extends Service { |
| 9 | + @service intl; |
| 10 | + @service fetch; |
| 11 | + @tracked locales = []; |
| 12 | + @tracked countries = []; |
| 13 | + @tracked currentLocale; |
| 14 | + @tracked availableLocales = {}; |
| 15 | + |
| 16 | + get languages() { |
| 17 | + const availableLanguages = []; |
| 18 | + for (let key in this.availableLocales) { |
| 19 | + if (!key || !this.availableLocales[key]) continue; |
| 20 | + |
| 21 | + availableLanguages.push({ |
| 22 | + ...this.availableLocales[key], |
| 23 | + locale: key, |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + return availableLanguages; |
| 28 | + } |
| 29 | + |
| 30 | + constructor() { |
| 31 | + super(...arguments); |
| 32 | + |
| 33 | + this.locales = this.intl.locales; |
| 34 | + this.currentLocale = this.intl.primaryLocale; |
| 35 | + this.loadAvailableCountries.perform(); |
| 36 | + |
| 37 | + // Check for locale change |
| 38 | + this.intl.onLocaleChanged(() => { |
| 39 | + this.currentLocale = this.intl.primaryLocale; |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + @action changeLocale(selectedLocale) { |
| 44 | + this.currentLocale = selectedLocale; |
| 45 | + this.intl.setLocale(selectedLocale); |
| 46 | + this.saveUserLocale.perform(selectedLocale); |
| 47 | + } |
| 48 | + |
| 49 | + @task *loadAvailableCountries() { |
| 50 | + try { |
| 51 | + this.countries = yield this.fetch.get( |
| 52 | + 'lookup/countries', |
| 53 | + { columns: ['name', 'cca2', 'flag', 'emoji', 'languages'] }, |
| 54 | + { fromCache: true, expirationInterval: 1, expirationIntervalUnit: 'week' } |
| 55 | + ); |
| 56 | + this.availableLocales = this._createAvailableLocaleMap(); |
| 57 | + } catch (error) { |
| 58 | + debug('Locale Error: ' + error.message); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @task *saveUserLocale(locale) { |
| 63 | + try { |
| 64 | + yield this.fetch.post('users/locale', { locale }); |
| 65 | + } catch (err) { |
| 66 | + debug('[LanguageService] Unable to save user locale: ' + err.message); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + getLanguage(languageName, options = { prop: 'language' }) { |
| 71 | + const { prop } = options; |
| 72 | + |
| 73 | + return this.languages?.find((l) => l[prop] === languageName) ?? null; |
| 74 | + } |
| 75 | + |
| 76 | + hasLanguage(languageName, options = { prop: 'language' }) { |
| 77 | + return this.getLanguage(languageName, options) !== null; |
| 78 | + } |
| 79 | + |
| 80 | + _createAvailableLocaleMap() { |
| 81 | + const localeMap = {}; |
| 82 | + |
| 83 | + for (let i = 0; i < this.locales.length; i++) { |
| 84 | + const locale = this.locales.objectAt(i); |
| 85 | + |
| 86 | + localeMap[locale] = this._findCountryDataForLocale(locale); |
| 87 | + } |
| 88 | + |
| 89 | + return localeMap; |
| 90 | + } |
| 91 | + |
| 92 | + _findCountryDataForLocale(locale) { |
| 93 | + const localeCountry = locale.split('-')[1]; |
| 94 | + const country = this.countries.find((country) => country.cca2.toLowerCase() === localeCountry); |
| 95 | + |
| 96 | + if (country) { |
| 97 | + // get the language |
| 98 | + country.language = Object.values(country.languages)[0]; |
| 99 | + } |
| 100 | + |
| 101 | + return country; |
| 102 | + } |
| 103 | +} |
0 commit comments