Skip to content

Locale Management

cgebken edited this page Nov 10, 2020 · 6 revisions

The locale management is an out of the box feature of Beyond Canvas.

If you have I18n installed and more than one locale set in the available_locales attribute, a locale switch dropdown will be automatically displayed on all views using the Beyond Canvas layout.

How it works

Every time a page is requested, Beyond Canvas will search for a cookie called locale. If no locale is set in the cookie, Beyond Canvas will find a browser compatible locale and set it to the cookie. If a locale is set in the cookie, Beyond Canvas will display the app in the respective locale. If the app is not available in the required locale, the default locale configured for your app will be used.

Customization

Let's have a look at an example to explain the customization options you have in terms of the locale management: Beyond Canvas will display en and de as values for the <select> input options if your app supports multiple locales like this:

# config/application.rb

module YourAppName
  class Application < Rails::Application
    ...
    config.i18n.load_path += Dir[Rails.root.join('config/locales/**/*.{rb,yml}')]
    config.i18n.available_locales = [:en, :de]
    config.i18n.default_locale = :en
    ...
  end
end

If you'd like to display custom values for the locales, add the respective translation values to your locale files as follows:

# config/locales/de.yml

de:
  locales:
    de: Deutsch
    en: Englisch
# config/locales/en.yml

en:
  locales:
    de: German
    en: English

The above mentioned configuration will then generate the following html code:

<form action="/locale" accept-charset="UTF-8" method="post">
  <input type="hidden" name="_method" value="put">

  <select class="select--locale" onchange="this.form.submit()" name="system[locale]" id="system_locale">
    <option selected="selected" value="en">English</option>
    <option value="de">German</option>
  </select>
</form>