rails_translation_manager 1.1.2 → 1.4.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: 6e7ef3e878a1b5ffc7ae48ad8f683887e79073b3f34a5a3439656f5f7abb6e20
4
- data.tar.gz: 2aac75f9cae7275812da73da0973de515baf0d78eb63e64182e683f8717696ca
3
+ metadata.gz: 328572b2e2f76e544577abf6e4294fbc18b39324be6804a41107d7961e3545dd
4
+ data.tar.gz: cff1f18b58513f7fc12b8b2473aabf1f24fa5841999935ce4288d1a3bb7bd0ec
5
5
  SHA512:
6
- metadata.gz: f0b2fa6abaadb21ea912976b1092c6459ea0194a8e19092c522e38224da218395d230936d0d4a0aa830e3d37bd9f6eb6ac1e4909bd5406369c0aa99d1a4d13f4
7
- data.tar.gz: 0dbbefe8208052b63f5ddc80734340ed7985b199f9113aaf5457c48d50e34e889e7379a20c61963b002343f4fb0d35aacc0307b442a15a2b7fbdea77c25349dd
6
+ metadata.gz: 2f3f6c481946426f3be7f562dc63487c5b29d16000b309980f581bf08f8089aa17a909389c0caf72be0a719f787ce4453d3cd91f31987a110b953ea4b7aa8c5f
7
+ data.tar.gz: 4cedacbcb032ca11e17b64bd2c699c7caf802062b768c129d43ef52b0f11e064b7f8990596007f58a020ea053fb5ecb374f99d7980b215f669d3e5673c1e6af5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.4.0
2
+
3
+ Add `skip_validation` parameter to `LocaleChecker`. https://github.com/alphagov/rails_translation_manager/pull/35
4
+
5
+ ## 1.3.0
6
+
7
+ Add remove-unused task wrapper. https://github.com/alphagov/rails_translation_manager/pull/32
8
+
9
+ ## 1.2.0
10
+
11
+ Add two new checker classes. https://github.com/alphagov/rails_translation_manager/pull/30
12
+
13
+ ## 1.1.3
14
+
15
+ Handle importing files that contain rows with a blank "key". https://github.com/alphagov/rails_translation_manager/pull/28
16
+
1
17
  ## 1.1.2
2
18
 
3
19
  Handle importing files that contain Byte Order Marks. https://github.com/alphagov/rails_translation_manager/pull/27
@@ -15,7 +15,9 @@ class RailsTranslationManager::Importer
15
15
  end
16
16
 
17
17
  def import
18
- csv = CSV.read(csv_path, encoding: "bom|utf-8", 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*/)
@@ -3,8 +3,9 @@
3
3
  class AllLocales
4
4
  attr_reader :locale_path
5
5
 
6
- def initialize(locale_path)
6
+ def initialize(locale_path, skip_validation = [])
7
7
  @locale_path = locale_path
8
+ @skip_validation = skip_validation
8
9
  end
9
10
 
10
11
  def generate
@@ -15,12 +16,12 @@ class AllLocales
15
16
  paths.flat_map do |locale_group|
16
17
  {
17
18
  locale: locale_group[:locale],
18
- keys: all_keys_for_locale(locale_group)
19
+ keys: all_keys_for_locale(locale_group),
19
20
  }
20
21
  end
21
22
  end
22
23
 
23
- private
24
+ private
24
25
 
25
26
  def locale_file_paths
26
27
  I18n.available_locales.map do |locale|
@@ -43,6 +44,13 @@ class AllLocales
43
44
  def keys_from_file(locale_hash: nil, key_chain: nil, locale_keys: [], file_path: nil)
44
45
  locale_hash ||= YAML.load_file(file_path)
45
46
  keys = locale_hash.keys
47
+
48
+ keys.reject! do |key|
49
+ key_chain && @skip_validation.map { |prefix|
50
+ "#{key_chain}.#{key}".start_with? ".#{key_chain.split('.')[1]}.#{prefix}"
51
+ }.any?
52
+ end
53
+
46
54
  keys.each do |key|
47
55
  if locale_hash.fetch(key).is_a?(Hash)
48
56
  keys_from_file(locale_hash: locale_hash.fetch(key), key_chain: "#{key_chain}.#{key}", locale_keys: locale_keys)
@@ -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,12 +4,15 @@ 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
- def initialize(locale_path)
13
+ def initialize(locale_path, skip_validation = [])
12
14
  @locale_path = locale_path
15
+ @skip_validation = skip_validation
13
16
  end
14
17
 
15
18
  def validate_locales
@@ -19,7 +22,7 @@ module RailsTranslationManager
19
22
  false
20
23
  end
21
24
 
22
- private
25
+ private
23
26
 
24
27
  def output_result
25
28
  errors = checker_errors.compact
@@ -44,7 +47,7 @@ module RailsTranslationManager
44
47
  end
45
48
 
46
49
  def all_locales
47
- @all_locales ||= AllLocales.new(locale_path).generate
50
+ @all_locales ||= AllLocales.new(locale_path, @skip_validation).generate
48
51
  end
49
52
  end
50
53
  end
@@ -1,3 +1,3 @@
1
1
  module RailsTranslationManager
2
- VERSION = "1.1.2"
2
+ VERSION = "1.4.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"
@@ -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,7 +61,7 @@ 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
 
@@ -84,4 +84,14 @@ namespace :translation do
84
84
  I18n::Tasks::CLI.start(option_parser)
85
85
  RailsTranslationManager::Cleaner.new(Rails.root.join("config", "locales")).clean
86
86
  end
87
+
88
+ desc "Remove unused keys"
89
+ task(:remove_unused, [:locale] => [:environment]) do |t, args|
90
+ option_parser = RailsTranslationManager::I18nTasksOptionParser.new(
91
+ ["remove-unused"], args[:locale]
92
+ ).with_optional_locale
93
+
94
+ I18n::Tasks::CLI.start(option_parser)
95
+ RailsTranslationManager::Cleaner.new(Rails.root.join("config", "locales")).clean
96
+ end
87
97
  end
@@ -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
+ ,,
@@ -16,6 +16,19 @@ RSpec.describe RailsTranslationManager::Importer do
16
16
  expect(File).to exist(import_directory + "/hy.yml")
17
17
  end
18
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
+
19
32
  context "when there is one locale file per language" do
20
33
  let(:yaml_translation_data) { YAML.load_file(import_directory + "/fr.yml")["fr"] }
21
34
 
@@ -21,4 +21,28 @@ RSpec.describe AllLocales do
21
21
  .to raise_error(AllLocales::NoLocaleFilesFound, 'No locale files found for the supplied path')
22
22
  end
23
23
  end
24
+
25
+ context "when some keys are excluded" do
26
+ it "generates an array of hashes without those keys" do
27
+ expect(described_class.new("spec/locales/out_of_sync/*.yml", skip_validation = %w{wrong_plural.one}).generate)
28
+ .to eq(
29
+ [
30
+ { keys: %w[test wrong_plural wrong_plural.zero], locale: :en },
31
+ { keys: %w[other_test], locale: :cy }
32
+ ]
33
+ )
34
+ end
35
+ end
36
+
37
+ context "when a key prefix is excluded" do
38
+ it "generates an array of hashes without those keys" do
39
+ expect(described_class.new("spec/locales/out_of_sync/*.yml", skip_validation = %w{wrong_plural}).generate)
40
+ .to eq(
41
+ [
42
+ { keys: %w[test], locale: :en },
43
+ { keys: %w[other_test], locale: :cy }
44
+ ]
45
+ )
46
+ end
47
+ end
24
48
  end
@@ -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,7 +39,7 @@ 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
 
@@ -106,6 +106,28 @@ describe "rake tasks" do
106
106
  end
107
107
  end
108
108
 
109
+ describe "translation:remove_unused", type: :task do
110
+ let(:task) { Rake::Task["translation:remove_unused"] }
111
+ let!(:cleaner_instance) { stub_cleaner }
112
+
113
+ before do
114
+ allow(I18n::Tasks::CLI).to receive(:start)
115
+ end
116
+
117
+ it "triggers Cleaner and allows to receive the right arguments" do
118
+ task.execute(locale_directory: "config/locales")
119
+ expect(RailsTranslationManager::Cleaner)
120
+ .to have_received(:new)
121
+ .with(Rails.root.join("config", "locales"))
122
+ expect(cleaner_instance).to have_received(:clean)
123
+ end
124
+
125
+ it "triggers i18n task and allows to receive the right arguments" do
126
+ task.execute
127
+ expect(I18n::Tasks::CLI).to have_received(:start).with(["remove-unused"])
128
+ end
129
+ end
130
+
109
131
  def stub_importer
110
132
  importer_instance = instance_double(RailsTranslationManager::Importer)
111
133
  allow(RailsTranslationManager::Importer).to receive(:new)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_translation_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.4.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-05 00:00:00.000000000 Z
11
+ date: 2022-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -164,9 +164,11 @@ files:
164
164
  - lib/rails_translation_manager/locale_checker/base_checker.rb
165
165
  - lib/rails_translation_manager/locale_checker/incompatible_plurals.rb
166
166
  - lib/rails_translation_manager/locale_checker/locale_checker_helper.rb
167
+ - lib/rails_translation_manager/locale_checker/missing_declared_locales.rb
167
168
  - lib/rails_translation_manager/locale_checker/missing_english_locales.rb
168
169
  - lib/rails_translation_manager/locale_checker/missing_foreign_locales.rb
169
170
  - lib/rails_translation_manager/locale_checker/plural_forms.rb
171
+ - lib/rails_translation_manager/locale_checker/undeclared_locale_files.rb
170
172
  - lib/rails_translation_manager/railtie.rb
171
173
  - lib/rails_translation_manager/version.rb
172
174
  - lib/rails_translation_manager/yaml_writer.rb
@@ -185,9 +187,11 @@ files:
185
187
  - spec/rails_translation_manager/locale_checker/all_locales_spec.rb
186
188
  - spec/rails_translation_manager/locale_checker/incompatible_plurals_spec.rb
187
189
  - spec/rails_translation_manager/locale_checker/locale_checker_helper_spec.rb
190
+ - spec/rails_translation_manager/locale_checker/missing_declared_locales_spec.rb
188
191
  - spec/rails_translation_manager/locale_checker/missing_english_locales_spec.rb
189
192
  - spec/rails_translation_manager/locale_checker/missing_foreign_locales_spec.rb
190
193
  - spec/rails_translation_manager/locale_checker/plural_forms_spec.rb
194
+ - spec/rails_translation_manager/locale_checker/undeclared_locale_files_spec.rb
191
195
  - spec/rails_translation_manager/locale_checker_spec.rb
192
196
  - spec/spec_helper.rb
193
197
  - spec/support/tasks.rb
@@ -233,9 +237,11 @@ test_files:
233
237
  - spec/rails_translation_manager/locale_checker/all_locales_spec.rb
234
238
  - spec/rails_translation_manager/locale_checker/incompatible_plurals_spec.rb
235
239
  - spec/rails_translation_manager/locale_checker/locale_checker_helper_spec.rb
240
+ - spec/rails_translation_manager/locale_checker/missing_declared_locales_spec.rb
236
241
  - spec/rails_translation_manager/locale_checker/missing_english_locales_spec.rb
237
242
  - spec/rails_translation_manager/locale_checker/missing_foreign_locales_spec.rb
238
243
  - spec/rails_translation_manager/locale_checker/plural_forms_spec.rb
244
+ - spec/rails_translation_manager/locale_checker/undeclared_locale_files_spec.rb
239
245
  - spec/rails_translation_manager/locale_checker_spec.rb
240
246
  - spec/spec_helper.rb
241
247
  - spec/support/tasks.rb