File: msginit.rb

package info (click to toggle)
ruby-gettext 3.4.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,084 kB
  • sloc: ruby: 10,461; makefile: 8
file content (586 lines) | stat: -rw-r--r-- 17,098 bytes parent folder | 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012  Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright (C) 2012-2021  Sutou Kouhei <kou@clear-code.com>
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require "etc"
require "optparse"

begin
  require "datasets"
rescue LoadError
end
require "locale/info"

require "gettext"
require "gettext/po_parser"
require "gettext/tools/msgmerge"

module GetText
  module Tools
    class MsgInit
      class Error < StandardError
      end

      class ArgumentError < Error
      end

      class ValidationError < Error
      end

      class << self
        # Create a new .po file from initializing .pot file with user's
        # environment and input.
        # @param [Array<String>] arguments arguments for rmsginit.
        # @return [void]
        def run(*arguments)
          new.run(*arguments)
        end
      end

      include GetText

      bindtextdomain("gettext")

      def initialize
        @input_file = nil
        @output_file = nil
        @locale = nil
        @language = nil
        @entry = nil
        @comment = nil
        @translator = nil
        @set_translator = true
        @translator_name = nil
        @translator_eamil = nil
      end

      # Create .po file from .pot file, user's inputs and metadata.
      # @param [Array] arguments the list of arguments for rmsginit
      def run(*arguments)
        parse_arguments(*arguments)
        validate

        parser = POParser.new
        parser.ignore_fuzzy = false
        pot = parser.parse_file(@input_file, GetText::PO.new)
        po = replace_pot_header(pot)

        File.open(@output_file, "w") do |f|
          f.puts(po.to_s)
        end
      end

      private
      VERSION = GetText::VERSION

      def parse_arguments(*arguments)
        parser = OptionParser.new
        description = _("Create a new .po file from initializing .pot " +
                          "file with user's environment and input.")
        parser.separator(description)
        parser.separator("")
        parser.separator(_("Specific options:"))

        input_description = _("Use INPUT as a .pot file. If INPUT is not " +
                                "specified, INPUT is a .pot file existing " +
                                "the current directory.")
        parser.on("-i", "--input=FILE", input_description) do |input|
          @input_file = input
        end

        output_description = _("Use OUTPUT as a created .po file. If OUTPUT " +
                                 "is not specified, OUTPUT depend on LOCALE " +
                                 "or the current locale on your environment.")
        parser.on("-o", "--output=OUTPUT", output_description) do |output|
          @output_file = output
        end

        locale_description = _("Use LOCALE as target locale. If LOCALE is " +
                                 "not specified, LOCALE is the current " +
                                 "locale on your environment.")
        parser.on("-l", "--locale=LOCALE", locale_description) do |loc|
          @locale = loc
        end

        parser.on("--[no-]translator",
                  _("Whether set translator information or not"),
                  _("(set)")) do |boolean|
          @set_translator = boolean
        end

        parser.on("--translator-name=NAME",
                  _("Use NAME as translator name")) do |name|
          @translator_name = name
        end

        parser.on("--translator-email=EMAIL",
                  _("Use EMAIL as translator email address")) do |email|
          @translator_email = email
        end

        parser.on("-h", "--help", _("Display this help and exit")) do
          puts(parser.help)
          exit(true)
        end

        version_description = _("Display version and exit")
        parser.on_tail("-v", "--version", version_description) do
          puts(VERSION)
          exit(true)
        end

        begin
          parser.parse!(arguments)
        rescue OptionParser::ParseError
          raise(ArgumentError, $!.message)
        end
      end

      def validate
        if @input_file.nil?
          @input_file = Dir.glob("./*.pot").first
          if @input_file.nil?
            raise(ValidationError,
                  _(".pot file does not exist in the current directory."))
          end
        else
          unless File.exist?(@input_file)
            raise(ValidationError,
                  _("file '%s' does not exist.") % @input_file)
          end
        end

        if @locale.nil?
          language_tag = Locale.current
        else
          language_tag = Locale::Tag.parse(@locale)
        end

        unless valid_locale?(language_tag)
          raise(ValidationError,
                _("Locale '%s' is invalid. " +
                  "Please check if your specified locale is usable.") %
                language_tag)
        end
        @locale = language_tag.to_simple.to_s
        @language = language_tag.language

        @output_file ||= "#{@locale}.po"
        if File.exist?(@output_file)
          raise(ValidationError,
                _("file '%s' has already existed.") % @output_file)
        end
      end

      def valid_locale?(language_tag)
        return false if language_tag.instance_of?(Locale::Tag::Irregular)

        Locale::Info.language_code?(language_tag.language)
      end

      def replace_pot_header(pot)
        @entry = pot[""].msgstr
        @comment = pot[""].translator_comment
        @translator = translator_info

        replace_entry
        replace_comment

        pot[""] = @entry
        pot[""].translator_comment = @comment
        pot[""].flags = pot[""].flags.reject do |flag|
          flag == "fuzzy"
        end
        pot
      end

      def translator_info
        return nil unless @set_translator
        name = translator_name
        email = translator_email
        if name and email
          "#{name} <#{email}>"
        else
          nil
        end
      end

      def translator_name
        @translator_name ||= read_translator_name
      end

      def read_translator_name
        prompt(_("Please enter your full name"), guess_translator_name)
      end

      def guess_translator_name
        name = guess_translator_name_from_password_entry
        name ||= ENV["USERNAME"]
        name
      end

      def guess_translator_name_from_password_entry
        password_entry = find_password_entry
        return nil if password_entry.nil?

        name = password_entry.gecos.split(/,/).first.strip
        name = nil if name.empty?
        name
      end

      def find_password_entry
        Etc.getpwuid
      rescue ArgumentError
        nil
      end

      def translator_email
        @translator_email ||= read_translator_email
      end

      def read_translator_email
        prompt(_("Please enter your email address"), guess_translator_email)
      end

      def guess_translator_email
        ENV["EMAIL"]
      end

      def prompt(message, default)
        return default if $stdin.eof?

        print(message)
        print(" [#{default}]") if default
        print(": ")

        user_input = $stdin.gets.chomp
        if user_input.empty?
          default
        else
          user_input
        end
      end

      def replace_entry
        replace_last_translator
        replace_pot_revision_date
        replace_language
        replace_plural_forms
      end

      def replace_comment
        replace_description
        replace_first_author
        replace_copyright_year
        @comment = @comment.gsub(/^fuzzy$/, "")
      end

      EMAIL = "EMAIL@ADDRESS"
      FIRST_AUTHOR_KEY = /^FIRST AUTHOR <#{EMAIL}>, (\d+\.)$/

      def replace_last_translator
        unless @translator.nil?
          @entry = @entry.gsub(LAST_TRANSLATOR_KEY, "\\1 #{@translator}")
        end
      end

      POT_REVISION_DATE_KEY = /^(PO-Revision-Date:).+/

      def replace_pot_revision_date
        @entry = @entry.gsub(POT_REVISION_DATE_KEY, "\\1 #{revision_date}")
      end

      LANGUAGE_KEY = /^(Language:).+/
      LANGUAGE_TEAM_KEY = /^(Language-Team:).+/

      def replace_language
        language_name = Locale::Info.get_language(@language).name
        @entry = @entry.gsub(LANGUAGE_KEY, "\\1 #{@locale}")
        @entry = @entry.gsub(LANGUAGE_TEAM_KEY, "\\1 #{language_name}")
      end

      PLURAL_FORMS =
        /^(Plural-Forms:) nplurals=INTEGER; plural=EXPRESSION;$/

      def replace_plural_forms
        plural_entry = plural_forms(@language)
        if PLURAL_FORMS =~ @entry
          @entry = @entry.gsub(PLURAL_FORMS, "\\1 #{plural_entry}\n")
        else
          @entry << "Plural-Forms: #{plural_entry}\n"
        end
      end

      def plural_forms(language)
        converter = CLDRPluralsConverter.new(language)
        converter.convert
      end

      DESCRIPTION_TITLE = /^SOME DESCRIPTIVE TITLE\.$/

      def replace_description
        language_name = Locale::Info.get_language(@language).name
        package_name = ""
        @entry.gsub(/Project-Id-Version: (.+?) .+/) do
          package_name = $1
        end
        description = "#{language_name} translations " +
                        "for #{package_name} package."
        @comment = @comment.gsub(DESCRIPTION_TITLE, "\\1 #{description}")
      end

      YEAR_KEY = /^(FIRST AUTHOR <#{EMAIL}>,) YEAR\.$/
      LAST_TRANSLATOR_KEY = /^(Last-Translator:) FULL NAME <#{EMAIL}>$/

      def replace_first_author
        @comment = @comment.gsub(YEAR_KEY, "\\1 #{year}.")
        unless @translator.nil?
          @comment = @comment.gsub(FIRST_AUTHOR_KEY, "#{@translator}, \\1")
        end
      end

      COPYRIGHT_KEY = /^(Copyright \(C\)) YEAR (THE PACKAGE'S COPYRIGHT HOLDER)$/
      def replace_copyright_year
        @comment = @comment.gsub(COPYRIGHT_KEY, "\\1 #{year} \\2")
      end

      def now
        @now ||= Time.now
      end

      def revision_date
        now.strftime("%Y-%m-%d %H:%M%z")
      end

      def year
        now.year
      end

      class CLDRPluralsConverter
        def initialize(language)
          @language = language

        end

        def convert
          n_plurals = nil
          expression = nil
          if defined?(Datasets::CLDRPlurals)
            plurals = Datasets::CLDRPlurals.new
            plurals.each do |locale|
              next unless locale.name == @language
              n_plurals, expression = convert_plural_rules(locale.rules)
              break
            end
          end
          "nplurals=#{n_plurals}; plural=#{expression};"
        end

        private
        def convert_plural_rules(rules)
          n_plurals = 1
          conditions = []
          order = [
            "one",
            "zero",
            "two",
            "few",
            "many",
            "other",
          ]
          rules = rules.reject do |rule|
            rule.integer_samples.nil?
          end
          rules = rules.sort_by do |rule|
            order.index(rule.count)
          end
          rules[0..-2].each do |rule|
            next if rule.condition.nil?
            condition = convert_plural_condition(rule.condition)
            next if condition.nil?
            next if condition == false
            n_plurals += 1
            conditions << condition
          end
          expression = ""
          case conditions.size
          when 0
            expression << "0"
          when 1
            condition = conditions[0]
            case condition
            when "(n == 1)"
              expression << "n != 1"
            when "(n <= 1)"
              expression << "n > 1"
            else
              expression << "#{condition} ? 1 : 0"
            end
          else
            (conditions.size + 1).times do |i|
              if i == conditions.size
                expression << i.to_s
              else
                condition = conditions[i]
                expression << "#{condition} ? #{i} : "
              end
            end
          end
          [n_plurals, expression]
        end

        def convert_plural_condition(condition)
          case condition[0]
          when :and
            gettext_condition = nil
            condition[1..-1].each do |sub_condition|
              sub_gettext_condition = convert_plural_condition(sub_condition)
              case sub_gettext_condition
              when String
                if gettext_condition.is_a?(String)
                  gettext_condition << " && #{sub_gettext_condition}"
                else
                  gettext_condition = sub_gettext_condition
                end
              when TrueClass
                unless gettext_condition.is_a?(String)
                  gettext_condition = true
                end
              when FalseClass
                return false
              else
                raise "unknown value #{sub_gettext_condition.inspect}"
              end
            end
            gettext_condition
          when :or
            gettext_condition = false
            condition[1..-1].each do |sub_condition|
              sub_gettext_condition = convert_plural_condition(sub_condition)
              case sub_gettext_condition
              when String
                if gettext_condition.is_a?(String)
                  gettext_condition << " || #{sub_gettext_condition}"
                else
                  gettext_condition = sub_gettext_condition
                end
              when TrueClass
                return true
              when FalseClass
              else
                raise "unknown value #{sub_gettext_condition.inspect}"
              end
            end
            gettext_condition
          when :equal
            left = convert_plural_condition(condition[1])
            right = condition[2]
            case left
            when String
              right = compact_equal_values(right)
              gettext_conditions = right.collect do |right_value|
                case right_value
                when Range
                  if right_value.begin.zero?
                    "(#{left} <= #{right_value.end})"
                  else
                    "(#{left} >= #{right_value.begin} && " +
                      "#{left} <= #{right_value.end})"
                  end
                else
                  "(#{left} == #{right_value})"
                end
              end
              if gettext_conditions.size == 1
                gettext_conditions[0]
              else
                gettext_conditions.join(" || ")
              end
            when 0
              if right.include?(0)
                true
              else
                false
              end
            else
              false
            end
          when :not_equal
            left = convert_plural_condition(condition[1])
            right = condition[2]
            case left
            when String
              right = compact_equal_values(right)
              gettext_conditions = right.collect do |right_value|
                case right_value
                when Range
                  "(#{left} < #{right_value.begin} || " +
                    "#{left} > #{right_value.end})"
                else
                  "(#{left} != #{right_value})"
                end
              end
              if gettext_conditions.size == 1
                gettext_conditions[0]
              else
                gettext_conditions = gettext_conditions.collect do |gettext_condition|
                  "(#{gettext_condition})"
                end
                gettext_conditions.join(" && ")
              end
            when 0
              if right.include?(0)
                false
              else
                true
              end
            else
              false
            end
          when :mod
            left = convert_plural_condition(condition[1])
            right = condition[2]
            case left
            when "n"
              "(n % #{right})"
            else
              false
            end
          when "n", "i"
            "n"
          when "v", "w"
            0
          when "f", "t", "c", "e"
            false
          else
            raise "unknown operator: #{condition[0].inspect}"
          end
        end

        def compact_equal_values(values)
          if values == [0, 1]
            [0..1]
          else
            values
          end
        end
      end
    end
  end
end