Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n localization works only for single language #150

Open
hoenth opened this issue Aug 11, 2021 · 2 comments
Open

I18n localization works only for single language #150

hoenth opened this issue Aug 11, 2021 · 2 comments

Comments

@hoenth
Copy link

hoenth commented Aug 11, 2021

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
Copy link
Author

hoenth commented Aug 12, 2021

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-
Copy link
Collaborator

jcw- commented Aug 16, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants