Sha256: 7f2f1bc3f0beb40aea2afdb991e29d923ad6183defac00256fc20d65b3b93601

Contents?: true

Size: 1.78 KB

Versions: 27

Compression:

Stored size: 1.78 KB

Contents

require 'fast_gettext/translation_repository/base'
require 'yaml'

module FastGettext
  module TranslationRepository
    # Responsibility:
    #  - find and store yaml files
    #  - provide access to translations in yaml files
    class Yaml < Base
      def initialize(name,options={})
        find_and_store_files(options)
        super
      end

      def available_locales
        @files.keys
      end

      def plural(*keys)
        ['one', 'other', 'plural2', 'plural3'].map do |name|
          self[yaml_dot_notation(keys.first, name)]
        end
      end

      def pluralisation_rule
        self['pluralisation_rule'] ? lambda{|n| eval(self['pluralisation_rule']) } : nil
      end

      protected

      MAX_FIND_DEPTH = 10

      def find_and_store_files(options)
        @files = {}
        path = options[:path] || 'config/locales'
        Dir["#{path}/??.yml"].each do |yaml_file|
          locale = yaml_file.match(/([a-z]{2})\.yml$/)[1]
          @files[locale] = load_yaml(yaml_file, locale)
        end
      end

      def current_translations
        @files[FastGettext.locale] || super
      end

      # Given a yaml file return a hash of key -> translation
      def load_yaml(file, locale)
        yaml = YAML.load_file(file)
        yaml_hash_to_dot_notation(yaml[locale])
      end

      def yaml_hash_to_dot_notation(yaml_hash)
        add_yaml_key({}, nil, yaml_hash)
      end

      def add_yaml_key(result, prefix, hash)
        hash.each_pair do |key, value|
          if value.kind_of?(Hash)
            add_yaml_key(result, yaml_dot_notation(prefix, key), value)
          else
            result[yaml_dot_notation(prefix, key)] = value
          end
        end
        result
      end

      def yaml_dot_notation(a,b)
        a ? "#{a}.#{b}" : b
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
fast_gettext-0.7.0 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.12 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.11 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.10 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.9 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.8 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.7 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.6 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.5 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.4 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.3 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.2 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.1 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.6.0 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.5.13 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.5.12 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.5.11 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.5.10 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.5.9 lib/fast_gettext/translation_repository/yaml.rb
fast_gettext-0.5.8 lib/fast_gettext/translation_repository/yaml.rb