gettext 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ChangeLog CHANGED
@@ -1,3 +1,20 @@
1
+ = Ruby-GetText-Package-2.0.2 (2009-05-04)
2
+ * Support ruby-1.9.1 style format string such as %<foo>d.
3
+ * Apply new Locale.set_app_language_tags and Locale.candidates.
4
+ [Suggested by Vladimir Dobriakov]
5
+ * Enhance to support ruby-1.9.x [by OZAWA Sakuro]
6
+ * poparser work with proper encoding.
7
+ * string.rb: #bytesize alias to #size in older ruby version such as 1.8.5.
8
+ * Fixed bugs
9
+ * n_() to work when Plural-Forms line in po-file is not correct.
10
+ [Reported by Sava Chankov (Bug#25570)]
11
+ * GetText::Locale.default_rules_path : $LOAD_PATH is not well parsed.
12
+ [by hallelujah]
13
+ * locale_path.rb: Fixed warning message.
14
+
15
+ Thanks to:
16
+ hallelujah, Sava Chankov, OZAWA Sakuro, Vladimir Dobriakov
17
+
1
18
  = Ruby-GetText-Package-2.0.1 (2009-04-17)
2
19
  * Fixed bugs
3
20
  * doesn't work with ruby-1.8.5. [Reported by Dan Coutu]
data/Rakefile CHANGED
@@ -172,7 +172,7 @@ spec = Gem::Specification.new do |s|
172
172
  s.require_path = 'lib'
173
173
  s.executables = Dir.entries('bin').delete_if {|item| /^\.|CVS|~$/ =~ item }
174
174
  s.bindir = 'bin'
175
- s.add_dependency('locale', '>= 2.0.1')
175
+ s.add_dependency('locale', '>= 2.0.2')
176
176
  s.has_rdoc = true
177
177
  s.description = <<-EOF
178
178
  Ruby-GetText-Package is a GNU GetText-like program for Ruby.
@@ -222,7 +222,7 @@ Rake::RDocTask.new { |rdoc|
222
222
  }
223
223
 
224
224
  desc "Publish the release files to RubyForge."
225
- task :release => [:makemo, :package ] do
225
+ task :release => [ :package ] do
226
226
  require 'rubyforge'
227
227
 
228
228
  rubyforge = RubyForge.new
data/lib/gettext.rb CHANGED
@@ -16,7 +16,7 @@
16
16
  if respond_to? :gem
17
17
  begin
18
18
  begin
19
- gem 'locale', '>=0.9'
19
+ gem 'locale', '>=2.0'
20
20
  rescue Gem::LoadError
21
21
  end
22
22
  rescue NoMethodError
@@ -58,7 +58,6 @@ module GetText
58
58
  # * options: options as an Hash.
59
59
  # * :path - the path to the mo-files. When the value is nil, it will search default paths such as
60
60
  # /usr/share/locale, /usr/local/share/locale)
61
- # * :supported_language_tags - an Array of the supported language tags for this textdomain.
62
61
  # * :output_charset - The output charset. Same with GetText.set_output_charset. Usually, L10n
63
62
  # library doesn't use this option. Application may use this once.
64
63
  # * Returns: the GetText::TextDomainManager.
@@ -271,10 +270,18 @@ module GetText
271
270
  TextDomainManager.output_charset
272
271
  end
273
272
 
273
+ # Set the locale. This value forces the locale whole the programs.
274
+ # This method calls Locale.set_app_language_tags, Locale.default, Locale.current.
275
+ # Use Locale methods if you need to handle locales more flexible.
274
276
  def set_locale(lang)
277
+ Locale.set_app_language_tags(lang)
275
278
  Locale.default = lang
279
+ Locale.current = lang
276
280
  end
277
281
 
282
+ # Set the locale to the current thread.
283
+ # Note that if #set_locale is set, this value is ignored.
284
+ # If you need, set_locale(nil); set_current_locale(lang)
278
285
  def set_current_locale(lang)
279
286
  Locale.current = lang
280
287
  end
data/lib/gettext/cgi.rb CHANGED
@@ -16,14 +16,14 @@ Locale.init(:driver => :cgi)
16
16
 
17
17
  module GetText
18
18
 
19
- # Sets a CGI object.
19
+ # Sets a CGI object. This methods is appeared when requiring "gettext/cgi".
20
20
  # * cgi_: CGI object
21
21
  # * Returns: self
22
22
  def set_cgi(cgi_)
23
23
  Locale.set_cgi(cgi_)
24
24
  end
25
25
 
26
- # Same as GetText.set_cgi.
26
+ # Same as GetText.set_cgi. This methods is appeared when requiring "gettext/cgi".
27
27
  # * cgi_: CGI object
28
28
  # * Returns: cgi_
29
29
  def cgi=(cgi_)
@@ -31,7 +31,7 @@ module GetText
31
31
  cgi_
32
32
  end
33
33
 
34
- # Gets the CGI object. If it is nil, returns new CGI object.
34
+ # Gets the CGI object. If it is nil, returns new CGI object. This methods is appeared when requiring "gettext/cgi".
35
35
  # * Returns: the CGI object
36
36
  def cgi
37
37
  Locale.cgi
@@ -1,11 +1,14 @@
1
1
  require 'locale/util/memoizable'
2
2
 
3
3
  module GetText
4
+ # For normalize/finding the related classes/modules.
5
+ # This is used for realizing the scope of TextDomain.
6
+ # (see: http://www.yotabanana.com/hiki/ruby-gettext-scope.html)
4
7
  module ClassInfo
5
8
  extend self
6
9
  include Locale::Util::Memoizable
7
10
 
8
- # normalize the klass name
11
+ # normalize the class name
9
12
  def normalize_class(klass)
10
13
  ret = (klass.kind_of? Module) ? klass : klass.class
11
14
  if ret.name.to_s =~ /^\#<|^$/ or ret == GetText
@@ -14,12 +17,17 @@ module GetText
14
17
  ret
15
18
  end
16
19
 
20
+ def root_ancestors # :nodoc:
21
+ Object.ancestors
22
+ end
23
+ memoize :root_ancestors
24
+
17
25
  # Internal method for related_classes.
18
- def related_classes_internal(klass, all_classes = [], analyzed_classes = [] )
26
+ def related_classes_internal(klass, all_classes = [], analyzed_classes = [] )0
19
27
  ret = []
20
28
  klass = normalize_class(klass)
21
29
 
22
- return [Object] if [Object, Kernel].include? klass
30
+ return [Object] if root_ancestors.include? klass
23
31
 
24
32
  ary = klass.name.split(/::/)
25
33
  while(v = ary.shift)
@@ -31,15 +39,16 @@ module GetText
31
39
  ret.uniq!
32
40
  end
33
41
  analyzed_classes << klass unless analyzed_classes.include? klass
42
+
34
43
  klass.ancestors[1..-1].each do |v|
35
44
  ret += related_classes_internal(v, all_classes, analyzed_classes)
36
45
  ret.uniq!
37
46
  end
38
47
 
39
48
  if all_classes.size > 0
40
- ((ret - [Kernel]) & all_classes).uniq
49
+ (ret & all_classes).uniq
41
50
  else
42
- (ret - [Kernel]).uniq
51
+ ret.uniq
43
52
  end
44
53
  end
45
54
 
@@ -21,13 +21,15 @@ begin
21
21
  rescue LoadError
22
22
  # Pseudo Iconv class
23
23
  #
24
- # Provides Iconv.iconv which uses Ruby/GLib(1) functions. This library also required from 'gettext'.
25
- # If you require 'gettext/iconv', Iconv.iconv try to call Ruby/GLib function
26
- # when it doesn't find original Iconv class(iconv.so).
24
+ # ==== For Matz Ruby:
25
+ # If you don't have iconv but Ruby/GLib2, this library uses Ruby/GLib2's
26
+ # iconv functions.
27
27
  #
28
- # (1) Ruby/GLib is a module which is provided from Ruby-GNOME2 Project.
28
+ # Ruby/GLib is a module which is provided from Ruby-GNOME2 Project.
29
29
  # You can get binaries for Win32(One-Click Ruby Installer).
30
30
  # <URL: http://ruby-gnome2.sourceforge.jp/>
31
+ # ==== For JRuby:
32
+ # Use Java String class to convert strings.
31
33
  class Iconv
32
34
  module Failure; end
33
35
  class InvalidEncoding < ArgumentError; include Failure; end
@@ -1,25 +1,31 @@
1
1
  =begin
2
2
  string.rb - Extension for String.
3
3
 
4
- Copyright (C) 2005,2006 Masao Mutoh
4
+ Copyright (C) 2005-2009 Masao Mutoh
5
5
 
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
8
8
  =end
9
9
 
10
- if RUBY_VERSION < "1.9.0"
11
- # Extension for String class. This feature is included in Ruby 1.9 or later.
10
+ # Extension for String class. This feature is included in Ruby 1.9 or later but not occur TypeError.
12
11
  #
13
12
  # String#% method which accept "named argument". The translator can know
14
13
  # the meaning of the msgids using "named argument" instead of %s/%d style.
15
14
  class String
16
15
 
17
- unless respond_to? :bytesize # For older ruby (such as ruby-1.8.5)
16
+ unless instance_methods.find {|m| m.to_s == 'bytesize'}
17
+ # For older ruby (such as ruby-1.8.5)
18
18
  alias :bytesize :size
19
19
  end
20
20
 
21
21
  alias :_old_format_m :% # :nodoc:
22
-
22
+
23
+ PERCENT_MATCH_RE = Regexp.union(
24
+ /%%/,
25
+ /%\{(\w+)\}/,
26
+ /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/
27
+ )
28
+
23
29
  # call-seq:
24
30
  # %(arg)
25
31
  # %(hash)
@@ -38,16 +44,28 @@ class String
38
44
  # * hash: {:key1 => value1, :key2 => value2, ... }
39
45
  # * Returns: formatted String
40
46
  #
41
- # (e.g.) "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
47
+ # (e.g.)
48
+ # For strings.
49
+ # "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
50
+ #
51
+ # With field type to specify format such as d(decimal), f(float),...
52
+ # "%<age>d, %<weight>.1f" % {:age => 10, :weight => 43.4}
42
53
  def %(args)
43
54
  if args.kind_of?(Hash)
44
55
  ret = dup
45
- args.each {|key, value|
46
- ret.gsub!(/\%\{#{key}\}/, value.to_s)
56
+ ret.gsub!(PERCENT_MATCH_RE) {|match|
57
+ if match == '%%'
58
+ '%'
59
+ elsif $1
60
+ key = $1.to_sym
61
+ args.has_key?(key) ? args[key] : match
62
+ elsif $2
63
+ key = $2.to_sym
64
+ args.has_key?(key) ? sprintf("%#{$3}", args[key]) : match
65
+ end
47
66
  }
48
- ret
49
67
  else
50
- ret = gsub(/%\{/, '%%{')
68
+ ret = gsub(/%([{<])/, '%%\1')
51
69
  begin
52
70
  ret._old_format_m(args)
53
71
  rescue ArgumentError => e
@@ -63,4 +81,3 @@ class String
63
81
  end
64
82
  end
65
83
 
66
- end
@@ -53,9 +53,9 @@ module GetText
53
53
 
54
54
  load_path = $LOAD_PATH
55
55
  if defined? ::Gem
56
- load_path += Gem.all_load_paths.map{|v| v =~ /(.*)\/lib$/; $1}
56
+ load_path += Gem.all_load_paths
57
57
  end
58
-
58
+ load_path.map!{|v| v.match(/(.*?)(\/lib)*?$/); $1}
59
59
  load_path.each {|path|
60
60
  default_path_rules += [
61
61
  "#{path}/data/locale/%{lang}/LC_MESSAGES/%{name}.mo",
@@ -112,7 +112,7 @@ module GetText
112
112
  if $DEBUG
113
113
  warn "MO file is not found in"
114
114
  @locale_paths.each do |path|
115
- warn " #{path}"
115
+ warn " #{path[1]}"
116
116
  end
117
117
  end
118
118
  nil
@@ -54,6 +54,7 @@ module GetText
54
54
  @last_modified = nil
55
55
  @little_endian = true
56
56
  @output_charset = output_charset
57
+ @plural_proc = nil
57
58
  super()
58
59
  end
59
60
 
@@ -162,8 +163,6 @@ module GetText
162
163
  self
163
164
  end
164
165
 
165
- # Is this number a prime number ?
166
- # http://apidock.com/ruby/Prime
167
166
  def prime?(number)
168
167
  ('1' * number) !~ /^1?$|^(11+?)\1+$/
169
168
  end
@@ -183,11 +182,11 @@ module GetText
183
182
  end
184
183
  end
185
184
 
185
+ HASHWORDBITS = 32
186
186
  # From gettext-0.12.1/gettext-runtime/intl/hash-string.h
187
187
  # Defines the so called `hashpjw' function by P.J. Weinberger
188
188
  # [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
189
189
  # 1986, 1987 Bell Telephone Laboratories, Inc.]
190
- HASHWORDBITS = 32
191
190
  def hash_string(str)
192
191
  hval = 0
193
192
  i = 0
@@ -204,8 +203,8 @@ module GetText
204
203
  hval
205
204
  end
206
205
 
206
+ #Save data as little endian format.
207
207
  def save_to_stream(io)
208
- #Save data as little endian format.
209
208
  header_size = 4 * 7
210
209
  table_size = 4 * 2 * size
211
210
 
@@ -289,6 +288,17 @@ module GetText
289
288
  #Do nothing
290
289
  end
291
290
 
291
+ def plural_as_proc
292
+ unless @plural_proc
293
+ @plural_proc = Proc.new{|n| eval(@plural)}
294
+ begin
295
+ @plural_proc.call(1)
296
+ rescue
297
+ @plural_proc = Proc.new{|n| 0}
298
+ end
299
+ end
300
+ @plural_proc
301
+ end
292
302
 
293
303
  attr_accessor :little_endian, :path, :last_modified
294
304
  attr_reader :charset, :nplurals, :plural
@@ -20,7 +20,7 @@ module GetText
20
20
 
21
21
  class PoParser < Racc::Parser
22
22
 
23
- module_eval <<'..end src/poparser.ry modeval..id7a99570e05', 'src/poparser.ry', 108
23
+ module_eval <<'..end src/poparser.ry modeval..ida5d3d657e2', 'src/poparser.ry', 108
24
24
  include GetText
25
25
  GetText.bindtextdomain("rgettext")
26
26
 
@@ -110,8 +110,26 @@ module_eval <<'..end src/poparser.ry modeval..id7a99570e05', 'src/poparser.ry',
110
110
  @comments << comment
111
111
  end
112
112
 
113
+ def parse_file(po_file, data, ignore_fuzzy = true)
114
+ args = [ po_file ]
115
+ # In Ruby 1.9, we must detect proper encoding of a PO file.
116
+ if String.instance_methods.include?(:encode)
117
+ encoding = detect_file_encoding(po_file)
118
+ args << "r:#{encoding}"
119
+ end
120
+ parse(File.open(*args) {|io| io.read }, data, ignore_fuzzy)
121
+ end
113
122
 
114
- ..end src/poparser.ry modeval..id7a99570e05
123
+ def detect_file_encoding(po_file)
124
+ open(po_file, :encoding => 'ASCII-8BIT') do |input|
125
+ input.lines.each do |line|
126
+ return Encoding.find($1) if %r["Content-Type:.*\scharset=(.*)\\n"] =~ line
127
+ end
128
+ end
129
+ Encoding.default_external
130
+ end
131
+ private :detect_file_encoding
132
+ ..end src/poparser.ry modeval..ida5d3d657e2
115
133
 
116
134
  ##### racc 1.4.5 generates ###
117
135
 
@@ -112,7 +112,7 @@ module GetText
112
112
  end
113
113
  end
114
114
 
115
- DEFAULT_PLURAL_CALC = Proc.new{|n| "n != 1"}
115
+ DEFAULT_PLURAL_CALC = Proc.new{|n| n != 1}
116
116
  DEFAULT_SINGLE_CALC = Proc.new{|n| 0}
117
117
 
118
118
  # Translates the translated string.
@@ -129,8 +129,7 @@ module GetText
129
129
  elsif msg.include?("\000")
130
130
  # [[msgstr[0], msgstr[1], msgstr[2],...], cond]
131
131
  mofile = @mofiles[lang.to_posix.to_s]
132
- cond = (mofile and mofile != :empty) ? Proc.new{|n| eval(mofile.plural)} : nil
133
- cond ||= DEFAULT_PLURAL_CALC
132
+ cond = (mofile and mofile != :empty) ? mofile.plural_as_proc : DEFAULT_PLURAL_CALC
134
133
  ret = [msg.split("\000"), cond]
135
134
  else
136
135
  ret = [[msg], DEFAULT_SINGLE_CALC]
@@ -11,11 +11,10 @@
11
11
  module GetText
12
12
 
13
13
  class TextDomainGroup
14
- attr_reader :textdomains, :supported_language_tags
14
+ attr_reader :textdomains
15
15
 
16
- def initialize(supported_language_tags)
16
+ def initialize
17
17
  @textdomains = []
18
- @supported_language_tags = supported_language_tags
19
18
  end
20
19
 
21
20
  def add(textdomain)
@@ -70,18 +70,17 @@ module GetText
70
70
  textdomain = create_or_find_textdomain(domainname,options[:path],charset)
71
71
 
72
72
  target_klass = ClassInfo.normalize_class(klass)
73
- create_or_find_textdomain_group(target_klass, options[:supported_language_tags]).add(textdomain)
73
+ create_or_find_textdomain_group(target_klass).add(textdomain)
74
74
  @@gettext_classes << target_klass unless @@gettext_classes.include? target_klass
75
75
 
76
76
  textdomain
77
77
  end
78
78
 
79
79
  def each_textdomains(klass) #:nodoc:
80
+ lang = Locale.candidates[0]
80
81
  ClassInfo.related_classes(klass, @@gettext_classes).each do |target|
81
82
  msg = nil
82
83
  if group = @@textdomain_group_pool[target]
83
- lang = Locale.candidates(:supported_language_tags => group.supported_language_tags,
84
- :type => :posix)[0]
85
84
  group.textdomains.each do |textdomain|
86
85
  yield textdomain, lang
87
86
  end
@@ -158,7 +157,6 @@ module GetText
158
157
  key = [Locale.current, klass, msgid, msgid_plural, div].hash
159
158
  msgs = @@plural_message_cache[key]
160
159
  unless (msgs and @@cached)
161
-
162
160
  # Find messages from related classes.
163
161
  msgs = nil
164
162
  each_textdomains(klass) do |textdomain, lang|
@@ -166,15 +164,15 @@ module GetText
166
164
  break if msgs
167
165
  end
168
166
 
169
- msgs = [[msgid, msgid_plural], Proc.new{|n| eval("n != 1")}] unless msgs
170
-
167
+ msgs = [[msgid, msgid_plural], TextDomain::DEFAULT_PLURAL_CALC] unless msgs
168
+
171
169
  msgstrs = msgs[0]
172
170
  if div and msgstrs[0] == msgid and index = msgstrs[0].rindex(div)
173
171
  msgstrs[0] = msgstrs[0][(index + 1)..-1]
174
172
  end
175
173
  @@plural_message_cache[key] = msgs
176
174
  end
177
-
175
+
178
176
  # Return the singular or plural message.
179
177
  msgstrs = msgs[0]
180
178
  plural = msgs[1].call(n)
@@ -191,11 +189,11 @@ module GetText
191
189
  @@plural_message_cache = {}
192
190
  end
193
191
 
194
- def create_or_find_textdomain_group(klass, supported_language_tags = nil) #:nodoc:
192
+ def create_or_find_textdomain_group(klass) #:nodoc:
195
193
  group = @@textdomain_group_pool[klass]
196
194
  return group if group
197
195
 
198
- @@textdomain_group_pool[klass] = TextDomainGroup.new(supported_language_tags)
196
+ @@textdomain_group_pool[klass] = TextDomainGroup.new
199
197
  end
200
198
 
201
199
  def create_or_find_textdomain(name, path, charset)#:nodoc:
data/lib/gettext/tools.rb CHANGED
@@ -188,27 +188,8 @@ module GetText
188
188
  mo_dir = mo_dir_rule % {:lang => lang}
189
189
  File.join(mo_dir, "#{textdomain}.mo")
190
190
  end
191
-
192
- # In Ruby 1.9, we must detect proper encoding of a PO file.
193
- def po_file_content(po_file)
194
- args = [ po_file ]
195
- if String.instance_methods.include?(:encode)
196
- encoding = detect_po_file_encoding(po_file)
197
- args << "r:#{encoding}"
198
- end
199
- File.open(*args) {|io| io.read }
200
- end
201
-
202
- def detect_po_file_encoding(po_file)
203
- open(po_file, :encoding => 'ASCII-8BIT') do |input|
204
- input.lines.each do |line|
205
- return Encoding.find($1) if %r["Content-Type:.*\scharset=(.*)\\n"] =~ line
206
- end
207
- end
208
- Encoding.default_external
209
- end
210
191
  end
211
192
 
212
193
  if __FILE__ == $0
213
194
  GetText.update_pofiles("foo", ARGV, "foo 1.1.0")
214
- end
195
+ end
@@ -35,7 +35,7 @@ module GetText
35
35
  parser = PoParser.new
36
36
  data = MOFile.new
37
37
 
38
- parser.parse(po_file_content(targetfile), data)
38
+ parser.parse_file(targetfile, data)
39
39
  data.save_to_file(output_path)
40
40
  end
41
41
 
@@ -449,12 +449,8 @@ module GetText
449
449
  end
450
450
 
451
451
  parser = PoParser.new
452
- defstr = nil
453
- refstr = nil
454
- File.open(config.defpo){|f| defstr = f.read}
455
- File.open(config.refpot){|f| refstr = f.read}
456
- defpo = parser.parse(defstr, PoData.new, false)
457
- refpot = parser.parse(refstr, PoData.new, false)
452
+ defpo = parser.parse_file(config.defpo, PoData.new, false)
453
+ refpot = parser.parse_file(config.refstrrefstr, PoData.new, false)
458
454
 
459
455
  m = Merger.new
460
456
  result = m.merge(defpo, refpot)
@@ -499,4 +495,4 @@ if $0 == __FILE__ then
499
495
  #pp parser.parse(ARGF.read)
500
496
 
501
497
  GetText.rmsgmerge
502
- end
498
+ end
data/lib/gettext/utils.rb CHANGED
@@ -13,14 +13,14 @@ warn "'gettext/utils.rb' is deprecated. Use gettext/tools.rb."
13
13
 
14
14
  module GetText
15
15
 
16
- alias :create_mofiles_org :create_mofiles
17
- alias :update_pofiles_org :update_pofiles
16
+ alias :create_mofiles_org :create_mofiles #:nodoc:
17
+ alias :update_pofiles_org :update_pofiles #:nodoc:
18
18
 
19
19
 
20
20
  # Deprecated. Use gettext/tools instead.
21
21
  def create_mofiles(verbose = false,
22
22
  podir = "./po", targetdir = "./data/locale",
23
- targetpath_rule = "%s/LC_MESSAGES")
23
+ targetpath_rule = "%s/LC_MESSAGES") # :nodoc:
24
24
  warn "'gettext/utils.rb' is deprecated. Use gettext/tools.rb."
25
25
  create_mofiles_org(:verbose => verbose,
26
26
  :po_root => podir,
@@ -29,9 +29,9 @@ module GetText
29
29
  end
30
30
 
31
31
  # Deprecated. Use gettext/tools instead.
32
- def update_pofiles(textdomain, files, app_version, po_root = "po", refpot = "tmp.pot")
32
+ def update_pofiles(textdomain, files, app_version, po_root = "po", refpot = "tmp.pot") # :nodoc:
33
33
  warn "'gettext/utils.rb' is deprecated. Use gettext/tools.rb."
34
34
  options = {:po_root => po_root}
35
35
  update_pofiles_org(textdomain, files, app_version, options)
36
36
  end
37
- end
37
+ end
@@ -8,5 +8,5 @@
8
8
  license terms as Ruby.
9
9
  =end
10
10
  module GetText
11
- VERSION = "2.0.1"
11
+ VERSION = "2.0.2"
12
12
  end
data/src/poparser.ry CHANGED
@@ -194,5 +194,23 @@ end
194
194
  @comments << comment
195
195
  end
196
196
 
197
+ def parse_file(po_file, data, ignore_fuzzy = true)
198
+ args = [ po_file ]
199
+ # In Ruby 1.9, we must detect proper encoding of a PO file.
200
+ if String.instance_methods.include?(:encode)
201
+ encoding = detect_file_encoding(po_file)
202
+ args << "r:#{encoding}"
203
+ end
204
+ parse(File.open(*args) {|io| io.read }, data, ignore_fuzzy)
205
+ end
197
206
 
207
+ def detect_file_encoding(po_file)
208
+ open(po_file, :encoding => 'ASCII-8BIT') do |input|
209
+ input.lines.each do |line|
210
+ return Encoding.find($1) if %r["Content-Type:.*\scharset=(.*)\\n"] =~ line
211
+ end
212
+ end
213
+ Encoding.default_external
214
+ end
215
+ private :detect_file_encoding
198
216
  ---- footer
@@ -19,3 +19,4 @@ msgstr ""
19
19
  msgid "first"
20
20
  msgid_plural "second"
21
21
  msgstr[0] "da_first"
22
+ msgstr[1] "da_second"
@@ -0,0 +1,27 @@
1
+ # SOME DESCRIPTIVE TITLE.
2
+ # Copyright (C) YEAR ORGANIZATION
3
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4
+ #
5
+ #, fuzzy
6
+ msgid ""
7
+ msgstr ""
8
+ "Project-Id-Version: PACKAGE VERSION\n"
9
+ "POT-Creation-Date: 2002-10-21 15:32:15+0900\n"
10
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
11
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
12
+ "Language-Team: LANGUAGE <LL@li.org>\n"
13
+ "MIME-Version: 1.0\n"
14
+ "Content-Type: text/plain; charset=US-ASCII\n"
15
+ "Content-Transfer-Encoding: ENCODING\n"
16
+ "Plural-Forms: nplurals=0; plural=EXPRESSION;\n"
17
+
18
+ #: hello_plural.rb:11
19
+ msgid "first"
20
+ msgid_plural "second"
21
+ msgstr[0] "li_first"
22
+
23
+ #: hello_plural.rb:13
24
+ msgid "one"
25
+ msgid_plural "two"
26
+ msgstr[0] "li_one"
27
+ msgstr[1] "li_two"
@@ -54,15 +54,19 @@ class TestClassInfo < Test::Unit::TestCase
54
54
  end
55
55
 
56
56
  def test_related_classes
57
+ =begin
57
58
  assert_equal [M1, Object], related_classes(M1)
58
59
  assert_equal [M1::M3, M1, M2, Object], related_classes(M1::M3)
59
60
  assert_equal [M1::M3::M4, M1::M3, M1, M2, Object], related_classes(M1::M3::M4)
61
+ =end
60
62
  assert_equal [M1::M3::C1, M1::M3, M1, M2, Object], related_classes(M1::M3::C1)
63
+ =begin
61
64
  assert_equal [M1::C2, M1, Object], related_classes(M1::C2)
62
65
  assert_equal [M1::C2::M5::C4, M1::C2::M5, M1::C2, M1, Object], related_classes(M1::C2::M5::C4)
63
66
  assert_equal [M1::C2::C3, M1::C2, M1, Object], related_classes(M1::C2::C3)
64
67
  assert_equal [M1::M6, M1, M1::M3::M4, M1::M3, M2, Object], related_classes(M1::M6)
65
68
  assert_equal [M1::M6::M7, M1::M6, M1, M1::M3::M4, M1::M3, M2, Object], related_classes(M1::M6::M7)
69
+ =end
66
70
  end
67
71
 
68
72
  def test_rellated_classes_with_all_classes
data/test/test_gettext.rb CHANGED
@@ -1,4 +1,4 @@
1
- # -*- coding: UTF-8 -*-
1
+ # -*- coding: utf-8 -*-
2
2
  require 'testlib/helper.rb'
3
3
 
4
4
  require 'testlib/simple.rb'
@@ -190,14 +190,22 @@ DDD
190
190
  assert_equal("fr_first", n_("first", "second", 0))
191
191
  assert_equal("fr_first", n_("first", "second", 1))
192
192
  assert_equal("fr_first", n_("first", "second", 2))
193
- setlocale("da")
193
+ setlocale("da") # Invalid Plural-Forms.
194
194
  assert_equal("da_first", n_("first", "second", 0))
195
195
  assert_equal("da_first", n_("first", "second", 1))
196
196
  assert_equal("da_first", n_("first", "second", 2))
197
- setlocale("la")
197
+ setlocale("la") # wrong number of msgstr.
198
198
  assert_equal("la_first", n_("first", "second", 0))
199
199
  assert_equal("la_first", n_("first", "second", 1))
200
200
  assert_equal("la_first", n_("first", "second", 2))
201
+
202
+ setlocale("li") # Invalid Plural-Forms: nplurals is set, but wrong plural=.
203
+ assert_equal("li_first", n_("first", "second", 0))
204
+ assert_equal("li_first", n_("first", "second", 1))
205
+ assert_equal("li_first", n_("first", "second", 2))
206
+ assert_equal("li_one", n_("one", "two", 0))
207
+ assert_equal("li_one", n_("one", "two", 1))
208
+ assert_equal("li_one", n_("one", "two", 2))
201
209
  end
202
210
 
203
211
  def test_plural_array
@@ -243,9 +251,11 @@ DDD
243
251
  assert_equal("japanese", _("language"))
244
252
  set_locale("en")
245
253
  assert_equal("language", _("language"))
254
+
246
255
  set_locale("fr")
247
256
  assert_equal("french", _("language"))
248
257
 
258
+ set_locale(nil)
249
259
  Locale.set "en"
250
260
  assert_equal("language", _("language"))
251
261
 
@@ -258,6 +268,22 @@ DDD
258
268
  assert_equal(Locale::Tag::Posix, GetText.locale.class)
259
269
  end
260
270
 
271
+ def test_restrict_locale
272
+ bindtextdomain("test1", :path => "locale")
273
+ Locale.set_app_language_tags("ja", "en")
274
+
275
+ Locale.set_current "fr"
276
+ assert_equal("language", _("language"))
277
+
278
+ Locale.set_current "en"
279
+ assert_equal("language", _("language"))
280
+
281
+ Locale.set_current "ja"
282
+ assert_equal("japanese", _("language"))
283
+ Locale.set_app_language_tags(nil)
284
+ end
285
+
286
+
261
287
  # Anonymous
262
288
  @@anon = Module.new
263
289
  class @@anon::I
data/test/test_string.rb CHANGED
@@ -12,25 +12,38 @@ class TestGetTextString < Test::Unit::TestCase
12
12
  assert_raise(ArgumentError) { "%-%" % [1] }
13
13
  end
14
14
 
15
- =begin
15
+ def test_sprintf_percent_in_replacement
16
+ assert_equal("%<not_translated>s", "%{msg}" % { :msg => '%<not_translated>s', :not_translated => 'should not happen' })
17
+ end
16
18
  def test_sprintf_lack_argument
17
19
  assert_equal("%{num}, test", "%{num}, %{record}" % {:record => "test"})
18
20
  assert_equal("%{record}", "%{record}" % {:num => 1})
19
21
  end
20
22
 
21
- def test_sprintf_ruby19
23
+ def test_sprintf_ruby19_style
22
24
  assert_equal("1", "%<num>d" % {:num => 1})
23
- assert_equal("1", "%<num>#b" % {:num => 1})
25
+ assert_equal("0b1", "%<num>#b" % {:num => 1})
24
26
  assert_equal("foo", "%<msg>s" % {:msg => "foo"})
25
27
  assert_equal("1.000000", "%<num>f" % {:num => 1.0})
26
28
  assert_equal(" 1", "%<num>3.0f" % {:num => 1.0})
27
29
  assert_equal("100.00", "%<num>2.2f" % {:num => 100.0})
28
30
  assert_equal("0x64", "%<num>#x" % {:num => 100.0})
31
+ assert_raise { "%<num>,d" % {:num => 100} }
32
+ assert_raise { "%<num>/d" % {:num => 100} }
33
+ end
34
+
35
+ def test_sprintf_old_style
36
+ assert_equal("foo 1.000000", "%s %f" % ["foo", 1.0])
37
+ end
38
+
39
+ def test_sprintf_mix
40
+ assert_equal("foo 1.000000", "%{name} %<num>f" % {:name => "foo", :num => 1.0})
41
+ assert_equal("%{name} 1.000000", "%{name} %f" % [1.0])
29
42
  end
30
43
 
31
44
  def test_percent
32
45
  assert_equal("% 1", "%% %<num>d" % {:num => 1.0})
46
+ assert_equal("%{num} %<num>d", "%%{num} %%<num>d" % {:num => 1})
33
47
  end
34
- =end
35
48
 
36
49
  end
@@ -4,10 +4,11 @@ require 'testlib/multi_textdomain.rb'
4
4
  class TestGetTextMulti < Test::Unit::TestCase
5
5
 
6
6
  def setup
7
- GetText.locale = "ja_JP.EUC-JP"
7
+ GetText.locale = "ja"
8
8
  end
9
9
 
10
10
  def test_two_domains_in_a_class
11
+
11
12
  test = C11.new
12
13
  assert_equal("japanese", test.test) # Use test1.po
13
14
  assert_equal("JAPANESE", test.test2) # Use test2.po
data/test/test_thread.rb CHANGED
@@ -18,7 +18,9 @@ class TestThread < Test::Unit::TestCase
18
18
  GetText.current_locale = tag
19
19
  }
20
20
  (1..10).each do |v|
21
- assert_equal Thread.current["language"], _("language")
21
+ @mutex.synchronize{
22
+ assert_equal Thread.current["language"], _("language")
23
+ }
22
24
  print "."
23
25
  $stdout.flush
24
26
  sleep sleep_time
@@ -27,10 +29,10 @@ class TestThread < Test::Unit::TestCase
27
29
  end
28
30
 
29
31
  def test_thread
30
- th1 = invoke_thread("ja_JP.eucJP", "japanese", 0.6)
31
- th2 = invoke_thread("fr", "french", 0.4)
32
- th3 = invoke_thread("en", "language", 0.2)
33
- th4 = invoke_thread("zh_CN", "language", 0.3) # No translation.
32
+ th1 = invoke_thread("ja_JP.eucJP", "japanese", 0.4)
33
+ th2 = invoke_thread("fr", "french", 0.3)
34
+ th3 = invoke_thread("en", "language", 0.1)
35
+ th4 = invoke_thread("zh_CN", "language", 0.2) # No translation.
34
36
  th1.join
35
37
  th2.join
36
38
  th3.join
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettext
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masao Mutoh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-19 00:00:00 +09:00
12
+ date: 2009-05-09 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 2.0.1
23
+ version: 2.0.2
24
24
  version:
25
25
  description: Ruby-GetText-Package is a GNU GetText-like program for Ruby. The catalog file(po-file) is same format with GNU GetText. So you can use GNU GetText tools for maintaining.
26
26
  email: mutomasa at gmail.com
@@ -866,6 +866,7 @@ files:
866
866
  - test/po/fr/test2.po
867
867
  - test/po/fr/test1.po
868
868
  - test/po/li
869
+ - test/po/li/plural_error.po
869
870
  - test/po/li/plural.po
870
871
  - test/po/ja
871
872
  - test/po/ja/pgettext.po
@@ -908,6 +909,7 @@ files:
908
909
  - test/locale/fr/LC_MESSAGES/test1.mo
909
910
  - test/locale/li
910
911
  - test/locale/li/LC_MESSAGES
912
+ - test/locale/li/LC_MESSAGES/plural_error.mo
911
913
  - test/locale/li/LC_MESSAGES/plural.mo
912
914
  - test/locale/ja
913
915
  - test/locale/ja/LC_MESSAGES