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).

Usage

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

Instance Method Summary collapse

Instance Method Details

#html_dirObject

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

Usage

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


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

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

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

#html_langObject

Lang

Helper for Views to return the identified language.

Usage

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


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

def html_lang
  I18n.locale.to_s
end