class FastGettext::MoFile

Responsibility:

- abstract mo files for Mo Repository

Constants

CONTEXT_SEPARATOR
PLURAL_SEPARATOR

Public Class Methods

empty() click to toggle source
# File lib/fast_gettext/mo_file.rb, line 45
def self.empty
  MoFile.new(File.join(File.dirname(__FILE__), 'vendor', 'empty.mo'))
end
new(file, options = {}) click to toggle source

file => path or FastGettext::GetText::MOFile

# File lib/fast_gettext/mo_file.rb, line 11
def initialize(file, options = {})
  @filename = file
  @data = nil
  load_data if options[:eager_load]
end

Public Instance Methods

[](key) click to toggle source
# File lib/fast_gettext/mo_file.rb, line 17
def [](key)
  data[key]
end
data() click to toggle source
# File lib/fast_gettext/mo_file.rb, line 40
def data
  load_data if @data.nil?
  @data
end
plural(*msgids) click to toggle source

returns the plural forms or all singular translations that where found Car, Cars => [Auto,Autos] or []

# File lib/fast_gettext/mo_file.rb, line 23
def plural(*msgids)
  split_plurals(self[msgids * PLURAL_SEPARATOR].to_s)
end
pluralisation_rule() click to toggle source
# File lib/fast_gettext/mo_file.rb, line 27
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|
    if /^Plural-Forms:\s*nplurals\s*\=\s*(\d*);\s*plural\s*\=\s*([^;]*)\n?/ =~ line
      return ->(n) do # rubocop:disable Lint/UnusedBlockArgument
        eval($2) # rubocop:disable Security/Eval
      end
    end
  end
  nil
end

Private Instance Methods

load_data() click to toggle source
# File lib/fast_gettext/mo_file.rb, line 51
def load_data
  @data =
    if @filename.is_a? FastGettext::GetText::MOFile
      @filename
    else
      FastGettext::GetText::MOFile.open(@filename, "UTF-8")
    end
  make_singular_and_plural_available
end
make_singular_and_plural_available() click to toggle source

(if plural==singular, prefer singular)

# File lib/fast_gettext/mo_file.rb, line 62
def make_singular_and_plural_available
  data = {}
  @data.each do |key, translation|
    next unless key.include? PLURAL_SEPARATOR

    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
split_plurals(singular_plural) click to toggle source
# File lib/fast_gettext/mo_file.rb, line 75
def split_plurals(singular_plural)
  singular_plural.split(PLURAL_SEPARATOR)
end