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 |
# File 'lib/gettext_simple.rb', line 4 def initialize(args = {}) @args = { :encoding => "utf-8", :i18n => false, :default_locale => "en" }.merge(args) @locales = {} # 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”)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gettext_simple.rb', line 36 def load_dir(dir) check_folders = ["LC_MESSAGES", "LC_ALL"] Dir.foreach(dir) do |file| fn = "#{dir}/#{file}" if File.directory?(fn) && file.match(/^[a-z]{2}/) @locales[file] = {} unless @locales[file] check_folders.each do |fname| fpath = "#{dir}/#{file}/#{fname}" if File.exists?(fpath) && File.directory?(fpath) Dir.foreach(fpath) do |pofile| if pofile.match(/\.po$/) pofn = "#{dir}/#{file}/#{fname}/#{pofile}" scan_pofile(file, pofn) end end end end end end end |
#locale_for_thread=(newlocale) ⇒ Object
29 30 31 |
# File 'lib/gettext_simple.rb', line 29 def locale_for_thread=(newlocale) Thread.current[:gettext_simple_locale] = newlocale end |
#locales ⇒ Object
Returns an array of available locales.
97 98 99 |
# File 'lib/gettext_simple.rb', line 97 def locales return @locales.keys end |
#register_kernel_methods ⇒ Object
24 25 26 27 |
# File 'lib/gettext_simple.rb', line 24 def register_kernel_methods require "#{__dir__}/../include/kernel_methods" $gettext_simple_kernel_instance = self end |
#translate(str, replaces = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/gettext_simple.rb', line 84 def translate(str, replaces = nil) if @i18n locale = I18n.locale 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”
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gettext_simple.rb', line 64 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 else translated_str = @locales[locale][str] end if replaces replaces.each do |key, val| translated_str = translated_str.gsub("%{#{key}}", val) end end return translated_str end |