module I18n::Backend::Gettext

Experimental support for using Gettext po files to store translations.

To use this you can simply include the module to the Simple backend - or whatever other backend you are using.

I18n::Backend::Simple.include(I18n::Backend::Gettext)

Now you should be able to include your Gettext translation (*.po) files to the I18n.load_path so they’re loaded to the backend and you can use them as usual:

I18n.load_path += Dir["path/to/locales/*.po"]

Following the Gettext convention this implementation expects that your translation files are named by their locales. E.g. the file en.po would contain the translations for the English locale.

To translate text you must use one of the translate methods provided by I18n::Gettext::Helpers.

include I18n::Gettext::Helpers
puts _("some string")

Without it strings containing periods (“.”) will not be translated.

Protected Instance Methods

# File lib/i18n/backend/gettext.rb, line 41
def load_po(filename)
  locale = ::File.basename(filename, '.po').to_sym
  data = normalize(locale, parse(filename))
  [{ locale => data }, false]
end
# File lib/i18n/backend/gettext.rb, line 51
def normalize(locale, data)
  data.inject({}) do |result, (key, value)|
    unless key.nil? || key.empty?
      key = key.gsub(I18n::Gettext::CONTEXT_SEPARATOR, '|')
      key, value = normalize_pluralization(locale, key, value) if key.index("\000")

      parts = key.split('|').reverse
      normalized = parts.inject({}) do |_normalized, part|
        { part => _normalized.empty? ? value : _normalized }
      end

      Utils.deep_merge!(result, normalized)
    end
    result
  end
end
# File lib/i18n/backend/gettext.rb, line 68
def normalize_pluralization(locale, key, value)
  # FIXME po_parser includes \000 chars that can not be turned into Symbols
  key = key.gsub("\000", I18n::Gettext::PLURAL_SEPARATOR).split(I18n::Gettext::PLURAL_SEPARATOR).first

  keys = I18n::Gettext.plural_keys(locale)
  values = value.split("\000")
  raise "invalid number of plurals: #{values.size}, keys: #{keys.inspect} on #{locale} locale for msgid #{key.inspect} with values #{values.inspect}" if values.size != keys.size

  result = {}
  values.each_with_index { |_value, ix| result[keys[ix]] = _value }
  [key, result]
end
# File lib/i18n/backend/gettext.rb, line 47
def parse(filename)
  GetText::PoParser.new.parse(::File.read(filename), PoData.new)
end