Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organization of Locale Files in rails app

I currently have the following 4 files in my config/locales of my root application:

-en.yml
-de.yml
-simple_form.en.yml
-simple_form.de.yml

In my application.rb which resides in a spec/dummy folder for testing the application gem I have the following line of code which seems to be retrieving the translations as expected:

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :de

I now wish to introduce to structure to the file structure of my locales folder but when I add the additional folders and change the load path in the application.rb I am getting translation not found errors. Here is my attempt:

Attempt at including structure in config/locales of my root application:

-views
  -en.yml
  -de.yml
-models
  -en.yml
  -de.yml
-forms
  -simple_form.en.yml
  -simple_form.de.yml

And my load path in the application.rb changed to:

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

According to the following rails guide: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-domain-name

like image 616
Jay Avatar asked May 24 '12 10:05

Jay


People also ask

What are locale files?

Locale files are JSON files that contain a set of translations for text strings used throughout the theme and theme editor.

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 I18n t?

Internationalization (i18n) is the process of preparing software so that it can support local languages and cultural settings. An internationalized product supports the requirements of local markets around the world, functioning more appropriately based on local norms and better meeting in-country user expectations.

Is en a valid locale?

I18n::InvalidLocale: :en is not a valid locale. Bookmark this question.


1 Answers

To test the host application you need to change the i18n.load_path to the config folder of your main app and not the dummy spec for testing purposes. Code as follows:

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.default_locale = :en
like image 178
John Paul Mc Feely Avatar answered Oct 03 '22 14:10

John Paul Mc Feely