Module: GettextSetup
- Defined in:
- lib/gettext-setup/gettext_setup.rb
Constant Summary collapse
- @@config =
nil- @@translation_repositories =
{}
Class Method Summary collapse
- .add_repository_to_chain(project_name, options) ⇒ Object
-
.candidate_locales ⇒ Object
Returns the locale for the current machine.
- .clear ⇒ Object
- .config ⇒ Object
- .default_locale ⇒ Object
-
.initialize(locales_path, options = {}) ⇒ Object
locales_pathshould include: - config.yaml - a .pot file for the project - i18n directories for languages, each with a .po file - if using .mo files, an LC_MESSAGES dir in each language dir, with the .mo file in it validoptionsfields: :file_format - one of the supported backends for fast_gettext (e.g. :po, :mo, :yaml, etc.). - .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.
- .set_default_locale(new_locale) ⇒ Object
- .translation_repositories ⇒ Object
Class Method Details
.add_repository_to_chain(project_name, options) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/gettext-setup/gettext_setup.rb', line 43 def self.add_repository_to_chain(project_name,) repository = FastGettext::TranslationRepository.build(project_name, :path => locales_path, :type => [:file_format] || :po, :ignore_fuzzy => false) @@translation_repositories[project_name] = repository unless @@translation_repositories.key? project_name end |
.candidate_locales ⇒ Object
Returns the locale for the current machine. This is most useful for shell applications that need an ACCEPT-LANGUAGE header set.
75 76 77 |
# File 'lib/gettext-setup/gettext_setup.rb', line 75 def self.candidate_locales Locale.candidates(type: :cldr).join(',') end |
.clear ⇒ Object
79 80 81 |
# File 'lib/gettext-setup/gettext_setup.rb', line 79 def self.clear Locale.clear end |
.config ⇒ Object
55 56 57 |
# File 'lib/gettext-setup/gettext_setup.rb', line 55 def self.config @@config ||= {} end |
.default_locale ⇒ Object
63 64 65 |
# File 'lib/gettext-setup/gettext_setup.rb', line 63 def self.default_locale config['default_locale'] || "en" end |
.initialize(locales_path, options = {}) ⇒ Object
locales_path should include:
- config.yaml
- a .pot file for the project
- i18n directories for languages, each with a .po file
- if using .mo files, an LC_MESSAGES dir in each language dir, with the .mo file in it
valid
optionsfields: :file_format - one of the supported backends for fast_gettext (e.g. :po, :mo, :yaml, etc.)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gettext-setup/gettext_setup.rb', line 18 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. add_repository_to_chain(config['project_name'], ) # 'chain' is the only available multi-domain type in fast_gettext 1.1.0 We should consider # investigating 'merge' once we can bump our dependency FastGettext.add_text_domain('master_domain', type: :chain, chain: @@translation_repositories.values) FastGettext.default_text_domain = 'master_domain' # Likewise, be explicit in our default language choice. FastGettext.default_locale = default_locale FastGettext.default_available_locales = FastGettext.default_available_locales | locales Locale.set_default(default_locale) end |
.locales ⇒ Object
83 84 85 86 87 88 |
# File 'lib/gettext-setup/gettext_setup.rb', line 83 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
51 52 53 |
# File 'lib/gettext-setup/gettext_setup.rb', line 51 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/gettext-setup/gettext_setup.rb', line 93 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)| -1 * qvalue.to_f end.select do |(locale,_)| FastGettext.available_locales.include?(locale) end if available_locales and available_locales.first available_locales.first.first else # We can't satisfy the request preference. Just use the default locale. default_locale end end |
.set_default_locale(new_locale) ⇒ Object
67 68 69 70 71 |
# File 'lib/gettext-setup/gettext_setup.rb', line 67 def self.set_default_locale(new_locale) FastGettext.default_locale = new_locale Locale.set_default(new_locale) config['default_locale'] = new_locale end |
.translation_repositories ⇒ Object
59 60 61 |
# File 'lib/gettext-setup/gettext_setup.rb', line 59 def self.translation_repositories @@translation_repositories end |