Class: I18nService
- Includes:
- LangTools, Singleton
- Defined in:
- lib/i18nservice.rb
Constant Summary collapse
- FILE_SUFFIX =
'.po'- FILE_PATTERN =
"*#{FILE_SUFFIX}"
Constants included from LangTools
LangTools::NPLURAL, LangTools::PLURAL_FAMILIES
Instance Attribute Summary collapse
-
#application_encoding ⇒ Object
readonly
Returns the value of attribute application_encoding.
-
#po_dir ⇒ Object
Returns the value of attribute po_dir.
-
#table ⇒ Object
Returns the value of attribute table.
Attributes included from LangTools
Instance Method Summary collapse
-
#available_languages ⇒ Object
list of languages that have a catalog in the current
po_dir. -
#create_catalogs(new_msg, new_langs) ⇒ Object
for every lang.
-
#each_file ⇒ Object
access the po files.
-
#filename(lang) ⇒ Object
:nodoc:.
-
#in_po_dir ⇒ Object
execute code bloc in the PO dir.
-
#kcode ⇒ Object
Returns the value of attribute application_encoding.
- #lang=(l) ⇒ Object
-
#read_po(fn) ⇒ Object
Read and parse a PO file, convert encoding to application_encoding.
-
#try_load ⇒ Object
tries to load the PO file for the actual language (@lang).
-
#update(lang, new_msg) ⇒ Object
add messages in an existing ‘lang’ catalog that do not already exist in it.
-
#update_catalogs(new_msgs) ⇒ Object
for all availables catalogues, add messages that do not already exist in them.
-
#write_po(lang) ⇒ Object
write the actual catalog in a PO file.
-
#write_pot(t) ⇒ Object
write a PO template file (POT).
Methods included from LangTools
#country_code, #find_family, #lang_encoding, #language_code, #language_country, #nplural, #plural_family
Instance Attribute Details
#application_encoding ⇒ Object (readonly)
Returns the value of attribute application_encoding.
16 17 18 |
# File 'lib/i18nservice.rb', line 16 def application_encoding @application_encoding end |
#po_dir ⇒ Object
Returns the value of attribute po_dir.
15 16 17 |
# File 'lib/i18nservice.rb', line 15 def po_dir @po_dir end |
#table ⇒ Object
Returns the value of attribute table.
14 15 16 |
# File 'lib/i18nservice.rb', line 14 def table @table end |
Instance Method Details
#available_languages ⇒ Object
list of languages that have a catalog in the current po_dir
70 71 72 73 74 75 |
# File 'lib/i18nservice.rb', line 70 def available_languages ret = in_po_dir do Dir[FILE_PATTERN] end ret.collect{|path| File.basename(path, FILE_SUFFIX)}.sort end |
#create_catalogs(new_msg, new_langs) ⇒ Object
for every lang. that does not already have a catalog, create one with the new_msg content
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/i18nservice.rb', line 84 def create_catalogs(new_msg, new_langs) # dont overwrite existing catalogs new_langs = new_langs.dup - available_languages new_langs.each{|l| @lang = l @table = Catalog.new(@lang) @table.update(new_msg) write_po(l) } end |
#each_file ⇒ Object
access the po files
63 64 65 66 67 |
# File 'lib/i18nservice.rb', line 63 def each_file #:nodoc: in_po_dir do Dir[FILE_PATTERN].each{|fn| yield fn} end end |
#filename(lang) ⇒ Object
:nodoc:
103 104 105 |
# File 'lib/i18nservice.rb', line 103 def filename(lang) #:nodoc: lang + FILE_SUFFIX end |
#in_po_dir ⇒ Object
execute code bloc in the PO dir
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/i18nservice.rb', line 50 def in_po_dir #:nodoc: wd = Dir.getwd ret = nil begin Dir.chdir(@po_dir) ret = yield ensure Dir.chdir(wd) end ret end |
#kcode ⇒ Object
Returns the value of attribute application_encoding.
17 18 19 |
# File 'lib/i18nservice.rb', line 17 def application_encoding @application_encoding end |
#lang=(l) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/i18nservice.rb', line 22 def lang=(l) unless ( @lang == l ) @lang = l load 'ri18n/plural_forms.rb' load 'ri18n/translators.rb' end end |
#read_po(fn) ⇒ Object
Read and parse a PO file, convert encoding to application_encoding
108 109 110 111 112 |
# File 'lib/i18nservice.rb', line 108 def read_po(fn) in_po_dir do PoSource.new(File.read(fn), @lang).parse(application_encoding) end end |
#try_load ⇒ Object
tries to load the PO file for the actual language (@lang)
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/i18nservice.rb', line 115 def try_load #:nodoc: loaded = true if @lang and @lang.kind_of?(String) then begin @table = read_po(filename(@lang)) rescue Errno::ENOENT loaded = false end else loaded = false end loaded end |
#update(lang, new_msg) ⇒ Object
add messages in an existing ‘lang’ catalog that do not already exist in it
97 98 99 100 101 |
# File 'lib/i18nservice.rb', line 97 def update(lang, new_msg) self.lang = lang @table.update(new_msg) write_po(lang) end |
#update_catalogs(new_msgs) ⇒ Object
for all availables catalogues, add messages that do not already exist in them
78 79 80 |
# File 'lib/i18nservice.rb', line 78 def update_catalogs(new_msgs) available_languages.each{|la| self.update(la, new_msgs) } end |
#write_po(lang) ⇒ Object
write the actual catalog in a PO file
130 131 132 133 134 135 136 137 138 |
# File 'lib/i18nservice.rb', line 130 def write_po(lang) fname = filename(lang) in_po_dir do FileUtils.cp(fname, "#{fname}.bak") if test(?f, fname) File.open(fname, File::CREAT|File::WRONLY|File::TRUNC){|f| f << @table.po_format(application_encoding) } end end |
#write_pot(t) ⇒ Object
write a PO template file (POT)
141 142 143 144 145 146 147 148 149 |
# File 'lib/i18nservice.rb', line 141 def write_pot(t)#:nodoc: fname = 'en.pot' in_po_dir do FileUtils.cp(fname, "#{fname}.bak") if test(?f, fname) File.open(fname, File::CREAT|File::WRONLY|File::TRUNC){|f| f << t.pot_format(nplural, application_encoding) } end end |