Last Updated: February 25, 2016
·
1.314K
· francof

Simple pattern for i18n spanish genders in rails

After some projects that included i18n to spanish I found a pattern that works for translating model names with the proper genderization for the i18n-inflector gem .
Of course is still far from perfect, but it may help someone

class ApplicationController < ActionController::Base
  inflection_method :gender

  def gender
    model_name = controller_name.gsub("Controller","").downcase.singularize    
    @gender ||= (t("activerecord.models.#{model_name}") =~ /[ad]$/ ) ? :f : :m
  end

end

it would be used like this:

t(:add_model, :model => t('activerecord.models.somemodel'))
  i18n:
    inflections:
      gender:
        f: "mujer"
        m: "hombre"
...
    add_model: 'Agregar @{m:un|f:una} %{model}'

I think it will be so much better to find a way to specify the gender of nouns in the .yaml file so that translators can do it instead.