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

Already on GitHub? Sign in to your account

Underscores and hyphens confuse Rails I18n #60

Closed
psy-q opened this Issue May 8, 2012 · 9 comments

Comments

Projects
None yet
3 participants

psy-q commented May 8, 2012

It seems trying to set I18n.locale to e.g. "en-GB" will break in many possible ways:

Underscores in file names

Setting I18n.locale = "en_GB".to_sym is ignored unless the directories inside locale have names with underscores. So having these directories:

locale/en_GB
locale/en_US

will work, whereas having these directories:

locale/en-GB
locale/en-US

Will not allow setting I18n.locale to anything. Setting I18n.locale = "en-US".to_sym will return :"en-US" as if it had worked, but in reality nothing will be set, I18n.locale will still be set to whatever it was before. This can be seen here:

https://gist.github.com/2635349

Can't set locale for I18n and Gettext

If the locale files are renamed to e.g. locale/en_GB so they match the gettext convetion, this breaks the rest of Rails' I18n. Using the default Rails I18n files, which define en en-GB namespace (not an en_GB one) will break all date formatting and other localizations that are supposed to come from the config/locales/foo.yml files.

The error is:

translation missing: en_GB.leihs.date.formats.day_long

What's worse, even when we set I18n.locale to "en-GB" instead of "en_GB", the error will be for en_GB because FastGettext seems to do some replacement of the underscores internally (?).

So the question is, can Gettext not coexist with Rails I18n .yml files? We want to use Gettext for translations, but Rails' built-in localizations for date and time.

Owner

grosser commented May 9, 2012

I think the part that causes your troubles is in fast_gettext/storage.rb

    #de-de -> de_DE
    def format_locale(locale)
      locale.sub(/^([a-zA-Z]{2,3})[-_]([a-zA-Z]{2,3})$/){$1.downcase+'_'+$2.upcase}
    end

can you try to patch/overwrite it with e.g.

      locale.sub(/^([a-zA-Z]{2,3})[-_]([a-zA-Z]{2,3})$/){$1.downcase+'-'+$2.upcase}

and see if all your problems go away + things still work ?

psy-q commented May 14, 2012

We've just introduced some horrible hacks to get around the problem. I will try to find the time to remove them and test the new regex.

Would you be OK with it if we'd just capture the result of [-_] and reuse that in the replacement? Or will this break fast_gettext in other places?

psy-q commented May 14, 2012

Just using a "-" or capturing the - or _ in a group and putting it into the result of the regex makes Rails' I18n stop complaining, but it doesn't work. It makes it impossible to assign I18n.locale to anything during runtime. The variable is apparently set (no error reported when doing I18n.locale = "foo-BAR"), but the locale is not changed.

Only renaming all our config/foo-BAR.yml to config/foo_BAR.yml seems to work, in conjunction with gettext locales in locale/foo_BAR.

Should we change the regex to - _and_ rename our locales from locale/foo_BAR to locale/foo-BAR, and _then_ use config/locales/foo-BAR.yml as YAML file?

psy-q commented May 14, 2012

No, renaming those directories and using - in the regex does nothing:

https://gist.github.com/2693734

I18n.locale still fails the same way.

Here is the workaround we have to make with the foo_BAR.yml files:

https://github.com/psy-q/leihs/tree/Rails3.1/config/locales

Here's our set_gettext_locale. It got very bloated because we thought having strings instead of symbols was what's breaking. Half this stuff isn't even necessary:

https://github.com/psy-q/leihs/blob/Rails3.1/app/controllers/application_controller.rb#L50

Here's the gettext directory structure -- it works perfectly if we duplicate all the .yml files as well:

https://github.com/psy-q/leihs/tree/Rails3.1/locale

Any ideas?

rollick commented May 31, 2012

Also having the same problem trying to mix gettext and rails i18n. @psy-q did you find a way to work around the issue?

psy-q commented May 31, 2012

No, the only way I found was to be extremely stupid about it and simply copy foo-bar.yml to foo_bar.yml. This is quite a braindead approach, but I could not find out how to fix it in faster_gettext or in gettext_i18n_rails.

Owner

grosser commented Jun 1, 2012

a symlink might be a slightly better hotfix

psy-q commented Jun 1, 2012

We can't check symlinks into our git repository because git doesn't handle symlinks gracefully on all platforms. We have a Mac OS X, Linux and Windows mixed environment :(

Owner

grosser commented Jun 3, 2012

0.6.0

@grosser grosser closed this Jun 3, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment