Sha256: fe5542a1ac723436da900d433471f90be08f2095da579c8ab61afb5e40b87003

Contents?: true

Size: 1.72 KB

Versions: 21

Compression:

Stored size: 1.72 KB

Contents

require 'fast_gettext/vendor/mofile'
module FastGettext
  # Responsibility:
  #  - abstract mo files for Mo Repository
  class MoFile
    PLURAL_SEPERATOR = "\000"

    # file => path or FastGettext::GetText::MOFile
    def initialize(file)
      if file.is_a? FastGettext::GetText::MOFile
        @data = file
      else
        @data = FastGettext::GetText::MOFile.open(file, "UTF-8")
      end
      make_singular_and_plural_available
    end

    def [](key)
      @data[key]
    end

    #returns the plural forms or all singular translations that where found
    # Car, Cars => [Auto,Autos] or []
    def plural(*msgids)
      split_plurals(self[msgids*PLURAL_SEPERATOR].to_s)
    end

    def pluralisation_rule
      #gettext uses 0 as default rule, which would turn off all pluralisation, very clever...
      #additionally parsing fails when directly accessing po files, so this line was taken from gettext/mofile
      (@data['']||'').split("\n").each do |line|
        return lambda{|n|eval($2)} if /^Plural-Forms:\s*nplurals\s*\=\s*(\d*);\s*plural\s*\=\s*([^;]*)\n?/ =~ line
      end
      nil
    end

    def self.empty
      MoFile.new(File.join(File.dirname(__FILE__),'vendor','empty.mo'))
    end

    private

    #(if plural==singular, prefer singular)
    def make_singular_and_plural_available
      data = {}
      @data.each do |key,translation|
        next unless key.include? PLURAL_SEPERATOR
        singular, plural = split_plurals(key)
        translation = split_plurals(translation)
        data[singular] ||= translation[0]
        data[plural] ||= translation[1]
      end
      @data.merge!(data){|key,old,new| old}
    end

    def split_plurals(singular_plural)
      singular_plural.split(PLURAL_SEPERATOR)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
fast_gettext-1.0.0 lib/fast_gettext/mo_file.rb
fast_gettext-0.9.2 lib/fast_gettext/mo_file.rb
fast_gettext-0.9.1 lib/fast_gettext/mo_file.rb
fast_gettext-0.9.0 lib/fast_gettext/mo_file.rb
fast_gettext-0.8.1 lib/fast_gettext/mo_file.rb
fast_gettext-0.8.0 lib/fast_gettext/mo_file.rb
fast_gettext-0.7.1 lib/fast_gettext/mo_file.rb
fast_gettext-0.7.0 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.12 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.11 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.10 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.9 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.8 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.7 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.6 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.5 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.4 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.3 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.2 lib/fast_gettext/mo_file.rb
fast_gettext-0.6.1 lib/fast_gettext/mo_file.rb