Sha256: 697ff0149d1901d826c4867f28c77d1cae62c1ef39827f48741437eb7fbf6191

Contents?: true

Size: 1.97 KB

Versions: 10

Compression:

Stored size: 1.97 KB

Contents

require File.join(File.dirname(__FILE__),'..','..','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 singlgular translations that where found
    def plural(*msgids)
      translations = plural_translations(msgids)
      return translations unless translations.empty?
      msgids.map{|msgid| self[msgid] || msgid} #try to translate each id
    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.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
    end

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

    # Car, Cars => [Auto,Autos] or []
    def plural_translations(msgids)
      plurals = self[msgids*PLURAL_SEPERATOR]
      if plurals then split_plurals(plurals) else [] end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
grosser-fast_gettext-0.3.0 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.1 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.2 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.3 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.4 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.5 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.6 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.7 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.3.8 lib/fast_gettext/mo_file.rb
grosser-fast_gettext-0.4.0 lib/fast_gettext/mo_file.rb