Sha256: 6248ad88b6527bff66ffa53f529c61acf8ecaf2e5bef8358d575659f8b0ca100

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

module Olelo
  # Simple localization implementation
  module Locale
    @locale = nil
    @translations = Hash.with_indifferent_access
    @loaded = []

    class << self
      attr_accessor :locale

      # Load locale from file
      #
      # A locale is a yamlfile which maps
      # keys to strings.
      #
      # @param [String] file name
      # @return [void]
      #
      def load(file)
        if !@loaded.include?(file) && File.file?(file)
          locale = YAML.load_file(file)
          @translations.update(locale[$1] || {}) if @locale =~ /^(\w+)(_|-)/
          @translations.update(locale[@locale] || {})
          @translations.each_value(&:freeze)
          @loaded << file
        end
      end

      # Return translated string for key
      #
      # A translated string can contain variables which are substituted in this method.
      # You have to pass an arguments hash.
      #
      # @option args [Integer] :count    if count is not 1, the key #{key}_plural is looked up instead
      # @option args [String]  :fallback Fallback string if key is not found in the locale
      # @param [Symbol, String] key which identifies string in locale
      # @param [Hash] args Arguments hash for string interpolation
      # @return [String] translated string
      #
      def translate(key, args = {})
        if !key.to_s.ends_with?('_plural') && args[:count] && args[:count] != 1
          translate("#{key}_plural", args)
        elsif @translations[key]
          args.inject(@translations[key]) {|s,(k,v)| s.gsub("#\{#{k}\}", v.to_s) }
        else
          args[:fallback] || "##{key}"
        end
      end
    end
  end
end

class Symbol
  # Lookup translated string identified by this symbol
  #
  # @param [Hash] args Arguments hash for string interpolation
  # @return [String] translated string
  # @see Olelo::Locale#translate
  #
  def t(args = {})
    Olelo::Locale.translate(self, args)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
olelo-0.9.4 lib/olelo/locale.rb
olelo-0.9.3 lib/olelo/locale.rb
olelo-0.9.2 lib/olelo/locale.rb
olelo-0.9.1 lib/olelo/locale.rb
olelo-0.9.0 lib/olelo/locale.rb