Pluralization with default_locale not working in Rails4 Beta1 #10125

Closed
darnreich opened this Issue Apr 7, 2013 · 10 comments

7 participants

@darnreich

I am just trying to setup an application in Rails 4, Beta 1. The application in (exclusively) in German and therefore also the URLs should be German. I want to create a controller and model for an entity called "Region"; Rails pluralizes that to "Regions", but it should be "Regionen". What I did is: in config/application.rb uncomment this line:

config.i18n.default_locale = :de

And in config/initializers/inflections.rb I put this:

ActiveSupport::Inflector.inflections(:de) do |inflect|
  inflect.irregular 'region', 'regionen'
end

But its not working... When I change the inflection to

inflect.irregular 'region', I18n.locale

and remove the (:de) I can see that the passed locale is :en and not :de. Why is my default locale not passed to this inflections?

Thanks

@carlosantoniodasilva
Ruby on Rails member

At this poing of defining the inflections, the default locale doesn't actually matter, it's just a "definition" of the inflections for a particular locale. When you change the locale in the app, then it should pick up the inflections based on the locale, and if that's not working I guess we might have a bug.

@darnreich

OK, cool.
Can you tell me how and where I can set the locale!?

@carlosantoniodasilva
Ruby on Rails member

Is your application setting the locale somewhere else? Or is it just supposed to be using :de and that's it? If you're only using one locale, I imagine that those inflections should work.

@darnreich

The locale is only set in the application.rb file.
If I try this inside a view:

<%= pluralize(2, 'region') %>            
<br />
<%= I18n.locale %>

...it gives me "regions" (which is the wrong plural) but :de as locale. So the locale seems to be set correctly...

@carlosantoniodasilva
Ruby on Rails member

Hmm... apparently the inflection methods like pluralize don't use the current application locale, they accept a second argument that is the locale to fetch from.

This has been added recently to master but I'd say it should make use of the current i18n locale if possible.

@fxn do you have any thoughts about it?

@steveklabnik steveklabnik added a commit to steveklabnik/rails that referenced this issue Apr 10, 2013
@thenickcox thenickcox Fix inflector to respect default locale.
The inflector was made aware of locales in 7db0b07,
but it defaulted to :en. That should actually be our default
locale instead.

Fixes #10125
8cf88cc
@steveklabnik steveklabnik added a commit that closed this issue Apr 10, 2013
@thenickcox thenickcox Fix inflector to respect default locale.
The inflector was made aware of locales in 7db0b07,
but it defaulted to :en. That should actually be our default
locale instead.

Fixes #10125
8cf88cc
@josevalim
Ruby on Rails member

The proper fix to this issue is to change pluralize to pass the default locale when calling the inflections.

@josevalim josevalim reopened this Apr 10, 2013
@fxn
Ruby on Rails member
fxn commented May 1, 2013

Just for the record, there are some conflicts here.

Up to Rails 4, the inflector had no support for multiple locales. There was only one set of rules. The application has a default locale, and in a i18n application each request may have a different locale, but that didn't affect the inflector.

The inflector is not only used by the application, it is also used by the framework to convert paths to class names, class names to tables, create method names dynamically for the associations API, etc.

Obviously, those computations cannot vary. If your schema has a "regions" table, Active Record has to map the Region class always to the "regions" table, no matter the evolution of the application (unless the schema changes, but the schema has to be visualized as mostly static regarding to this, much more static that a configuration option).

I have worked on applications that started development using :en, get i18ned, and then switch to a default locale of :es. The locale is something that affects the interface in that mindset. Everything internal should work as it did before.

You should be able to change the default locale and everything else in a way that does not affect static things like association names, tables, routes, etc.

In could be the case that you have i18n routes (which change with the locale of the request), but in general the statement above should be true.

In order to be as backwards compatible as possible, we have left the framework untouched, and have made the inflections to have a default of :en so that existing applications get the same mappings after an upgrade.

I believe this new argument to the inflector methods needed more thought, its addition has conflicting consequences.

@darnreich darnreich added the stale label Apr 23, 2014
@rafaelfranca
Ruby on Rails member

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this May 27, 2014
@klyonrad

is still broken on 4.2.5.2. 😡

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