gettext-setup 0.26 → 0.28

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d76056589632a45d8d207a37b534aae61cca3aa2
4
- data.tar.gz: 64c7222fcd4ef7a1f0ce2f78fe63c25f0a2e6ac5
3
+ metadata.gz: 26b13ff0d0fa9a44b156dbc8d9196fa7f0ec2974
4
+ data.tar.gz: df14184c767513cf61870658ef8968c5e8d76fbc
5
5
  SHA512:
6
- metadata.gz: 664b2b64c91f93ea96ae19a3eb3add4e125d2b0dd9b5b3d9502d74845ae9f890839f66ae2fd549fac8224df0981abf8004e43a908a99ac567d9b6aa231e70729
7
- data.tar.gz: 7f604a999607a42879c2975382184274f02529b2321b91a38b1cf89f52bf7bb88e55ad00108fcb7be34f1722a1315594ab2dcc1a56837a25a317c580325d1906
6
+ metadata.gz: 74b6d2e5689e91e796a0c8c75e767c1fbffc955b752d4dd340abdfa5a25391ca1d29649efd6cea16fbb5d58a23e85da9f04ff0dc955719419eb93614aff79856
7
+ data.tar.gz: 35d1f5cf80a0674c907add49aff503c5965cf9c6c3939be9ff8956548bdb7aeb8ae391b43732c8125527191cb0df4a3cd6d080e3500358daaff9f9f0889ce06c
data/README.md CHANGED
@@ -99,3 +99,14 @@ E.g. `#. The placeholder in this string represents the name of a parameter.`
99
99
  4. When a PO file reaches 100% translated and reviewed, a webhook pushes it back to the source repo ready to be consumed by your app.
100
100
 
101
101
  5. Your app checks the user's locale settings (the browser settings for web apps, or the system settings for the CLI). If we support the user's preferred locale, the app will display strings in the user's language. Otherwise, it defaults to English.
102
+
103
+ ## Merge Pot files rake task
104
+
105
+ The rake task that merges .pot files is present for the internationalisation of a module. This task uses 'msgcat', which is only natively present on OSes that are GNU based. For running this task locally on another OS you will need to download the gettext pkg and install it locally:
106
+ https://pkgs.org/download/gettext
107
+
108
+ This task will run within the gettext setup locales_path provided by GettextSetup. The result will be a merged pot file created from all pot files kept in this location.
109
+
110
+ By default the merged pot file is locales_path/project_name.pot. This can be overridden when calling the method by providing a chosen path.
111
+
112
+ Please note: Since the default merged file name is project_name.pot, it will override anything of that name within the locales directory.
@@ -98,7 +98,7 @@ module GettextSetup
98
98
  explicit = Dir.glob(File.absolute_path('*/*.po', locales_path)).map do |x|
99
99
  File.basename(File.dirname(x))
100
100
  end
101
- (explicit + [default_locale]).uniq
101
+ ([default_locale] + explicit).uniq
102
102
  end
103
103
 
104
104
  # Given an HTTP Accept-Language header return the locale with the highest
@@ -50,9 +50,14 @@ module GettextSetup
50
50
  return true
51
51
  end
52
52
 
53
- def self.generate_new_pot(locales_path = GettextSetup.locales_path, path = nil)
53
+ # @param [:locales_path] opts
54
+ # The directory for the locales.
55
+ # @param [:target_path] opts
56
+ # The output path for the new POT file.
57
+ def self.generate_new_pot(opts = {})
58
+ locales_path = opts[:locales_path] || GettextSetup.locales_path
54
59
  GettextSetup.initialize(locales_path)
55
- path ||= pot_file_path
60
+ target_path = opts[:target_path] || pot_file_path
56
61
  config = GettextSetup.config
57
62
  package_name = config['package_name']
58
63
  bugs_address = config['bugs_address']
@@ -60,7 +65,7 @@ module GettextSetup
60
65
  # Done this way to allow the user to enter an empty string in the config.
61
66
  comments_tag = config.key?('comments_tag') ? config['comments_tag'] : 'TRANSLATORS'
62
67
  version = `git describe`
63
- system("rxgettext -o #{path} --no-wrap --sort-by-file " \
68
+ system("rxgettext -o #{target_path} --no-wrap --sort-by-file " \
64
69
  "--add-comments#{comments_tag.to_s == '' ? '' : '=' + comments_tag} --msgid-bugs-address '#{bugs_address}' " \
65
70
  "--package-name '#{package_name}' " \
66
71
  "--package-version '#{version}' " \
@@ -116,13 +121,13 @@ module GettextSetup
116
121
 
117
122
  if !File.exist? path
118
123
  puts 'No existing POT file, generating new'
119
- result = GettextSetup::Pot.generate_new_pot(locales_path, path)
124
+ result = GettextSetup::Pot.generate_new_pot(locales_path: locales_path, target_path: path)
120
125
  puts "POT file #{path} has been generated" if result
121
126
  result
122
127
  else
123
128
  old_pot = path + '.old'
124
129
  File.rename(path, old_pot)
125
- result = GettextSetup::Pot.generate_new_pot(locales_path, path)
130
+ result = GettextSetup::Pot.generate_new_pot(locales_path: locales_path, target_path: path)
126
131
  if !result
127
132
  puts 'POT creation failed'
128
133
  result
@@ -137,5 +142,34 @@ module GettextSetup
137
142
  end
138
143
  end
139
144
  end
145
+
146
+ # @param [:locales_path] opts
147
+ # The directory for the locales.
148
+ def self.merge(opts = {})
149
+ locales_path = opts[:locales_path] || GettextSetup.locales_path
150
+ GettextSetup.initialize(locales_path)
151
+ target_filename = GettextSetup.config['project_name'] + '.pot'
152
+ target_path = File.expand_path(target_filename, locales_path)
153
+ oldpot_dir = File.expand_path('oldpot', locales_path)
154
+ oldpot_path = File.expand_path("oldpot/old_#{target_filename}", locales_path)
155
+
156
+ if File.exist? target_path
157
+ FileUtils.mkdir_p(oldpot_dir)
158
+ begin
159
+ FileUtils.mv(target_path, oldpot_path)
160
+ rescue
161
+ raise "There was a problem creating .pot backup #{oldpot_path}, merge failed."
162
+ end
163
+ puts "Warning - #{target_filename} already exists and will be relocated to oldpot/old_#{target_filename}."
164
+ end
165
+
166
+ locales_glob = Dir.glob("#{locales_path}/*.pot")
167
+ cmd = "msgcat #{locales_glob.join(' ')} -o #{target_path}"
168
+ _, _, _, wait = Open3.popen3(cmd)
169
+ exitstatus = wait.value
170
+ raise 'PO files failed to merge' unless exitstatus.success?
171
+ puts "PO files have been successfully merged, #{target_filename} has been created."
172
+ exitstatus
173
+ end
140
174
  end
141
175
  end
@@ -18,11 +18,12 @@ namespace :gettext do
18
18
  end
19
19
 
20
20
  desc 'Generate POT file'
21
- task :pot do
21
+ task :pot, [:target_path] do |_, args|
22
22
  begin
23
- result = GettextSetup::Pot.generate_new_pot
24
- if result
25
- puts "POT file #{GettextSetup::Pot.pot_file_path} has been generated"
23
+ target_path = args.target_path
24
+ if GettextSetup::Pot.generate_new_pot(target_path: target_path)
25
+ target_path = GettextSetup::Pot.pot_file_path if target_path.nil?
26
+ puts "POT file #{target_path} has been generated"
26
27
  else
27
28
  exit 1
28
29
  end
@@ -54,4 +55,14 @@ namespace :gettext do
54
55
  puts e.message
55
56
  end
56
57
  end
58
+
59
+ desc 'Merge all Pot files within locales folder'
60
+ task :merge do
61
+ begin
62
+ result = GettextSetup::Pot.merge
63
+ exit 1 unless result
64
+ rescue GettextSetup::NoConfigFoundError => e
65
+ puts e.message
66
+ end
67
+ end
57
68
  end
@@ -6,7 +6,7 @@ gettext:
6
6
  # called <project_name>.pot?
7
7
  project_name: 'sinatra-i18n'
8
8
  # This is used in comments in the .pot and .po files to indicate what
9
- # project the files belong to and should bea little more desctiptive than
9
+ # project the files belong to and should bea little more descriptive than
10
10
  # <project_name>
11
11
  package_name: Sinatra i18n demo
12
12
  # The locale that the default messages in the .pot file are in
@@ -0,0 +1,21 @@
1
+ ---
2
+ # This is the project-specific configuration file for setting up
3
+ # fast_gettext for your project.
4
+ gettext:
5
+ # This is used for the name of the .pot and .po files; they will be
6
+ # called <project_name>.pot?
7
+ project_name: 'merge_locales'
8
+ # This is used in comments in the .pot and .po files to indicate what
9
+ # project the files belong to and should bea little more desctiptive than
10
+ # <project_name>
11
+ package_name: Merge locales
12
+ # The locale that the default messages in the .pot file are in
13
+ default_locale: en
14
+ # The email used for sending bug reports.
15
+ bugs_address: docs@puppetlabs.com
16
+ # The holder of the copyright.
17
+ copyright_holder: Puppet, LLC.
18
+ # Patterns for +Dir.glob+ used to find all files that might contain
19
+ # translatable content, relative to the project root directory
20
+ source_files:
21
+ - 'spec/fixtures/merge_locales/*.rb'
@@ -0,0 +1,22 @@
1
+ ---
2
+ # This is the project-specific configuration file for setting up
3
+ # fast_gettext for your project.
4
+ gettext:
5
+ # This is used for the name of the .pot and .po files; they will be
6
+ # called <project_name>.pot?
7
+ project_name: 'sinatra-i18n'
8
+ # This is used in comments in the .pot and .po files to indicate what
9
+ # project the files belong to and should bea little more desctiptive than
10
+ # <project_name>
11
+ package_name: Sinatra i18n demo
12
+ # The locale that the default messages in the .pot file are in
13
+ default_locale: en
14
+ # The email used for sending bug reports.
15
+ bugs_address: docs@puppetlabs.com
16
+ # The holder of the copyright.
17
+ copyright_holder: Puppet Labs, LLC.
18
+ # Patterns for +Dir.glob+ used to find all files that might contain
19
+ # translatable content, relative to the project root directory
20
+ source_files:
21
+ - 'lib/**/*.rb'
22
+ - 'spec/**/*.rb'
@@ -0,0 +1,23 @@
1
+ # German translations for Sinatra i18n demo package.
2
+ # Copyright (C) 2016 Puppet Labs, LLC.
3
+ # This file is distributed under the same license as the Sinatra i18n demo package.
4
+ # Automatically generated, 2016.
5
+ #
6
+ msgid ""
7
+ msgstr ""
8
+ "Project-Id-Version: Sinatra i18n demo \n"
9
+ "Report-Msgid-Bugs-To: docs@puppetlabs.com\n"
10
+ "POT-Creation-Date: 2016-04-05 10:39-0700\n"
11
+ "PO-Revision-Date: 2016-02-26 18:21-0800\n"
12
+ "Last-Translator: Automatically generated\n"
13
+ "Language-Team: none\n"
14
+ "Language: de\n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=UTF-8\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
+ "Plural-Forms: nplurals=2; plural=(n != 1);\n"
19
+
20
+ #: ../app.rb:14
21
+ msgid "Hello, world!"
22
+ msgstr "Hallo, Welt!"
23
+
@@ -0,0 +1,40 @@
1
+ # SOME DESCRIPTIVE TITLE.
2
+ # Copyright (C) 2017 Puppet Labs, LLC.
3
+ # This file is distributed under the same license as the Sinatra i18n demo package.
4
+ # FIRST AUTHOR <EMAIL@ADDRESS>, 2017.
5
+ #
6
+ #, fuzzy
7
+ msgid ""
8
+ msgstr ""
9
+ "Project-Id-Version: Sinatra i18n demo 0.21-4-geccdac4\n"
10
+ "\n"
11
+ "Report-Msgid-Bugs-To: docs@puppetlabs.com\n"
12
+ "POT-Creation-Date: 2017-04-11 20:08-0500\n"
13
+ "PO-Revision-Date: 2017-04-11 20:08-0500\n"
14
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
15
+ "Language-Team: LANGUAGE <LL@li.org>\n"
16
+ "Language: \n"
17
+ "MIME-Version: 1.0\n"
18
+ "Content-Type: text/plain; charset=UTF-8\n"
19
+ "Content-Transfer-Encoding: 8bit\n"
20
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
21
+
22
+ #: ../../lib/gettext_setup_spec.rb:26 ../../lib/gettext_setup_spec.rb:89 ../../lib/gettext_setup_spec.rb:91
23
+ msgid "Hello, world!"
24
+ msgstr ""
25
+
26
+ #: ../../lib/pot_spec.rb:52
27
+ msgid "merged-po-file"
28
+ msgstr ""
29
+
30
+ #: ../../lib/pot_spec.rb:74
31
+ msgid "no-pot-file"
32
+ msgstr ""
33
+
34
+ #: ../../lib/pot_spec.rb:85
35
+ msgid "some-spec-only-string"
36
+ msgstr ""
37
+
38
+ #: ../../lib/pot_spec.rb:104
39
+ msgid "unchanged-string"
40
+ msgstr ""
@@ -70,11 +70,11 @@ describe GettextSetup do
70
70
  expect(GettextSetup.candidate_locales).to include('en')
71
71
  GettextSetup.clear
72
72
  begin
73
- old_locale = ENV['LANG']
74
- ENV['LANG'] = 'de_DE'
73
+ old_locale = Locale.current
74
+ Locale.current = 'de_DE'
75
75
  expect(GettextSetup.candidate_locales).to eq('de_DE,de,en')
76
76
  ensure
77
- ENV['LANG'] = old_locale
77
+ Locale.current = old_locale
78
78
  end
79
79
  end
80
80
  end
@@ -18,6 +18,10 @@ describe GettextSetup::Pot do
18
18
  File.join(File.dirname(__FILE__), '../../fixtures/locales')
19
19
  end
20
20
 
21
+ def merge_locales_path
22
+ File.join(File.dirname(__FILE__), '../../fixtures/merge_locales')
23
+ end
24
+
21
25
  describe 'string_changes?', if: msgcmp_present? do
22
26
  old_pot = File.absolute_path('../../fixtures/string_changes/old.pot', File.dirname(__FILE__))
23
27
 
@@ -45,12 +49,12 @@ describe GettextSetup::Pot do
45
49
  context 'generate_new_pot' do
46
50
  it "fails when GettextSetup can't find a config.yaml" do
47
51
  path = File.join(Dir.mktmpdir, 'empty.pot')
48
- expect { GettextSetup::Pot.generate_new_pot(Dir.mktmpdir, path) }.to raise_error(NoConfigFoundError)
52
+ expect { GettextSetup::Pot.generate_new_pot(locales_path: Dir.mktmpdir, target_path: path) }.to raise_error(NoConfigFoundError)
49
53
  end
50
54
  it 'builds a POT file' do
51
55
  path = File.join(Dir.mktmpdir, 'new.pot')
52
56
  expect do
53
- GettextSetup::Pot.generate_new_pot(fixture_locales_path, path)
57
+ GettextSetup::Pot.generate_new_pot(locales_path: fixture_locales_path, target_path: path)
54
58
  end.to output('').to_stdout # STDOUT is determined in `update_pot`.
55
59
  contents = File.read(path)
56
60
  expect(contents).to match(/Fixture locales/)
@@ -144,4 +148,59 @@ describe GettextSetup::Pot do
144
148
  expect(new_contents).to eq(contents)
145
149
  end
146
150
  end
151
+ context 'Merge pot files' do
152
+ # setup
153
+ before :all do
154
+ { 'ruby' => 'ruby.pot', 'puppet' => 'puppet.pot', 'metadata' => 'metadata.pot' }.each do |pot_type, pot_name|
155
+ File.open(File.join(merge_locales_path, pot_name), 'w') do |file|
156
+ file.write <<-EOF
157
+ # Copyright (C) 2017 Puppet, Inc.
158
+ # This file is distributed under the same license as the puppetlabs-mysql package.
159
+ # FIRST AUTHOR <EMAIL@ADDRESS>, 2017.
160
+ #
161
+ #, fuzzy
162
+ msgid ""
163
+ msgstr ""
164
+ "Project-Id-Version: puppetlabs-mysql 3.11.0-30-g4cc0bbf\\n"
165
+ "Report-Msgid-Bugs-To: docs@puppet.com\\n"
166
+ "POT-Creation-Date: 2017-08-26 21:30+0100\\n"
167
+ "PO-Revision-Date: 2017-08-26 21:30+0100\\n"
168
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
169
+ "Language-Team: LANGUAGE <LL@li.org>\\n"
170
+ "MIME-Version: 1.0\\n"
171
+ "Content-Type: text/plain; charset=UTF-8\\n"
172
+ "Content-Transfer-Encoding: 8bit\\n"
173
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n"
174
+
175
+ #: ../lib/puppet/parser/functions/mysql_strip_hash.rb:11
176
+ msgid "this is a #{pot_type} string"
177
+ msgstr ""
178
+ EOF
179
+ end
180
+ end
181
+ end
182
+ it 'merges pot files' do
183
+ expect do
184
+ GettextSetup::Pot.merge(locales_path: merge_locales_path)
185
+ end.to output(%r{PO files have been successfully merged}).to_stdout
186
+ contents = File.read(File.join(merge_locales_path, GettextSetup.config['project_name'] + '.pot'))
187
+ expect(contents).to match(%r{.*\"this is a metadata string\".*})
188
+ expect(contents).to match(%r{.*\"this is a puppet string\".*})
189
+ expect(contents).to match(%r{.*\"this is a ruby string\".*})
190
+ end
191
+
192
+ it 'creates an oldpot file if one already exists' do
193
+ expect do
194
+ GettextSetup::Pot.merge(locales_path: merge_locales_path)
195
+ end.to output("Warning - merge_locales.pot already exists and will be relocated to oldpot/old_merge_locales.pot.\nPO files have been successfully merged, merge_locales.pot has been created.\n").to_stdout
196
+ file = File.expand_path('oldpot/old_merge_locales.pot', merge_locales_path)
197
+ expect(File.exist?(file)).to be true
198
+ end
199
+
200
+ # cleanup
201
+ after :all do
202
+ FileUtils.rm(Dir.glob("#{merge_locales_path}/*.pot"))
203
+ FileUtils.rm_rf("#{merge_locales_path}/oldpot")
204
+ end
205
+ end
147
206
  end
@@ -9,6 +9,7 @@ describe 'gettext.rake' do
9
9
  tmp_locales = File.expand_path('../../fixtures/tmp_locales', File.dirname(__FILE__))
10
10
  fixture_locales = File.expand_path('../../fixtures/fixture_locales', File.dirname(__FILE__))
11
11
  tmp_pot_path = File.expand_path('sinatra-i18n.pot', tmp_locales)
12
+ merge_locales = File.expand_path('../../fixtures/merge_locales', File.dirname(__FILE__))
12
13
 
13
14
  before :each do
14
15
  FileUtils.rm_r(tmp_locales, force: true)
@@ -40,6 +41,14 @@ describe 'gettext.rake' do
40
41
  end.to raise_error(SystemExit)
41
42
  end
42
43
  end
44
+ context Rake::Task['gettext:pot'] do
45
+ it 'outputs correctly, when passing a filename' do
46
+ expect do
47
+ GettextSetup.initialize(tmp_locales)
48
+ subject.invoke(File.expand_path('bill.pot', tmp_locales))
49
+ end.to output(/POT file .+\/spec\/fixtures\/tmp_locales\/bill.pot has been generated/).to_stdout
50
+ end
51
+ end
43
52
  context Rake::Task['gettext:metadata_pot'] do
44
53
  it 'outputs correctly' do
45
54
  expect do
@@ -100,4 +109,20 @@ describe 'gettext.rake' do
100
109
  end.to raise_error(SystemExit)
101
110
  end
102
111
  end
112
+
113
+ context Rake::Task['gettext:merge'] do
114
+ it 'outputs correctly' do
115
+ expect do
116
+ GettextSetup.initialize(merge_locales)
117
+ subject.invoke
118
+ end.to output(/PO files have been successfully merged/).to_stdout
119
+ end
120
+ it 'exits 1 on error' do
121
+ allow(GettextSetup::Pot).to receive(:merge).and_return(false)
122
+ expect do
123
+ GettextSetup.initialize(merge_locales)
124
+ subject.invoke
125
+ end.to raise_error(SystemExit)
126
+ end
127
+ end
103
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettext-setup
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.26'
4
+ version: '0.28'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-11 00:00:00.000000000 Z
11
+ date: 2017-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fast_gettext
@@ -188,6 +188,7 @@ files:
188
188
  - spec/fixtures/locales/config.yaml
189
189
  - spec/fixtures/locales/de/sinatra-i18n.po
190
190
  - spec/fixtures/locales/sinatra-i18n.pot
191
+ - spec/fixtures/merge_locales/config.yaml
191
192
  - spec/fixtures/spec_locales/config.yaml
192
193
  - spec/fixtures/spec_locales/sinatra-i18n.pot
193
194
  - spec/fixtures/string_changes/add.pot
@@ -195,6 +196,9 @@ files:
195
196
  - spec/fixtures/string_changes/non_string_changes.pot
196
197
  - spec/fixtures/string_changes/old.pot
197
198
  - spec/fixtures/string_changes/remove.pot
199
+ - spec/fixtures/tmp_locales/config.yaml
200
+ - spec/fixtures/tmp_locales/de/sinatra-i18n.po
201
+ - spec/fixtures/tmp_locales/sinatra-i18n.pot
198
202
  - spec/lib/gettext-setup/gettext_setup_spec.rb
199
203
  - spec/lib/gettext-setup/metadata_pot_spec.rb
200
204
  - spec/lib/gettext-setup/pot_spec.rb
@@ -232,6 +236,7 @@ test_files:
232
236
  - spec/fixtures/locales/config.yaml
233
237
  - spec/fixtures/locales/de/sinatra-i18n.po
234
238
  - spec/fixtures/locales/sinatra-i18n.pot
239
+ - spec/fixtures/merge_locales/config.yaml
235
240
  - spec/fixtures/spec_locales/config.yaml
236
241
  - spec/fixtures/spec_locales/sinatra-i18n.pot
237
242
  - spec/fixtures/string_changes/add.pot
@@ -239,6 +244,9 @@ test_files:
239
244
  - spec/fixtures/string_changes/non_string_changes.pot
240
245
  - spec/fixtures/string_changes/old.pot
241
246
  - spec/fixtures/string_changes/remove.pot
247
+ - spec/fixtures/tmp_locales/config.yaml
248
+ - spec/fixtures/tmp_locales/de/sinatra-i18n.po
249
+ - spec/fixtures/tmp_locales/sinatra-i18n.pot
242
250
  - spec/lib/gettext-setup/gettext_setup_spec.rb
243
251
  - spec/lib/gettext-setup/metadata_pot_spec.rb
244
252
  - spec/lib/gettext-setup/pot_spec.rb