rails_translation_manager 1.1.1 → 1.3.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: 234b68e06027a3335015a5818993014afea669efe42729d87f4de40efd4dc487
4
- data.tar.gz: 54a16b4f583813415a6801c62fda5eb9ee603e84bf65206700c0bc33b295b238
3
+ metadata.gz: 7345dfdb610385c480d9434eda36b6be02c37617046eb0029393fe250a37fe4f
4
+ data.tar.gz: c7d42c8eeb4268b289f96377f5dcee785c97c8c67b1895c53cb7a9b4d984f99d
5
5
  SHA512:
6
- metadata.gz: 2ab59ec749b2827c011728fe5711c850099d379d9089c2d73fc112def0f02c994b58ed646f42fe106c2d15d56cd5aaad1db636d7afe1504740109b57baca3c2f
7
- data.tar.gz: 9313168a8704e4985ae4a57d89fb6bc8dc47a5ac2f1ae6cdd73686aeec0fe657617776620db6a65c0c45b708ec2de06dc390d546509198a836b6ff7a14212ca3
6
+ metadata.gz: 27a9d0906d1fff39bec4c877368c7118a9e95d209251433f2fa3890c270aeb9d58a2479153c768069aaf2834fd28b74b916f4a0ed36d58ec844a53c2a3cedf2f
7
+ data.tar.gz: d2ce1ff2d3e9253b2109d4ee6db895ff80476f39b881dc8a9775766936d4b2092429e036d5fe0ed1d9c995a02b2204f06ac6d5fcdfcd064a82fc08b8c3b43a8d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.3.0
2
+
3
+ Add remove-unused task wrapper. https://github.com/alphagov/rails_translation_manager/pull/32
4
+
5
+ ## 1.2.0
6
+
7
+ Add two new checker classes. https://github.com/alphagov/rails_translation_manager/pull/30
8
+
9
+ ## 1.1.3
10
+
11
+ Handle importing files that contain rows with a blank "key". https://github.com/alphagov/rails_translation_manager/pull/28
12
+
13
+ ## 1.1.2
14
+
15
+ Handle importing files that contain Byte Order Marks. https://github.com/alphagov/rails_translation_manager/pull/27
16
+
1
17
  ## 1.1.1
2
18
 
3
19
  Fix Rails Translation Manager / Rails naming clash for class. https://github.com/alphagov/rails_translation_manager/pull/26
@@ -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.1"
2
+ VERSION = "1.3.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
@@ -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
 
@@ -102,6 +106,28 @@ describe "rake tasks" do
102
106
  end
103
107
  end
104
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
+
105
131
  def stub_importer
106
132
  importer_instance = instance_double(RailsTranslationManager::Importer)
107
133
  allow(RailsTranslationManager::Importer).to receive(:new)
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.1
4
+ version: 1.3.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-04 00:00:00.000000000 Z
11
+ date: 2021-12-15 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
  - - ">="
@@ -150,9 +164,11 @@ files:
150
164
  - lib/rails_translation_manager/locale_checker/base_checker.rb
151
165
  - lib/rails_translation_manager/locale_checker/incompatible_plurals.rb
152
166
  - lib/rails_translation_manager/locale_checker/locale_checker_helper.rb
167
+ - lib/rails_translation_manager/locale_checker/missing_declared_locales.rb
153
168
  - lib/rails_translation_manager/locale_checker/missing_english_locales.rb
154
169
  - lib/rails_translation_manager/locale_checker/missing_foreign_locales.rb
155
170
  - lib/rails_translation_manager/locale_checker/plural_forms.rb
171
+ - lib/rails_translation_manager/locale_checker/undeclared_locale_files.rb
156
172
  - lib/rails_translation_manager/railtie.rb
157
173
  - lib/rails_translation_manager/version.rb
158
174
  - lib/rails_translation_manager/yaml_writer.rb
@@ -161,6 +177,7 @@ files:
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