gregschmit / recurring_select

A gem for adding selectors for setting recurring events.
MIT License
336 stars 238 forks source link

I18n localization works only for single language #150

Open hoenth opened 3 years ago

hoenth commented 3 years ago

In our app, we allow the user to switch between different translations. We were excited to see that recurring_select offers localization, but it appears that one for one language, that cannot be dynamically changed.

I believe this is because the gem is simple overwriting the $.fn.recurring_select.texts object. It only contains one set of translations, based on whatever file is being loaded last.

For example, I adding a spanish translation coffee file in the assets/javascripts/recurring_select folder called es.js.coffee

$.fn.recurring_select.texts = {
  locale_iso_code: "es"
  repeat: "Recurrencia"
  last_day: "Último día"
  frequency: "Frecuencia"
  daily: "Diaria"
  weekly: "Semanal"
  monthly: "Mensual"
  yearly: "Anual"
  every: "Cada"
  days: "día(s)"
  weeks_on: "semana(s)"
  months: "mes(es)"
  years: "año(s)"
  first_day_of_week: 1
  day_of_month: "Día del mes"
  day_of_week: "Día de la semana"
  cancel: "Cancelar"
  summary: "Resumen"
  ok: "OK"
  days_first_letter: ["D", "L", "M", "M", "J", "V", "S" ]
  order: ["1er", "2ème", "3ème", "4ème", "5ème", "Dernier"]
  show_week: [true, true, true, true, false, false]
}

Then included this file in my application.js file after I include recurring_select.

This overwrites the values in the texts object, and all translations were then in spanish. This is fine if the only language you ever need is one. But if users can dynamically switch between languages, which works in the rails app out of the box, then this implementation won't work.

I'm not seeing anywhere in the code where different locales files are selected based on the current locales value (I18n.locale)

It seems to me that the current locales value would have to be set as an element's data value, or as a js value, on each request. And the recurring_select would pull the correct text values from master locales file using whatever is the current selected locale.

I'd be grateful for any suggestions on how to modify recurring_select to do this.

hoenth commented 3 years ago

Here's what I am doing to solve it, though I don't like it much: I've added a locales file in app/assets/javascripts/recurring_select called locales.js

$.fn.recurring_select.locales = {
  en: {
    locale_iso_code: "en",
    repeat: "Repeat",
    last_day: 'Last Day',
    frequency: "Frequency",
    daily: "Daily",
    weekly: "Weekly",
    monthly: "Monthly",
    yearly: "Yearly",
    every: "Every",
    days: "day(s)",
    weeks_on: "week(s) on",
    months: "month(s)",
    years: "year(s)",
    first_day_of_week: 1,
    day_of_month: "Day of month",
    day_of_week: "Day of week",
    cancel: "Cancel",
    summary: "Resume",
    ok: "OK",
    days_first_letter: ["S", "M", "T", "W", "T", "F", "S"],
    order: ["1st", "2nd", "3rd", "4th", "5th", "Last"]
  },
  es: {
    locale_iso_code: "es",
    repeat: "Recurrencia",
    last_day: "Último día",
    frequency: "Frecuencia",
    daily: "Diaria",
    weekly: "Semanal",
    monthly: "Mensual",
    yearly: "Anual",
    every: "Cada",
    days: "día(s)",
    weeks_on: "semana(s)",
    months: "mes(es)",
    years: "año(s)",
    first_day_of_week: 1,
    day_of_month: "Día del mes",
    day_of_week: "Día de la semana",
    cancel: "Cancelar",
    summary: "Resumen",
    ok: "OK",
    days_first_letter: ["D", "L", "M", "M", "J", "V", "S" ],
    order: ["1er", "2ème", "3ème", "4ème", "5ème", "Dernier"]
  }
}

Then, at the bottom of my layout, I added:

:javascript
  $.fn.recurring_select.texts = $.fn.recurring_select.locales['#{I18n.locale}'];

Not very elegant, but functional.

Another approach would be create the translation files as described in the gem's documentation, but don't include them by default in the application.js file.

Then, have a javascript include tag that references the locale after the include for the application.js file:

= javascript_include_tag "recurring_select/#{I18n.locale}"
jcw- commented 3 years ago

thanks for raising the issue - afaik, multi-language support has not been investigated for this gem, so I'm unable to offer you any other ideas