File: task.rb

package info (click to toggle)
ruby-gettext-i18n-rails-js 2.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: ruby: 927; javascript: 33; makefile: 11
file content (129 lines) | stat: -rw-r--r-- 3,516 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
# frozen_string_literal: true

#
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

module GettextI18nRailsJs
  module Task
    extend self

    def po_to_json
      set_config

      if files_list.empty?
        puts "Couldn't find PO files in #{locale_path}, run 'rake gettext:find'"
      else
        files_iterate
        print_footer
      end
    end

    protected

    def destination(lang)
      path = output_path.join(lang)
      path.mkpath

      path.join("#{domain}.js").open("w") do |f|
        f.rewind
        f.write yield
      end

      puts "Created #{domain}.js in #{path}"
    end

    def lang_for(file)
      file.dirname.basename.to_s
    end

    def json_for(file)
      PoToJson.new(
        file.to_s
      ).generate_for_jed(
        lang_for(file),
        GettextI18nRailsJs.config.jed_options
      )
    end

    def set_config
      GettextI18nRailsJs::Parser::Javascript
        .gettext_function = GettextI18nRailsJs.config.javascript_function

      GettextI18nRailsJs::Parser::Handlebars
        .gettext_function = GettextI18nRailsJs.config.handlebars_function
    end

    def files_iterate
      files_list.each do |input|
        # Language is used for filenames, while language code is used as the
        # in-app language code. So for instance, simplified chinese will live
        # in app/assets/locale/zh_CN/app.js but inside the file the language
        # will be referred to as locales['zh-CN']. This is to adapt to the
        # existing gettext_rails convention.

        destination(
          lang_for(input)
        ) do
          json_for(input)
        end
      end
    end

    def files_list
      require "gettext_i18n_rails/tasks"

      ::Pathname.glob(
        ::File.join(
          locale_path,
          "**",
          "*.po"
        )
      )
    end

    def output_path
      engine_root.join(
        GettextI18nRailsJs.config.output_path
      )
    end

    def engine_root
      GettextI18nRailsJs.config.rails_engine.root
    end

    def domain
      GettextI18nRailsJs.config.domain
    end

    def print_footer
      puts
      puts "All files created, make sure they are being added to your assets."
      puts "If they are not, you can add them with this line (configurable):"
      puts
      puts "//= require_tree ./locale"
      puts "//= require gettext/all"
      puts
    end
  end
end