Class: Vocab::Translator::Rails
- Defined in:
- lib/vocab/translator/rails.rb
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
Class Method Summary collapse
Instance Method Summary collapse
- #available_locales ⇒ Object
- #fetch(key) ⇒ Object
- #flattened_translations(options = {}) ⇒ Object
-
#initialize(locale = :en) ⇒ Rails
constructor
A new instance of Rails.
- #load_dir(dir) ⇒ Object
- #load_file(file) ⇒ Object
- #store(key, value) ⇒ Object
- #translations(options = {}) ⇒ Object
- #write_file(file) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(locale = :en) ⇒ Rails
Returns a new instance of Rails.
12 13 14 15 |
# File 'lib/vocab/translator/rails.rb', line 12 def initialize( locale = :en ) @backend = I18n::Backend::Simple.new @locale = locale end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
10 11 12 |
# File 'lib/vocab/translator/rails.rb', line 10 def locale @locale end |
Class Method Details
.en_equivalent_path(path) ⇒ Object
62 63 64 |
# File 'lib/vocab/translator/rails.rb', line 62 def self.en_equivalent_path( path ) return "#{File.dirname( path )}/en.yml" end |
Instance Method Details
#available_locales ⇒ Object
17 18 19 |
# File 'lib/vocab/translator/rails.rb', line 17 def available_locales return @backend.available_locales end |
#fetch(key) ⇒ Object
58 59 60 |
# File 'lib/vocab/translator/rails.rb', line 58 def fetch( key ) return flattened_translations[ key.to_sym ] end |
#flattened_translations(options = {}) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/vocab/translator/rails.rb', line 43 def flattened_translations( = {} ) return @backend.flatten_translations( @locale, translations( ), true, false ) rescue puts "Error fetching locale '#{@locale.inspect}' from keys #{@backend.send(:translations).keys}" # TODO put logic to detect BOM here to give extra clear error message or clean the file raise end |
#load_dir(dir) ⇒ Object
21 22 23 24 |
# File 'lib/vocab/translator/rails.rb', line 21 def load_dir( dir ) I18n.load_path = Dir.glob( "#{dir}/**/*.{yml,rb}" ) load_translations end |
#load_file(file) ⇒ Object
26 27 28 29 30 |
# File 'lib/vocab/translator/rails.rb', line 26 def load_file( file ) @locale = File.basename( file ).gsub( /\..*$/, '' ).to_sym I18n.load_path = [ file ] load_translations end |
#store(key, value) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/vocab/translator/rails.rb', line 51 def store( key, value ) keys = I18n.normalize_keys( @locale, key, [], nil) keys.shift # remove locale data = keys.reverse.inject( value ) { |result, _key| { _key => result } } @backend.store_translations( @locale, data ) end |
#translations(options = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/vocab/translator/rails.rb', line 37 def translations( = {} ) t = @backend.send(:translations) trans = t[ @locale ] return [ :prefix ] == true ? { @locale => trans } : trans end |
#write_file(file) ⇒ Object
32 33 34 35 |
# File 'lib/vocab/translator/rails.rb', line 32 def write_file( file ) yaml = { @locale => self.translations }.to_yaml File.open( file, 'w+' ) { |f| f.write( yaml ) } end |