Sha256: bdc714514f659027586336268e603db837c934560f6967c1367ed6f2d14a3968

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

import { I18n } from 'i18n-js'
import flatpickr from 'flatpickr'

// Container for i18n translations
export const i18n = new I18n()

function localizeTrestle (locale, fallbacks) {
  // Set up i18n fallbacks
  i18n.enableFallback = true
  i18n.locales.register(locale, [locale, ...fallbacks])

  // Set current locale
  i18n.locale = locale
}

// Some of Flatpickr's locale names differ from Rails. This maps the Rails I18n locale to their Flatpickr equivalent.
const FlatpickrLocaleConversions = { ca: 'cat', el: 'gr', nb: 'no', vi: 'vn' }

function localizeFlatpickr (...locales) {
  for (const locale of locales) {
    const flatpickrLocale = FlatpickrLocaleConversions[locale] || locale

    if (flatpickr.l10ns[flatpickrLocale]) {
      flatpickr.localize(flatpickr.l10ns[flatpickrLocale])
      break
    }
  }
}

// Sets up localization for Trestle and its dependencies, in particular Flatpickr.
// This method accepts a list of locales in descending order of priority.
//
//     Trestle.localize('es-MX', 'es', 'en')
//
export function localize (locale, ...fallbacks) {
  localizeTrestle(locale, fallbacks)
  localizeFlatpickr(locale, ...fallbacks)
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trestle-0.10.1 frontend/js/core/i18n.js
trestle-0.10.0 frontend/js/core/i18n.js
trestle-0.10.0.pre2 frontend/js/core/i18n.js
trestle-0.10.0.pre frontend/js/core/i18n.js