rails_translation_manager 1.1.0 → 1.2.0

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
  SHA256:
3
- metadata.gz: 0b6a096ee29746cead2529ea0cb83660c0e0217b710e085790969f203f246871
4
- data.tar.gz: 3c13cb54190c17f1382246827c120cf928e7763a9de4b4724ec36ffe6005fc62
3
+ metadata.gz: 1698a9fc00bbc68d6a07e11ab9a61fa009cdf11ab0556c1634e6586ea7f8043f
4
+ data.tar.gz: b93eba9d7309f7e7184a548487592a684bc508afd87af84a6270a4abaa66f624
5
5
  SHA512:
6
- metadata.gz: 37abc2c32de6245dc3d1cd57da56f14ab3c68f88f9b3802a65735d3c36c02984f823d45ff177729daad6b2234929d03535ca28239a4188889a14be60f35875df
7
- data.tar.gz: 73f08794d826203ba2be01695751d742bd7a546dee1bf9f8946c98cf57350a48651addd5eef53cfcd9b23303900d6efe0dcd41fb0ae81d67ba060ed851b5061a
6
+ metadata.gz: e8877d5f9a8b3baff02a1f62df4b5b8645297774cbb6fc583ff04b3c23c41298d093f8455253f803f7d21fbdbdf4a832b93d5b0582dda96df0a67eff49041be5
7
+ data.tar.gz: bd9f568b8f7c330c005b9c897179d3a37d0f4a88dd718af7135544fd47cd7719329139799c4d050f2a85de20a134b62bb85ad771017002f408faac92a3097e34
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.2.0
2
+
3
+ Add two new checker classes. https://github.com/alphagov/rails_translation_manager/pull/30
4
+
5
+ ## 1.1.3
6
+
7
+ Handle importing files that contain rows with a blank "key". https://github.com/alphagov/rails_translation_manager/pull/28
8
+
9
+ ## 1.1.2
10
+
11
+ Handle importing files that contain Byte Order Marks. https://github.com/alphagov/rails_translation_manager/pull/27
12
+
13
+ ## 1.1.1
14
+
15
+ Fix Rails Translation Manager / Rails naming clash for class. https://github.com/alphagov/rails_translation_manager/pull/26
16
+
1
17
  ## 1.1.0
2
18
 
3
19
  Allow multiple files per language to be imported. https://github.com/alphagov/rails_translation_manager/pull/20
@@ -1,5 +1,4 @@
1
- class TranslationHelper
2
-
1
+ class RailsTranslationManager::I18nTasksOptionParser
3
2
  def initialize(task_options, locale)
4
3
  @task_options = task_options
5
4
  @locale = locale
@@ -15,7 +15,9 @@ class RailsTranslationManager::Importer
15
15
  end
16
16
 
17
17
  def import
18
- csv = CSV.read(csv_path, headers: true, header_converters: :downcase)
18
+ csv = reject_nil_keys(
19
+ CSV.read(csv_path, encoding: "bom|utf-8", headers: true, header_converters: :downcase)
20
+ )
19
21
 
20
22
  multiple_files_per_language ? import_csv_into_multiple_files(csv) : import_csv(csv)
21
23
  end
@@ -40,6 +42,14 @@ class RailsTranslationManager::Importer
40
42
  write_yaml(import_yml_path, { locale.to_s => data })
41
43
  end
42
44
 
45
+ def reject_nil_keys(csv)
46
+ csv.reject do |row|
47
+ nil_key = row["key"].nil?
48
+ puts "Invalid row: #{row.inspect} for csv_path: #{csv_path}\n" if nil_key == true
49
+ nil_key
50
+ end
51
+ end
52
+
43
53
  def parse_translation(translation)
44
54
  if translation =~ /^\[/
45
55
  values = translation.gsub(/^\[/, '').gsub(/\]$/, '').gsub("\"", '').split(/\s*,\s*/)
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MissingDeclaredLocales < BaseChecker
4
+ include LocaleCheckerHelper
5
+
6
+ def report
7
+ diff = compare
8
+ format_error_message(diff) unless diff.empty?
9
+ end
10
+
11
+ private
12
+
13
+ def format_error_message(locales)
14
+ <<~OUTPUT.chomp
15
+ \e[31m[ERROR]\e[0m No locale files detected for:
16
+
17
+ #{locales.join("\n\n")}
18
+
19
+ \e[1mEither create these locale files or remove these locales from your I18n `available_locales` config.\e[22m
20
+ OUTPUT
21
+ end
22
+
23
+ def actual_locales
24
+ all_locales.map { |l| l[:locale] }
25
+ end
26
+
27
+ def compare
28
+ I18n.available_locales - actual_locales
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UndeclaredLocaleFiles < BaseChecker
4
+ include LocaleCheckerHelper
5
+
6
+ def report
7
+ diff = compare
8
+ format_error_message(diff) unless diff.empty?
9
+ end
10
+
11
+ private
12
+
13
+ def format_error_message(locales)
14
+ <<~OUTPUT.chomp
15
+ \e[31m[ERROR]\e[0m Undeclared locale file(s) detected for:
16
+
17
+ #{locales.join("\n\n")}
18
+
19
+ \e[1mEither declare these locale files or remove them from your locale files directory.\e[22m
20
+ OUTPUT
21
+ end
22
+
23
+ def actual_locales
24
+ all_locales.map { |l| l[:locale] }
25
+ end
26
+
27
+ def compare
28
+ actual_locales - I18n.available_locales
29
+ end
30
+ end
@@ -4,9 +4,11 @@ module RailsTranslationManager
4
4
  class LocaleChecker
5
5
  attr_reader :locale_path
6
6
 
7
- CHECKER_CLASSES = [MissingEnglishLocales,
7
+ CHECKER_CLASSES = [IncompatiblePlurals,
8
+ MissingDeclaredLocales,
9
+ MissingEnglishLocales,
8
10
  MissingForeignLocales,
9
- IncompatiblePlurals].freeze
11
+ UndeclaredLocaleFiles].freeze
10
12
 
11
13
  def initialize(locale_path)
12
14
  @locale_path = locale_path
@@ -1,3 +1,3 @@
1
1
  module RailsTranslationManager
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -6,10 +6,12 @@ require "rails-i18n"
6
6
 
7
7
  require "rails_translation_manager/locale_checker/base_checker"
8
8
  require "rails_translation_manager/locale_checker/locale_checker_helper"
9
+ require "rails_translation_manager/locale_checker/missing_declared_locales"
9
10
  require "rails_translation_manager/locale_checker/missing_foreign_locales"
10
11
  require "rails_translation_manager/locale_checker/missing_english_locales"
11
12
  require "rails_translation_manager/locale_checker/plural_forms"
12
13
  require "rails_translation_manager/locale_checker/incompatible_plurals"
14
+ require "rails_translation_manager/locale_checker/undeclared_locale_files"
13
15
  require "rails_translation_manager/locale_checker/all_locales"
14
16
  require "rails_translation_manager/locale_checker"
15
17
  require "rails_translation_manager/cleaner"
@@ -1,6 +1,6 @@
1
- require "rails_translation_manager"
2
1
  require "i18n/tasks/cli"
3
- require_relative "../tasks/translation_helper"
2
+ require_relative "../rails_translation_manager"
3
+ require_relative "../rails_translation_manager/i18n_tasks_option_parser"
4
4
 
5
5
  namespace :translation do
6
6
 
@@ -42,7 +42,7 @@ namespace :translation do
42
42
  )
43
43
  importer.import
44
44
 
45
- puts "Imported CSV from: #{csv_path} to #{import_dir}"
45
+ puts "\nImported CSV from: #{csv_path} to #{import_dir}"
46
46
  end
47
47
 
48
48
  namespace :import do
@@ -61,19 +61,27 @@ namespace :translation do
61
61
  importer.import
62
62
  end
63
63
 
64
- puts "Imported all CSVs from: #{directory} to #{import_dir}"
64
+ puts "\nImported all CSVs from: #{directory} to #{import_dir}"
65
65
  end
66
66
  end
67
67
 
68
68
  desc "Add missing translations"
69
69
  task(:add_missing, [:locale] => [:environment]) do |t, args|
70
- I18n::Tasks::CLI.start(TranslationHelper.new(["add-missing", "--nil-value"], args[:locale]).with_optional_locale)
70
+ option_parser = RailsTranslationManager::I18nTasksOptionParser.new(
71
+ ["add-missing", "--nil-value"], args[:locale]
72
+ ).with_optional_locale
73
+
74
+ I18n::Tasks::CLI.start(option_parser)
71
75
  RailsTranslationManager::Cleaner.new(Rails.root.join("config", "locales")).clean
72
76
  end
73
77
 
74
78
  desc "Normalize translations"
75
79
  task(:normalize, [:locale] => [:environment]) do |t, args|
76
- I18n::Tasks::CLI.start(TranslationHelper.new(["normalize"], args[:locale]).with_optional_locale)
80
+ option_parser = RailsTranslationManager::I18nTasksOptionParser.new(
81
+ ["normalize"], args[:locale]
82
+ ).with_optional_locale
83
+
84
+ I18n::Tasks::CLI.start(option_parser)
77
85
  RailsTranslationManager::Cleaner.new(Rails.root.join("config", "locales")).clean
78
86
  end
79
87
  end
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "rails-i18n"
22
21
  spec.add_dependency "activesupport"
22
+ spec.add_dependency "csv", "~> 3.2"
23
23
  spec.add_dependency "i18n-tasks"
24
+ spec.add_dependency "rails-i18n"
24
25
 
25
26
  spec.add_development_dependency "bundler"
26
27
  spec.add_development_dependency "rake", "~> 10.0"
@@ -6,3 +6,4 @@ world_location.sentiment,:whatever,:bof
6
6
  shared.price,123,123
7
7
  shared.key1,is true,true
8
8
  shared.key2,is false,false
9
+ ,,
@@ -0,0 +1,4 @@
1
+ key,source,translation
2
+ unpublishing.title,The page you're looking for is no longer available,Ձեր փնտրած էջն այլևս հասանելի չէ
3
+ working_group.contact_details,Contact details,Կոնտակտային տվյալներ
4
+ working_group.policies,Policies,Քաղաքականություններ
@@ -4,6 +4,31 @@ require "tmpdir"
4
4
  RSpec.describe RailsTranslationManager::Importer do
5
5
  let(:import_directory) { Dir.mktmpdir }
6
6
 
7
+ it "imports CSV containing a byte order mark" do
8
+ importer = described_class.new(
9
+ locale: "hy",
10
+ csv_path: "spec/locales/importer/hy_with_byte_order_mark.csv",
11
+ import_directory: import_directory,
12
+ multiple_files_per_language: false
13
+ )
14
+ importer.import
15
+
16
+ expect(File).to exist(import_directory + "/hy.yml")
17
+ end
18
+
19
+ it "doesn't try to import a row with a blank key" do
20
+ importer = described_class.new(
21
+ locale: "hy",
22
+ csv_path: "spec/locales/importer/fr.csv",
23
+ import_directory: import_directory,
24
+ multiple_files_per_language: false
25
+ )
26
+
27
+ expect { importer.import }.to output(
28
+ "Invalid row: #<CSV::Row \"key\":nil \"source\":nil \"translation\":nil> for csv_path: spec/locales/importer/fr.csv\n"
29
+ ).to_stdout
30
+ end
31
+
7
32
  context "when there is one locale file per language" do
8
33
  let(:yaml_translation_data) { YAML.load_file(import_directory + "/fr.yml")["fr"] }
9
34
 
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe MissingDeclaredLocales do
6
+ let(:all_locales) do
7
+ [
8
+ {
9
+ locale: :en,
10
+ keys: %w[browse.same_key]
11
+ },
12
+ {
13
+ locale: :cy,
14
+ keys: %w[browse.same_key]
15
+ }
16
+ ]
17
+ end
18
+
19
+ context "when there are missing locales" do
20
+ before do
21
+ I18n.available_locales = [:en, :cy, :pt]
22
+ end
23
+
24
+ it "outputs the missing locales" do
25
+ expect(described_class.new(all_locales).report)
26
+ .to eq(
27
+ <<~OUTPUT.chomp
28
+ \e[31m[ERROR]\e[0m No locale files detected for:
29
+
30
+ pt
31
+
32
+ \e[1mEither create these locale files or remove these locales from your I18n `available_locales` config.\e[22m
33
+ OUTPUT
34
+ )
35
+ end
36
+ end
37
+
38
+ context "when there aren't missing locales" do
39
+ before do
40
+ I18n.available_locales = [:en, :cy]
41
+ end
42
+
43
+ it "outputs nil" do
44
+ expect(described_class.new(all_locales).report)
45
+ .to be_nil
46
+ end
47
+ end
48
+ end
@@ -44,7 +44,7 @@ RSpec.describe MissingEnglishLocales do
44
44
  ]
45
45
  end
46
46
 
47
- it 'outputs nil' do
47
+ it "outputs nil" do
48
48
  expect(described_class.new(all_locales).report)
49
49
  .to be_nil
50
50
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe UndeclaredLocaleFiles do
6
+ let(:all_locales) do
7
+ [
8
+ {
9
+ locale: :en,
10
+ keys: %w[browse.same_key]
11
+ },
12
+ {
13
+ locale: :cy,
14
+ keys: %w[browse.same_key]
15
+ }
16
+ ]
17
+ end
18
+
19
+ context "when there are undeclared locales" do
20
+ before do
21
+ I18n.available_locales = [:en]
22
+ end
23
+
24
+ it "outputs the missing locales" do
25
+ expect(described_class.new(all_locales).report)
26
+ .to eq(
27
+ <<~OUTPUT.chomp
28
+ \e[31m[ERROR]\e[0m Undeclared locale file(s) detected for:
29
+
30
+ cy
31
+
32
+ \e[1mEither declare these locale files or remove them from your locale files directory.\e[22m
33
+ OUTPUT
34
+ )
35
+ end
36
+ end
37
+
38
+ context "when there aren't undeclared locales" do
39
+ before do
40
+ I18n.available_locales = [:en, :cy]
41
+ end
42
+
43
+ it "outputs nil" do
44
+ expect(described_class.new(all_locales).report)
45
+ .to be_nil
46
+ end
47
+ end
48
+ end
@@ -15,7 +15,7 @@ describe "rake tasks" do
15
15
 
16
16
  it "outputs to stdout" do
17
17
  expect { task.execute(csv_path: csv_path) }
18
- .to output("Imported CSV from: #{csv_path} to #{Rails.root.join("config", "locales")}\n")
18
+ .to output("\nImported CSV from: #{csv_path} to #{Rails.root.join("config", "locales")}\n")
19
19
  .to_stdout
20
20
  end
21
21
 
@@ -39,20 +39,24 @@ describe "rake tasks" do
39
39
 
40
40
  it "outputs to stdout" do
41
41
  expect { task.execute(csv_directory: csv_directory) }
42
- .to output("Imported all CSVs from: #{csv_directory} to #{Rails.root.join("config", "locales")}\n")
42
+ .to output("\nImported all CSVs from: #{csv_directory} to #{Rails.root.join("config", "locales")}\n")
43
43
  .to_stdout
44
44
  end
45
45
 
46
46
  it "calls the importer class for each target path" do
47
47
  task.execute(csv_directory: csv_directory, multiple_files_per_language: true)
48
-
49
- expect(RailsTranslationManager::Importer)
50
- .to have_received(:new)
51
- .with(locale: "fr",
52
- csv_path: "spec/locales/importer/fr.csv",
53
- import_directory: Rails.root.join("config", "locales"),
54
- multiple_files_per_language: true)
55
- expect(importer_instance).to have_received(:import)
48
+ import_paths = Dir["spec/locales/importer/*.csv"]
49
+
50
+ import_paths.each do |csv_path|
51
+ expect(RailsTranslationManager::Importer)
52
+ .to have_received(:new)
53
+ .with(locale: File.basename(csv_path, ".csv"),
54
+ csv_path: csv_path,
55
+ import_directory: Rails.root.join("config", "locales"),
56
+ multiple_files_per_language: true)
57
+ end
58
+
59
+ expect(importer_instance).to have_received(:import).exactly(import_paths.count)
56
60
  end
57
61
  end
58
62
 
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_translation_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edd Sowden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-03 00:00:00.000000000 Z
11
+ date: 2021-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails-i18n
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,21 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activesupport
28
+ name: csv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n-tasks
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -39,7 +53,7 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: i18n-tasks
56
+ name: rails-i18n
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -143,24 +157,27 @@ files:
143
157
  - lib/rails_translation_manager.rb
144
158
  - lib/rails_translation_manager/cleaner.rb
145
159
  - lib/rails_translation_manager/exporter.rb
160
+ - lib/rails_translation_manager/i18n_tasks_option_parser.rb
146
161
  - lib/rails_translation_manager/importer.rb
147
162
  - lib/rails_translation_manager/locale_checker.rb
148
163
  - lib/rails_translation_manager/locale_checker/all_locales.rb
149
164
  - lib/rails_translation_manager/locale_checker/base_checker.rb
150
165
  - lib/rails_translation_manager/locale_checker/incompatible_plurals.rb
151
166
  - lib/rails_translation_manager/locale_checker/locale_checker_helper.rb
167
+ - lib/rails_translation_manager/locale_checker/missing_declared_locales.rb
152
168
  - lib/rails_translation_manager/locale_checker/missing_english_locales.rb
153
169
  - lib/rails_translation_manager/locale_checker/missing_foreign_locales.rb
154
170
  - lib/rails_translation_manager/locale_checker/plural_forms.rb
171
+ - lib/rails_translation_manager/locale_checker/undeclared_locale_files.rb
155
172
  - lib/rails_translation_manager/railtie.rb
156
173
  - lib/rails_translation_manager/version.rb
157
174
  - lib/rails_translation_manager/yaml_writer.rb
158
175
  - lib/tasks/translation.rake
159
- - lib/tasks/translation_helper.rb
160
176
  - rails_translation_manager.gemspec
161
177
  - spec/locales/cleaner/clean.yml
162
178
  - spec/locales/cleaner/with_whitespace.yml
163
179
  - spec/locales/importer/fr.csv
180
+ - spec/locales/importer/hy_with_byte_order_mark.csv
164
181
  - spec/locales/in_sync/cy/browse.yml
165
182
  - spec/locales/in_sync/en/browse.yml
166
183
  - spec/locales/out_of_sync/cy.yml
@@ -170,9 +187,11 @@ files:
170
187
  - spec/rails_translation_manager/locale_checker/all_locales_spec.rb
171
188
  - spec/rails_translation_manager/locale_checker/incompatible_plurals_spec.rb
172
189
  - spec/rails_translation_manager/locale_checker/locale_checker_helper_spec.rb
190
+ - spec/rails_translation_manager/locale_checker/missing_declared_locales_spec.rb
173
191
  - spec/rails_translation_manager/locale_checker/missing_english_locales_spec.rb
174
192
  - spec/rails_translation_manager/locale_checker/missing_foreign_locales_spec.rb
175
193
  - spec/rails_translation_manager/locale_checker/plural_forms_spec.rb
194
+ - spec/rails_translation_manager/locale_checker/undeclared_locale_files_spec.rb
176
195
  - spec/rails_translation_manager/locale_checker_spec.rb
177
196
  - spec/spec_helper.rb
178
197
  - spec/support/tasks.rb
@@ -208,6 +227,7 @@ test_files:
208
227
  - spec/locales/cleaner/clean.yml
209
228
  - spec/locales/cleaner/with_whitespace.yml
210
229
  - spec/locales/importer/fr.csv
230
+ - spec/locales/importer/hy_with_byte_order_mark.csv
211
231
  - spec/locales/in_sync/cy/browse.yml
212
232
  - spec/locales/in_sync/en/browse.yml
213
233
  - spec/locales/out_of_sync/cy.yml
@@ -217,9 +237,11 @@ test_files:
217
237
  - spec/rails_translation_manager/locale_checker/all_locales_spec.rb
218
238
  - spec/rails_translation_manager/locale_checker/incompatible_plurals_spec.rb
219
239
  - spec/rails_translation_manager/locale_checker/locale_checker_helper_spec.rb
240
+ - spec/rails_translation_manager/locale_checker/missing_declared_locales_spec.rb
220
241
  - spec/rails_translation_manager/locale_checker/missing_english_locales_spec.rb
221
242
  - spec/rails_translation_manager/locale_checker/missing_foreign_locales_spec.rb
222
243
  - spec/rails_translation_manager/locale_checker/plural_forms_spec.rb
244
+ - spec/rails_translation_manager/locale_checker/undeclared_locale_files_spec.rb
223
245
  - spec/rails_translation_manager/locale_checker_spec.rb
224
246
  - spec/spec_helper.rb
225
247
  - spec/support/tasks.rb