Class: Merb::Global::MessageProviders::Gettext
- Inherits:
-
Object
- Object
- Merb::Global::MessageProviders::Gettext
- Includes:
- Base, Base::Exporter, Base::Importer
- Defined in:
- lib/merb_global/message_providers/gettext.rb
Overview
:nodoc: all
Defined Under Namespace
Classes: GettextContext
Instance Method Summary collapse
- #create! ⇒ Object
- #export(data) ⇒ Object
- #import ⇒ Object
- #localize(singular, plural, n, locale) ⇒ Object
Methods included from Base
Instance Method Details
#create! ⇒ Object
29 30 31 |
# File 'lib/merb_global/message_providers/gettext.rb', line 29 def create! File.mkdirs Merb::Global::MessageProviders.localedir end |
#export(data) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/merb_global/message_providers/gettext.rb', line 74 def export(data) data.each do |lang_name, lang| lang_file = File.join(Merb::Global::MessageProviders.localedir, lang_name + '.po') open(lang_file, 'w') do |po| po.puts <<EOF msgid "" msgstr "" "Project-Id-Version: 0.0.1\\n" "POT-Creation-Date: #{Time.now.strftime('%Y-%m-%d %H:%M%z')}\\n" "PO-Revision-Date: #{Time.now.strftime('%Y-%m-%d %H:%M%z')}\\n" "Last-Translator: <[email protected]>\\n" "Language-Team: Language type\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=UTF-8\\n" "Content-Transfer-Encoding: 8bit\\n" "Plural-Forms: nplurals=#{lang[:nplurals]}; plural=#{lang[:plural]}\\n" EOF lang.each do |msgid, msgstr_hash| po.puts "" po.puts "msgid \"#{msgid}\"" if msgstr_hash[:plural] po.puts "msgid_plural \"#{msgstr_hash[:plural]}\"" msgstr_hash.each do |msgstr_index, msgstr| po.puts "msgstr[#{msgstr_index}] \"#{msgstr}\"" end else po.puts "msgstr \"#{msgstr_hash[nil]}\"" end end end lang_dir = File.join(Merb::Global::MessageProviders.localedir, lang, 'LC_MESSAGES') FileUtils.mkdir_p lang_dir domain = Merb::Global.config([:gettext, :domain], 'merbapp') `msgfmt #{lang_file} -o #{lang_dir}/#{domain}.mo` end end |
#import ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/merb_global/message_providers/gettext.rb', line 33 def import Treetop.load(Pathname(__FILE__).dirname..to_s + '/gettext') parser = Merb::Global::MessageProviders::GetTextParser.new data = {} Dir[Merb::Global::MessageProviders.localedir + '/*.po'].each do |file| lang_name = File.basename file, '.po' lang_tree = nil open file do |f| lang_tree = parser.parse f.read end # Put the parsed file in data data[lang_name] = lang_tree.to_hash # Remove the metadata to futher managing opts = data[lang_name].delete('')[nil].split("\n") # Find the line about plural line plural_line = nil for l in opts if l[0..."Plural-Forms:".length] == "Plural-Forms:" plural_line = l break end end # Remove the "Plural-Forms:" from the beginning... plural_line = plural_line["Plural-Forms:".length...plural_line.length] # and ; from end plural_line = plural_line[0...plural_line.length-1] # Split the line and build the hash plural_line = plural_line.gsub(/[[:space:]]/, '').split(/[=;]/, 4) # And change the plural and nplurals into :plural and :nplurals plural_line[2] = :plural plural_line[0] = :nplural # Adn the nplural value into integer plural_line[1] = plural_line[1].to_i data[lang_name].merge! Hash[*plural_line] end data end |
#localize(singular, plural, n, locale) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/merb_global/message_providers/gettext.rb', line 19 def localize(singular, plural, n, locale) context = locale._mg_gettext context.set_locale locale.to_s, true unless plural.nil? context.ngettext singular, plural, n else context.gettext singular end end |