Class: GettextSimple
- Inherits:
-
Object
- Object
- GettextSimple
- Defined in:
- lib/gettext_simple.rb
Overview
This class reads .po-files generated by something like POEdit and can be used to run multi-language applications or websites.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ GettextSimple
constructor
Initializes various data.
-
#load_dir(dir) ⇒ Object
Loads a ‘locales’-directory with .mo- and .po-files and fills the ‘@locales’-hash.
- #locale_for_thread=(newlocale) ⇒ Object
-
#locales ⇒ Object
Returns an array of available locales.
- #register_kernel_methods ⇒ Object
- #translate(str, replaces = nil) ⇒ Object
-
#translate_with_locale(locale, str, replaces = nil) ⇒ Object
Translates a given string to a given locale from the read .po-files.
Constructor Details
#initialize(args = {}) ⇒ GettextSimple
Initializes various data.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gettext_simple.rb', line 4 def initialize(args = {}) @args = { :encoding => "utf-8", :i18n => false, :default_locale => "en" }.merge(args) @locales = {} @debug = @args[:debug] # To avoid doing a hash lookup on every translate. @i18n = @args[:i18n] if @i18n @default_locale = I18n.default_locale else @default_locale = @args[:default_locale] end @locale = @default_locale end |
Instance Method Details
#load_dir(dir) ⇒ Object
Loads a ‘locales’-directory with .mo- and .po-files and fills the ‘@locales’-hash.
Examples
gtext.load_dir(“#File.dirname(__FILE__)/../locales”)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gettext_simple.rb', line 37 def load_dir(dir) check_folders = ["LC_MESSAGES", "LC_ALL"] Dir.foreach(dir) do |file| debug "New locale folder found: '#{file}'." if @debug fn = "#{dir}/#{file}" if !File.directory?(fn) || !file.match(/^[a-z]{2}/) debug "Skipping: '#{file}'." if @debug next end @locales[file] = {} unless @locales[file] check_folders.each do |fname| fpath = "#{dir}/#{file}/#{fname}" next if !File.exists?(fpath) || !File.directory?(fpath) debug "Found subfolder in '#{file}': '#{fname}'." if @debug Dir.foreach(fpath) do |pofile| if pofile.match(/\.po$/) debug "Starting to parse '#{pofile}'." if @debug pofn = "#{dir}/#{file}/#{fname}/#{pofile}" scan_pofile(file, pofn) end end end end end |
#locale_for_thread=(newlocale) ⇒ Object
30 31 32 |
# File 'lib/gettext_simple.rb', line 30 def locale_for_thread=(newlocale) Thread.current[:gettext_simple_locale] = newlocale end |
#locales ⇒ Object
Returns an array of available locales.
106 107 108 |
# File 'lib/gettext_simple.rb', line 106 def locales return @locales.keys end |
#register_kernel_methods ⇒ Object
25 26 27 28 |
# File 'lib/gettext_simple.rb', line 25 def register_kernel_methods require "#{__dir__}/../include/kernel_methods" $gettext_simple_kernel_instance = self end |
#translate(str, replaces = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/gettext_simple.rb', line 93 def translate(str, replaces = nil) if @i18n locale = I18n.locale.to_s elsif locale = Thread.current[:gettext_simple_locale] # Locale already set through condition. else locale = @default_locale end translate_with_locale(locale, str, replaces) end |
#translate_with_locale(locale, str, replaces = nil) ⇒ Object
Translates a given string to a given locale from the read .po-files.
Examples
str = “Hello” #=> “Hello” gtext.trans(“da_DK”, str) #=> “Hej”
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gettext_simple.rb', line 71 def translate_with_locale(locale, str, replaces = nil) locale = locale.to_s str = str.to_s raise "Locale was not found: '#{locale}' in '#{@locales.keys.join(", ")}'." unless @locales.key?(locale) if !@locales[locale].key?(str) translated_str = str.to_s else translated_str = @locales[locale][str] end if replaces replaces.each do |key, val| translated_str = translated_str.gsub("%{#{key}}", val.to_s) end end debug "Translation with locale '#{locale}' and replaces '#{replaces}': '#{str}' to '#{translated_str}'." if @debug return translated_str end |