Module: Rubi18n::Mixin
- Defined in:
- lib/rubi18n/mixin.rb
Overview
Mixin for application to add i18n support. It just a wrap for Translation and Locale classes but has more pretty API for i18n.
To use it you must set before translations_dir
and user locales.
Usage
translations/en.yml
hello: Hello, %1
application.rb
require "rubi18n"
class Application
include Rubi18n::Mixin
def initialize
translations_dir File.dirname(__FILE__) + "/translations/"
set_locales ENV["LANG"]
puts i18n.hello(ENV["USER"])
end
end
Instance Method Summary collapse
-
#i18n ⇒ Object
Return an application Translation.
-
#locale ⇒ Object
Return the first available user Locale (or Locale for default_locale).
-
#locales ⇒ Object
Return user locales.
-
#parse_http_accept_language(locales) ⇒ Object
Parse HTTP_ACCEPT_LANGUAGE and return array of users locales.
-
#set_locales(locales) ⇒ Object
Set user locales to load translation.
-
#translations ⇒ Object
Return all available translations (all file in
translations_dir
). -
#translations_dir(dir) ⇒ Object
Set dir with application translations.
Instance Method Details
#i18n ⇒ Object
Return an application Translation.
87 88 89 |
# File 'lib/rubi18n/mixin.rb', line 87 def i18n @translation end |
#locale ⇒ Object
Return the first available user Locale (or Locale for default_locale)
82 83 84 |
# File 'lib/rubi18n/mixin.rb', line 82 def locale Locale.current end |
#locales ⇒ Object
Return user locales
77 78 79 |
# File 'lib/rubi18n/mixin.rb', line 77 def locales @locales end |
#parse_http_accept_language(locales) ⇒ Object
Parse HTTP_ACCEPT_LANGUAGE and return array of users locales
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rubi18n/mixin.rb', line 92 def parse_http_accept_language(locales) return locales if locales.nil? locales = locales.split(",") locales.map! do |locale| locale = locale.split ";q=" if 1 == locale.size [locale[0], 1.0] else [locale[0], locale[1].to_f] end end locales.sort! { |a, b| b[1] <=> a[1] } locales.map! { |i| i[0] } end |
#set_locales(locales) ⇒ Object
Set user locales to load translation. You must set a translations_dir
before. Use Translation.default to set default locale.
69 70 71 72 73 74 |
# File 'lib/rubi18n/mixin.rb', line 69 def set_locales(locales) locales = locales.to_a if Array != locales.class @locales = locales @translation = Translation.load(locales, @translations_dir) Locale.current = Locale.find(locales + [Translation.default]) end |
#translations ⇒ Object
Return all available translations (all file in translations_dir
). It will be a hash with locale code as kay and locale title (in it language) as value.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubi18n/mixin.rb', line 56 def translations Translation.available(@translations_dir).inject({}) do |all, code| all[code] = if Locale.exists? code Locale.new(code)["title"] else nil end all end end |
#translations_dir(dir) ⇒ Object
Set dir with application translations. It must contain YAML files for each available locale.
49 50 51 |
# File 'lib/rubi18n/mixin.rb', line 49 def translations_dir(dir) @translations_dir = dir end |