Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I18N translate "." or "%" causes a full array of ALL translations

I am having an issue in my Rails app of translation using I18n. I create dynamic sentences in arrays like this [:this_is_a, 5, :which_is_a_number, "."] for which I translate each word one by one.

When I iterate through this and translate I18n.t(".") I get the full array of all translations in my entire app which outputs a 1000+ array text. This also seem to be true for I18n.t("%").

Is there a setting that stops this from happening? My only hack solution for this at the moment is to change all "." to " . " but I am hoping there is a better solution. Any advice?

like image 848
Christoffer Avatar asked Aug 24 '16 18:08

Christoffer


People also ask

What is I18N in Rails?

The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.

What is data I18N?

data-i18n is the most powerful translations manager for javascript applications on the web. Easy to integrate: min to no effort. No external dependencies: only javascript and html :) Blazing fast: it is a web component. Works with any framework (React, Angular, Stencil, ...

What is I18N JS?

Internationalization (I18N) is the process of designing and preparing software products (apps) to support multiple locales, languages, and regions. By internationalizing a codebase, developers and businesses can expand their user base and access a wider audience.


1 Answers

"." is used for namespacing translation keys. If you have file like this:

module1:
  key_a: "Module1 Translation A"
  key_b: "Module1 Translation B"
module2:
  key_a: "Module2 Translation A"

Then you access those with "."

I18n.t("module1.key_a")
I18n.t("module2.key_a")

If you just use I18n.t(".") it will give whole top-level namespace. You can change this behavior by changing separator

I18n.t("module1@key_a", separator: "@")

Just choose some character that you know won't appear as token.

"%" character is used for interpolation:

module1:
  name: "My name is %{name}"

And then

I18n.t("module1.name", name: "John")

It doesn't look like you can change "%" to something else because it is hardcoded. But it should not return the whole array of translation. It should just return "translation missing" error. At least that is what I have on my version of I18n gem.

like image 199
Michał Młoźniak Avatar answered Oct 13 '22 01:10

Michał Młoźniak