Module: Undercarriage::Controllers::LocaleConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/undercarriage/controllers/locale_concern.rb

Overview

Identify locale for translations

Identify locale based on HTTP_ACCEPT_LANGUAGE. Browser preferred language is passed with the request as en-US, en;q=0.9 where en-US (English with US dialect) is the preferred language with en (generic English) as an acceptable language.

When preferred language cannot be identified or no translation is available, fall back to I18n.default_locale (typically en).

Examples:

Controller

class ExamplesController < ApplicationController
  include Undercarriage::Controllers::LocaleConcern
end

Instance Method Summary collapse

Instance Method Details

#html_dirString

Text direction

Helper for Views to return text direction based on locale. Display text left-to-right for all languages but a few languages which should display as right-to-left. Returns rtl for the following languages:

  • Arabic
  • Aramaic
  • Azeri
  • Divehi
  • Hebrew
  • Persian/Farsi
  • Urdu

Examples:

View

# <html dir="<%= html_dir %>"> #=> <html dir="ltr">
# <html dir="<%= html_dir %>"> #=> <html dir="rtl">

Returns:

  • (String)

    direction for html tag dir attribute



62
63
64
65
66
# File 'lib/undercarriage/controllers/locale_concern.rb', line 62

def html_dir
  rtl_languages = %w[am ar az dv fa he ur]

  html_lang.start_with?(*rtl_languages) ? "rtl" : "ltr"
end

#html_langString

Lang

Helper for Views to return the identified language.

Examples:

View

# <html lang="<%= html_lang %>"> #=> '<html lang="de">'
# <html lang="<%= html_lang %>"> #=> '<html lang="de-at">'

Returns:

  • (String)

    locale for html tag lang attribute



39
40
41
# File 'lib/undercarriage/controllers/locale_concern.rb', line 39

def html_lang
  I18n.locale.to_s
end