2 require 'i18n/gettext/po_parser'
4 # Experimental support for using Gettext po files to store translations.
6 # To use this you can simply include the module to the Simple backend - or
7 # whatever other backend you are using.
9 # I18n::Backend::Simple.include(I18n::Backend::Gettext)
11 # Now you should be able to include your Gettext translation (*.po) files to
12 # the I18n.load_path so they're loaded to the backend and you can use them as
15 # I18n.load_path += Dir["path/to/locales/*.po"]
17 # Following the Gettext convention this implementation expects that your
18 # translation files are named by their locales. E.g. the file en.po would
19 # contain the translations for the English locale.
24 def set_comment(msgid_or_sym, comment)
31 locale = ::File.basename(filename, '.po').to_sym
32 data = normalize(locale, parse(filename))
37 GetText::PoParser.new.parse(::File.read(filename), PoData.new)
40 def normalize(locale, data)
41 data.inject({}) do |result, (key, value)|
42 unless key.nil? || key.empty?
43 key = key.gsub(I18n::Gettext::CONTEXT_SEPARATOR, '|')
44 key, value = normalize_pluralization(locale, key, value) if key.index("\000")
46 parts = key.split('|').reverse
47 normalized = parts.inject({}) do |_normalized, part|
48 { part => _normalized.empty? ? value : _normalized }
51 result.deep_merge!(normalized)
57 def normalize_pluralization(locale, key, value)
58 # FIXME po_parser includes \000 chars that can not be turned into Symbols
59 key = key.gsub("\000", I18n::Gettext::PLURAL_SEPARATOR).split(I18n::Gettext::PLURAL_SEPARATOR).first
61 keys = I18n::Gettext.plural_keys(locale)
62 values = value.split("\000")
63 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
66 values.each_with_index { |_value, ix| result[keys[ix]] = _value }