Module: GettextSetup
- Defined in:
- lib/gettext-setup/gettext_setup.rb
Constant Summary collapse
- @@config =
nil
Class Method Summary collapse
- .config ⇒ Object
- .default_locale ⇒ Object
-
.initialize(locales_path) ⇒ Object
locales_pathshould include: - config.yaml - a .pot file for the project - i18n directories for languages, each with a .po file. - .locales ⇒ Object
- .locales_path ⇒ Object
-
.negotiate_locale(accept_header) ⇒ Object
Given an HTTP Accept-Language header return the locale with the highest priority from it for which we have a locale available.
Class Method Details
.config ⇒ Object
42 43 44 |
# File 'lib/gettext-setup/gettext_setup.rb', line 42 def self.config @@config end |
.default_locale ⇒ Object
46 47 48 |
# File 'lib/gettext-setup/gettext_setup.rb', line 46 def self.default_locale config['default_locale'] || "en" end |
.initialize(locales_path) ⇒ Object
locales_path should include:
- config.yaml
- a .pot file for the project
- i18n directories for languages, each with a .po file
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/gettext-setup/gettext_setup.rb', line 12 def self.initialize(locales_path) config_path = File.absolute_path('config.yaml', locales_path) @@config = YAML.load_file(config_path)['gettext'] @@locales_path = locales_path # Make the translation methods available everywhere Object.send(:include, FastGettext::Translation) # Define our text domain, and set the path into our root. I would prefer to # have something smarter, but we really want this up earlier even than our # config loading happens so that errors there can be translated. # # We use the PO files directly, since it works about as efficiently with # fast_gettext, and avoids all the extra overhead of compilation down to # machine format, etc. FastGettext.add_text_domain(config['project_name'], :path => locales_path, :type => :po, :ignore_fuzzy => false) FastGettext.default_text_domain = config['project_name'] # Likewise, be explicit in our default language choice. FastGettext.default_locale = default_locale FastGettext.default_available_locales = locales end |
.locales ⇒ Object
50 51 52 53 54 55 |
# File 'lib/gettext-setup/gettext_setup.rb', line 50 def self.locales explicit = Dir.glob(File::absolute_path('*/*.po', locales_path)).map do |x| File::basename(File::dirname(x)) end (explicit + [ default_locale]).uniq end |
.locales_path ⇒ Object
38 39 40 |
# File 'lib/gettext-setup/gettext_setup.rb', line 38 def self.locales_path @@locales_path end |
.negotiate_locale(accept_header) ⇒ Object
Given an HTTP Accept-Language header return the locale with the highest priority from it for which we have a locale available. If none exists, return the default locale
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gettext-setup/gettext_setup.rb', line 60 def self.negotiate_locale(accept_header) unless @@config raise ArgumentError, "No config.yaml found! Use `GettextSetup.initialize(locales_path)` to locate your config.yaml" end return FastGettext.default_locale if accept_header.nil? available_locales = accept_header.split(",").map do |locale| pair = locale.strip.split(';q=') pair << '1.0' unless pair.size == 2 pair[0] = FastGettext.default_locale if pair[0] == '*' pair end.sort_by do |(locale,qvalue)| qvalue.to_f end.select do |(locale,_)| FastGettext.available_locales.include?(locale) end if available_locales and available_locales.last available_locales.last.first else # We can't satisfy the request preference. Just use the default locale. default_locale end end |