Package: ruby-gettext / 3.0.2-2~bpo70+1

0005-Restore-compatibility-with-Ruby-1.8.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
73
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Bobbio?= <lunar@debian.org>
Date: Mon, 2 Sep 2013 09:48:15 +0200
Subject: Restore compatibility with Ruby 1.8

---
 lib/gettext/core_ext/iconv.rb    |  110 ++++++++++++++++++++++++++++++++++++++
 lib/gettext/core_ext/string.rb   |   91 +++++++++++++++++++++++++++++++
 lib/gettext/locale_path.rb       |    1 +
 lib/gettext/mo.rb                |   54 +++++++++++++------
 lib/gettext/tools/parser/erb.rb  |   16 +++---
 lib/gettext/tools/parser/ruby.rb |    7 ++-
 lib/gettext/tools/xgettext.rb    |    6 ++-
 test/gettext-test-utils.rb       |   23 ++++++++
 test/test_parser.rb              |    2 +
 test/test_po_parser.rb           |    4 ++
 test/tools/parser/test_ruby.rb   |    2 +
 test/tools/test_xgettext.rb      |   24 +++++----
 12 files changed, 304 insertions(+), 36 deletions(-)
 create mode 100644 lib/gettext/core_ext/iconv.rb
 create mode 100644 lib/gettext/core_ext/string.rb

diff --git a/lib/gettext/core_ext/iconv.rb b/lib/gettext/core_ext/iconv.rb
new file mode 100644
index 0000000..389a2c7
--- /dev/null
+++ b/lib/gettext/core_ext/iconv.rb
@@ -0,0 +1,110 @@
+# encoding: utf-8
+=begin
+  iconv.rb - Pseudo Iconv class. Supports Iconv.iconv, Iconv.conv.
+
+  For Matz Ruby:
+  If you don't have iconv but glib2, this library uses glib2 iconv functions.
+
+  For JRuby:
+  Use Java String class to convert strings.
+
+  Copyright (C) 2004-2009  Masao Mutoh
+
+  You may redistribute it and/or modify it under the same
+  license terms as Ruby or LGPL.
+=end
+
+begin
+  require 'iconv.so'
+rescue LoadError
+  # Pseudo Iconv class
+  #
+  # ==== For Matz Ruby:
+  # If you don't have iconv but Ruby/GLib2, this library uses Ruby/GLib2's
+  # iconv functions.
+  #
+  # Ruby/GLib is a module which is provided from Ruby-GNOME2 Project.
+  # You can get binaries for Win32(One-Click Ruby Installer).
+  # <URL: http://ruby-gnome2.sourceforge.jp/>
+  # ==== For JRuby:
+  # Use Java String class to convert strings.
+  class Iconv
+    module Failure; end
+    class InvalidEncoding < ArgumentError;  include Failure; end
+    class IllegalSequence < ArgumentError;  include Failure; end
+    class InvalidCharacter < ArgumentError; include Failure; end
+
+    if RUBY_PLATFORM =~ /java/
+      def self.conv(to, from, str)
+        raise InvalidCharacter, "the 3rd argument is nil" unless str
+        begin
+          str = java.lang.String.new(str.unpack("C*").to_java(:byte), from)
+          str.getBytes(to).to_ary.pack("C*")
+        rescue java.io.UnsupportedEncodingException
+          raise InvalidEncoding
+        end
+      end
+    else
+      begin
+        require 'glib2'
+
+        def self.check_glib_version?(major, minor, micro) # :nodoc:
+          (GLib::BINDING_VERSION[0] > major ||
+           (GLib::BINDING_VERSION[0] == major &&
+            GLib::BINDING_VERSION[1] > minor) ||
+           (GLib::BINDING_VERSION[0] == major &&
+            GLib::BINDING_VERSION[1] == minor &&
+            GLib::BINDING_VERSION[2] >= micro))
+        end
+
+        if check_glib_version?(0, 11, 0)
+          # This is a function equivalent of Iconv.iconv.
+          # * to: encoding name for destination
+          # * from: encoding name for source
+          # * str: strings to be converted
+          # * Returns: Returns an Array of converted strings.
+          def self.conv(to, from, str)
+            begin
+              GLib.convert(str, to, from)
+            rescue GLib::ConvertError => e
+              case e.code
+              when GLib::ConvertError::NO_CONVERSION
+                raise InvalidEncoding.new(str)
+              when GLib::ConvertError::ILLEGAL_SEQUENCE
+                raise IllegalSequence.new(str)
+              else
+                raise InvalidCharacter.new(str)
+              end
+            end
+          end
+        else
+          def self.conv(to, from, str) # :nodoc:
+            begin
+              GLib.convert(str, to, from)
+            rescue
+              raise IllegalSequence.new(str)
+            end
+          end
+        end
+      rescue LoadError
+        def self.conv(to, from, str) # :nodoc:
+          warn "Iconv was not found." if $DEBUG
+          str
+        end
+      end
+    end
+    def self.iconv(to, from, str)
+      conv(to, from, str).split(//)
+    end
+  end
+end
+
+if __FILE__ == $0
+  puts Iconv.iconv("EUC-JP", "UTF-8", "ほげ").join
+  begin
+    puts Iconv.iconv("EUC-JP", "EUC-JP", "ほげ").join
+  rescue Iconv::Failure
+    puts $!
+    puts $!.class
+  end
+end
diff --git a/lib/gettext/core_ext/string.rb b/lib/gettext/core_ext/string.rb
new file mode 100644
index 0000000..900bea0
--- /dev/null
+++ b/lib/gettext/core_ext/string.rb
@@ -0,0 +1,91 @@
+# encoding: utf-8
+
+=begin
+  string.rb - Extension for String.
+
+  Copyright (C) 2005-2009 Masao Mutoh
+
+  You may redistribute it and/or modify it under the same
+  license terms as Ruby or LGPL.
+=end
+
+# Extension for String class. This feature is included in Ruby 1.9 or later but not occur TypeError.
+#
+# String#% method which accept "named argument". The translator can know
+# the meaning of the msgids using "named argument" instead of %s/%d style.
+class String
+
+  unless instance_methods.find {|m| m.to_s == 'bytesize'}
+    # For older ruby (such as ruby-1.8.5)
+    alias :bytesize :size
+  end
+
+  begin
+    formatted = "%{key}" % {:key => "value"}
+    raise ArgumentError if formatted != "value"
+  rescue ArgumentError
+    alias :_old_format_m :% # :nodoc:
+
+    PERCENT_MATCH_RE = Regexp.union(
+        /%%/,
+        /%\{(.+?)\}/,
+        /%<(.+?)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/
+    )
+
+    # call-seq:
+    #  %(arg)
+    #  %(hash)
+    #
+    # Format - Uses str as a format specification, and returns the result of applying it to arg.
+    # If the format specification contains more than one substitution, then arg must be
+    # an Array containing the values to be substituted. See Kernel::sprintf for details of the
+    # format string. This is the default behavior of the String class.
+    # * arg: an Array or other class except Hash.
+    # * Returns: formatted String
+    #
+    #  (e.g.) "%s, %s" % ["Masao", "Mutoh"]
+    #
+    # Also you can use a Hash as the "named argument". This is recommanded way for Ruby-GetText
+    # because the translators can understand the meanings of the msgids easily.
+    # * hash: {:key1 => value1, :key2 => value2, ... }
+    # * Returns: formatted String
+    #
+    #  (e.g.)
+    #         For strings.
+    #         "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
+    #
+    #         With field type to specify format such as d(decimal), f(float),...
+    #         "%<age>d, %<weight>.1f" % {:age => 10, :weight => 43.4}
+    def %(args)
+      if args.kind_of?(Hash)
+        ret = dup
+        ret.gsub!(PERCENT_MATCH_RE) {|match|
+          if match == '%%'
+            '%'
+          elsif $1
+            key = $1.to_sym
+            args.has_key?(key) ? args[key] : match
+          elsif $2
+            key = $2.to_sym
+            args.has_key?(key) ? sprintf("%#{$3}", args[key]) : match
+          end
+        }
+        ret
+      else
+        ret = gsub(/%([{<])/, '%%\1')
+        begin
+          ret._old_format_m(args)
+        rescue ArgumentError => e
+          if $DEBUG
+            $stderr.puts "  The string:#{ret}"
+            $stderr.puts "  args:#{args.inspect}"
+            puts e.backtrace
+          else
+            raise ArgumentError, e.message
+          end
+        end
+      end
+    end
+  end
+end
+
diff --git a/lib/gettext/locale_path.rb b/lib/gettext/locale_path.rb
index a0445c3..0b89581 100644
--- a/lib/gettext/locale_path.rb
+++ b/lib/gettext/locale_path.rb
@@ -11,6 +11,7 @@
 =end
 
 require 'rbconfig'
+require 'gettext/core_ext/string'
 
 module GetText
   # Treats locale-path for mo-files.
diff --git a/lib/gettext/mo.rb b/lib/gettext/mo.rb
index 56549dd..5cd7953 100644
--- a/lib/gettext/mo.rb
+++ b/lib/gettext/mo.rb
@@ -43,8 +43,12 @@ module GetText
       :trans_sysdep_tab_offset
     end
 
-    MAGIC_BIG_ENDIAN    = "\x95\x04\x12\xde".force_encoding("ASCII-8BIT")
-    MAGIC_LITTLE_ENDIAN = "\xde\x12\x04\x95".force_encoding("ASCII-8BIT")
+    MAGIC_BIG_ENDIAN    = "\x95\x04\x12\xde"
+    MAGIC_LITTLE_ENDIAN = "\xde\x12\x04\x95"
+    if "".respond_to?(:force_encoding)
+      MAGIC_BIG_ENDIAN.force_encoding("ASCII-8BIT")
+      MAGIC_LITTLE_ENDIAN.force_encoding("ASCII-8BIT")
+    end
 
     def self.open(arg = nil, output_charset = nil)
       result = self.new(output_charset)
@@ -308,24 +312,40 @@ module GetText
     attr_reader :charset, :nplurals, :plural
 
     private
-    def convert_encoding(string, original_string)
-      return string if @output_charset.nil? or @charset.nil?
+    if "".respond_to?(:encode)
+      def convert_encoding(string, original_string)
+        return string if @output_charset.nil? or @charset.nil?
 
-      if Encoding.find(@output_charset) == Encoding.find(@charset)
-        string.force_encoding(@output_charset)
-        return string
-      end
+        if Encoding.find(@output_charset) == Encoding.find(@charset)
+          string.force_encoding(@output_charset)
+          return string
+        end
 
-      begin
-        string.encode(@output_charset, @charset)
-      rescue EncodingError
-        if $DEBUG
-          warn "@charset = ", @charset
-          warn "@output_charset = ", @output_charset
-          warn "msgid = ", original_string
-          warn "msgstr = ", string
+        begin
+          string.encode(@output_charset, @charset)
+        rescue EncodingError
+          if $DEBUG
+            warn "@charset = ", @charset
+            warn "@output_charset = ", @output_charset
+            warn "msgid = ", original_string
+            warn "msgstr = ", string
+          end
+          string
+        end
+      end
+    else
+      require 'gettext/core_ext/iconv'
+      def convert_encoding(string, original_string)
+        begin
+          Iconv.conv(@output_charset, @charset, string)
+        rescue Iconv::Failure
+          if $DEBUG
+            warn "@charset = ", @charset
+            warn "@output_charset = ", @output_charset
+            warn "msgid = ", original_string
+            warn "msgstr = ", str
+          end
         end
-        string
       end
     end
 
diff --git a/lib/gettext/tools/parser/erb.rb b/lib/gettext/tools/parser/erb.rb
index dc2ad1c..dee952f 100644
--- a/lib/gettext/tools/parser/erb.rb
+++ b/lib/gettext/tools/parser/erb.rb
@@ -64,13 +64,15 @@ module GetText
       content = IO.read(@path)
       src = ERB.new(content).src
 
-      # Force the src encoding back to the encoding in magic comment
-      # or original content.
-      encoding = detect_encoding(src) || content.encoding
-      src.force_encoding(encoding)
-
-      # Remove magic comment prepended by erb in Ruby 1.9.
-      src = src.gsub(MAGIC_COMMENT, "")
+      if src.respond_to?(:encode)
+        # Force the src encoding back to the encoding in magic comment
+        # or original content.
+        encoding = detect_encoding(src) || content.encoding
+        src.force_encoding(encoding)
+
+        # Remove magic comment prepended by erb in Ruby 1.9.
+        src = src.gsub(MAGIC_COMMENT, "")
+      end
 
       RubyParser.new(@path, @options).parse_source(src)
     end
diff --git a/lib/gettext/tools/parser/ruby.rb b/lib/gettext/tools/parser/ruby.rb
index 2886b77..671d2c1 100644
--- a/lib/gettext/tools/parser/ruby.rb
+++ b/lib/gettext/tools/parser/ruby.rb
@@ -211,13 +211,16 @@ module GetText
     def parse
       source = IO.read(@path)
 
-      encoding = detect_encoding(source) || source.encoding
-      source.force_encoding(encoding)
+      if source.respond_to?(:encode)
+        encoding = detect_encoding(source) || source.encoding
+        source.force_encoding(encoding)
+      end
 
       parse_source(source)
     end
 
     def detect_encoding(source)
+      return nil unless source.respond_to?(:force_encoding)
       binary_source = source.dup.force_encoding("ASCII-8BIT")
       if /\A.*coding\s*[=:]\s*([[:alnum:]\-_]+)/ =~ binary_source
         $1.gsub(/-(?:unix|mac|dos)\z/, "")
diff --git a/test/gettext-test-utils.rb b/test/gettext-test-utils.rb
index 4dfab44..d973c06 100644
--- a/test/gettext-test-utils.rb
+++ b/test/gettext-test-utils.rb
@@ -22,6 +22,10 @@ require "tmpdir"
 require "tempfile"
 require "time"
 
+unless String.method_defined?(:encode)
+  require "iconv"
+end
+
 require "gettext"
 
 module GetTextTestUtils
@@ -41,4 +45,23 @@ module GetTextTestUtils
   def teardown_tmpdir
     FileUtils.rm_rf(@tmpdir, :secure => true) if @tmpdir
   end
+
+  def need_encoding
+    unless defined?(Encoding)
+      omit("This test needs encoding.")
+    end
+  end
+
+  def set_encoding(string, encoding)
+    return unless string.respond_to?(:force_encoding)
+    string.force_encoding(encoding)
+  end
+
+  def encode(string, encoding)
+    if string.respond_to?(:encode)
+      string.encode(encoding)
+    else
+      Iconv.iconv(encoding, "UTF-8", string).join("")
+    end
+  end
 end
diff --git a/test/test_parser.rb b/test/test_parser.rb
index 9bbc80c..68cb9aa 100644
--- a/test/test_parser.rb
+++ b/test/test_parser.rb
@@ -133,6 +133,8 @@ class TestGetTextParser < Test::Unit::TestCase
     include GetTextTestUtils
 
     def test_detect_encoding
+      need_encoding
+
       euc_file = Tempfile.new("euc-jp.rhtml")
       euc_file.open
       euc_file.puts("<%#-*- coding: euc-jp -*-%>")
diff --git a/test/test_po_parser.rb b/test/test_po_parser.rb
index 88329dd..ea73992 100644
--- a/test/test_po_parser.rb
+++ b/test/test_po_parser.rb
@@ -306,6 +306,8 @@ EOP
     end
   end
 
+  if defined?(Encoding)
+
   class TestHeader < self
     class TestEncoding < self
       def test_known
@@ -337,4 +339,6 @@ msgstr ""
       end
     end
   end
+
+  end
 end
diff --git a/test/tools/parser/test_ruby.rb b/test/tools/parser/test_ruby.rb
index 1d037f1..7459838 100644
--- a/test/tools/parser/test_ruby.rb
+++ b/test/tools/parser/test_ruby.rb
@@ -68,6 +68,8 @@ class TestRubyParser < Test::Unit::TestCase
   end
 
   class TestDetectEncoding < self
+    setup :need_encoding
+
     def test_ascii_and_hyphen
       assert_equal("euc-jp", detect_encoding("# coding: euc-jp"))
     end
diff --git a/test/tools/test_xgettext.rb b/test/tools/test_xgettext.rb
index 9837959..8d5e5ff 100644
--- a/test/tools/test_xgettext.rb
+++ b/test/tools/test_xgettext.rb
@@ -119,6 +119,8 @@ EOP
 
   class TestEncoding < self
     def test_different_encoding_from_current_locale
+      need_encoding
+
       rhtml = <<-EOR
 <%#-*- coding: sjis -*-%>
 <html>
@@ -131,34 +133,36 @@ EOP
 </html>
 EOR
       File.open(@rhtml_file_path, "w") do |rhtml_file|
-        rhtml_file.puts(rhtml.encode("sjis"))
+        rhtml_file.puts(encode(rhtml, "sjis"))
       end
 
       @xgettext.run("--output", @pot_file_path, @rhtml_file_path)
 
       encoding = "UTF-8"
       pot_content = File.read(@pot_file_path)
-      pot_content.force_encoding(encoding)
+      set_encoding(pot_content, encoding)
       expected_content = <<-EOP
 #{header}
 #: ../templates/xgettext.rhtml:7
 msgid "わたし"
 msgstr ""
 EOP
-      expected_content = expected_content.encode(encoding)
+      expected_content = encode(expected_content, encoding)
       assert_equal(expected_content, pot_content)
     end
 
     def test_multiple_encodings
+      need_encoding
+
       File.open(@rb_file_path, "w") do |rb_file|
-        rb_file.puts(<<-EOR.encode("euc-jp"))
+        rb_file.puts(encode(<<-EOR, "euc-jp"))
 # -*- coding: euc-jp -*-
 _("こんにちは")
 EOR
       end
 
       File.open(@rhtml_file_path, "w") do |rhtml_file|
-        rhtml_file.puts(<<-EOR.encode("cp932"))
+        rhtml_file.puts(encode(<<-EOR, "cp932"))
 <%# -*- coding: cp932 -*-%>
 <h1><%= _("わたし") %></h1>
 EOR
@@ -168,7 +172,7 @@ EOR
 
       encoding = "UTF-8"
       pot_content = File.read(@pot_file_path)
-      pot_content.force_encoding(encoding)
+      set_encoding(pot_content, encoding)
       expected_content = <<-EOP
 #{header}
 #: ../lib/xgettext.rb:2
@@ -179,7 +183,7 @@ msgstr ""
 msgid "わたし"
 msgstr ""
 EOP
-      expected_content = expected_content.encode(encoding)
+      expected_content = encode(expected_content, encoding)
       assert_equal(expected_content, pot_content)
     end
   end
@@ -346,13 +350,15 @@ msgstr ""
     end
 
     def test_to_code
+      need_encoding
+
       output_encoding = "EUC-JP"
       pot_content = generate(<<-EOR, "--output-encoding", output_encoding)
 # -*- coding: utf-8 -*-
 
 _("わたし")
 EOR
-      pot_content.force_encoding(output_encoding)
+      set_encoding(pot_content, output_encoding)
 
       options = {:to_code => output_encoding}
       expected_pot = <<-EOP
@@ -361,7 +367,7 @@ EOR
 msgid "わたし"
 msgstr ""
 EOP
-      expected_pot = expected_pot.encode(output_encoding)
+      expected_pot = encode(expected_pot, output_encoding)
 
       assert_equal(expected_pot, pot_content)
     end