rails_translation_manager 1.2.0 → 1.5.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: 1698a9fc00bbc68d6a07e11ab9a61fa009cdf11ab0556c1634e6586ea7f8043f
4
- data.tar.gz: b93eba9d7309f7e7184a548487592a684bc508afd87af84a6270a4abaa66f624
3
+ metadata.gz: 2d04ae993002360fe42edbf1b2fe1793e06c9c7adf9688975c07532290b3efd9
4
+ data.tar.gz: c6e504c5f2a89eae9b75cd7d5e4a68617cfeac7fe9d1f652967eb6371a8c03a6
5
5
  SHA512:
6
- metadata.gz: e8877d5f9a8b3baff02a1f62df4b5b8645297774cbb6fc583ff04b3c23c41298d093f8455253f803f7d21fbdbdf4a832b93d5b0582dda96df0a67eff49041be5
7
- data.tar.gz: bd9f568b8f7c330c005b9c897179d3a37d0f4a88dd718af7135544fd47cd7719329139799c4d050f2a85de20a134b62bb85ad771017002f408faac92a3097e34
6
+ metadata.gz: f4b3059c3908f6622d99b88bb2587275005084f72c5fb67e7623c04117e8159ae1c169633eb23232ff89009aa81066f4a1f212026430c8c3a36cd87861a467ba
7
+ data.tar.gz: c03ec40b44ccbfbe924a15f5efea3c9ff6e8469f56f638edacf4f24601f8ab40f4db181a09db46e119f103997ab481f8bdca1d09884bdde5a3aef8afaaabe236
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 1.5.0
2
+
3
+ Allow configuring the locale files directory with `RAILS_TRANSLATION_MANAGER_LOCALE_ROOT`. https://github.com/alphagov/rails_translation_manager/pull/38
4
+
5
+ ## 1.4.0
6
+
7
+ Add `skip_validation` parameter to `LocaleChecker`. https://github.com/alphagov/rails_translation_manager/pull/35
8
+
9
+ ## 1.3.0
10
+
11
+ Add remove-unused task wrapper. https://github.com/alphagov/rails_translation_manager/pull/32
12
+
1
13
  ## 1.2.0
2
14
 
3
15
  Add two new checker classes. https://github.com/alphagov/rails_translation_manager/pull/30
data/README.md CHANGED
@@ -1,35 +1,9 @@
1
1
  # Rails Translation Manager
2
2
 
3
- Support for translation workflow in rails applications.
4
-
5
- ## Nomenclature
6
-
7
- - **CSV**: comma separated values, a tabular data format which can be loaded into a
8
- spreadsheet package
9
- - **I18n**: an abbreviation of 'internationalisation', which is the process of adding
10
- support for multiple locales and languages to an application.
11
- `I18n` is also the name of a ruby gem which supports
12
- internationalisation in ruby applications.
13
- - **interpolation**: a technique used in I18n whereby data is inserted ("interpolated")
14
- into a translated string of text, for example `Hello %{name}`
15
- would become `Hello Sarah` if the variable `name` had the
16
- value `Sarah`.
17
- - **YAML**: YAML Ain't Markup Language, a textual data format used (in this case) for storing
18
- translation strings in rails applications
3
+ Rails Translation Manager (RTM) provides validation for locale files, and exposes several rake tasks which can be used to manage your translations. It can only be used on Rails apps.
19
4
 
20
5
  ## Technical documentation
21
6
 
22
- This gem provides a rails engine which adds rake tasks to manage translation
23
- files.
24
-
25
- It is intended to be included within your rails application by referencing it
26
- as a dependency in your `Gemfile`. You will then be able to use the rake tasks
27
- to manage your translation files and import/export translation strings.
28
-
29
- ### Dependencies
30
-
31
- To date it has only been tested with a rails 3.2.18 app, but it should work with later (and older) rails apps as well.
32
-
33
7
  ### Installation
34
8
 
35
9
  Add this line to your application's Gemfile:
@@ -38,207 +12,55 @@ Add this line to your application's Gemfile:
38
12
  gem 'rails_translation_manager'
39
13
  ```
40
14
 
41
- And then execute:
42
-
43
- $ bundle
44
-
45
- The gem depends on your rails application environment so it would not make
46
- sense to install this gem stand-alone.
47
-
48
- ### Running the application
49
-
50
- The primary usage of this gem is to support translation workflow.
51
-
52
- Once you have installed the gem into your application as described above, the
53
- expected usage is:
54
-
55
- 1. export translations to a CSV file using:
56
-
57
- ```
58
- rake translation:export:all[target_directory]
59
- ```
60
-
61
- or
62
-
63
- ```
64
- rake translation:export[target_directory,base_locale,target_locale]
65
- ```
66
-
67
- 2. send the appropriate CSV file to a translator
68
-
69
- 3. wait for translation to happen
70
-
71
- 4. receive translation file back, check it for [character encoding issues](https://github.com/alphagov/character_encoding_cleaner)
72
-
73
- 5. import the translation file using either:
74
-
75
- ```
76
- rake translation:import:all[source_directory]
77
- ```
78
-
79
- or
80
-
81
- ```
82
- rake translation:import[locale,path]
83
- ```
84
-
85
- this will generate `.yml` files for each translation
86
-
87
- 6. commit any changed `.yml` files
88
-
89
- ```
90
- git add config/locale
91
- git commit -m 'added new translations'
92
- ```
93
-
94
- ### Validation of interpolation keys
95
-
96
- A second feature supported by this library is the validation of interpolation
97
- keys.
98
-
99
- The I18n library supports 'interpolation' using the following syntax:
100
-
101
- ```yaml
102
- en:
103
- some_view:
104
- greeting: Hello, %{name}
105
- ```
106
-
107
- in this case the application can pass in the value of the `name` variable.
108
-
109
- If a translation includes an interpolation placeholder which has not been
110
- given a value by the application backend, then a runtime error will be raised.
111
-
112
- Unfortunately the placeholder variables sometimes get changed by mistake, or
113
- by translators who are not aware that they should not modify text within the
114
- special curly braces of the interpolation placeholder.
115
-
116
- This is obviously not great, and the validation task is intended to guard
117
- against it.
118
-
119
- It will check all translation files and report any which contain placeholders
120
- which do not exist in the english file.
121
-
122
- ```
123
- $ rake translation:validate
124
- Success! No unexpected interpolation keys found.
125
- ```
126
-
127
- ### Stealing translations from another app
128
-
129
- A third feature is the ability to "steal" one or more locales from an existing
130
- application. This functionality works by providing a mapping file, which defines
131
- how the translation keys in the the source app's files map to those in the app
132
- the gem is installed in.
133
-
134
- For example, given a locale file like this in the app to "steal" from:
135
-
136
- ```yaml
137
- es:
138
- document:
139
- type:
140
- case_study: Caso de estudio
141
- consultation: Consulta
142
- ```
143
-
144
- and a mapping file like this:
15
+ After a `bundle install`, this will make a number of rake tasks available to your application.
16
+ See the [Rake command reference](#rake-command-reference) below.
145
17
 
146
- ```yaml
147
- document.type: content_item.format
148
- ```
149
-
150
- running `rake translation:steal[es,../other_app,mapping_file_path.yml]` will
151
- result in the following locale file being created:
152
-
153
- ```yaml
154
- es:
155
- content_item:
156
- format:
157
- case_study: Caso de estudio
158
- consultation: Consulta
159
- ```
160
-
161
- The mapping file can live anywhere, as long as the full path (including filename)
162
- is given in the rake task invocation.
163
-
164
- The process will preserve data already in the output file if it is not
165
- referenced in the mapping, but will always override data belonging to keys
166
- that are in the mapping.
167
-
168
-
169
- ### Rake command reference
170
-
171
- #### Export a specific locale to CSV
172
-
173
- ```
174
- rake translation:export[directory,base_locale,target_locale]
175
- ```
176
-
177
- #### Export all locales to CSV files
178
-
179
- ```
180
- rake translation:export:all[directory]
181
- ```
182
-
183
- #### Import a specific locale CSV to YAML within the app
184
-
185
- ```
186
- rake translation:import[locale,path]
187
- ```
188
-
189
- #### Import all locale CSV files to YAML within the app
18
+ You will now also be able to run tests against your locale files to ensure that they are valid.
19
+ Create a test file as follows.
190
20
 
191
- ```
192
- rake translation:import:all[directory]
193
- ```
194
-
195
- ####
21
+ #### Minitest
196
22
 
197
- #### Regenerate all locales from the EN locale - run this after adding keys
198
-
199
- ```
200
- rake translation:regenerate[directory]
23
+ ```ruby
24
+ class LocalesValidationTest < ActiveSupport::TestCase
25
+ test "should validate all locale files" do
26
+ checker = RailsTranslationManager::LocaleChecker.new("config/locales/*.yml")
27
+ assert checker.validate_locales
28
+ end
29
+ end
201
30
  ```
202
31
 
203
- #### Check translation files for errors
32
+ #### RSpec
204
33
 
205
- ```
206
- rake translation:validate
34
+ ```ruby
35
+ RSpec.describe "locales files" do
36
+ it "should meet all locale validation requirements" do
37
+ checker = RailsTranslationManager::LocaleChecker.new("config/locales/*/*.yml")
38
+ expect(checker.validate_locales).to be_truthy
39
+ end
40
+ end
207
41
  ```
208
42
 
209
- #### Steal a specific locale file from another app
43
+ ### Running the test suite
210
44
 
211
- ```
212
- rake translation:steal[locale,source_app_path,mapping_file_path]
213
- ```
45
+ To run the test suite just run `bundle exec rake` from within the
46
+ `rails_translation_manager` directory.
214
47
 
215
- #### Steal all locale files from another app
48
+ ### Further documentation
216
49
 
217
- ```
218
- rake translation:steal:all[source_app_path,mapping_file_path]
219
- ```
50
+ - [Adding and editing plural forms](docs/adding-editing-plural-forms.md)
51
+ - [Rake command reference](docs/rake-command-reference.md)
52
+ - [Translating locale files](docs/translating-locale-files.md) using RTM
220
53
 
221
- ### Running the test suite
54
+ #### i18n-tasks
222
55
 
223
- To run the test suite just run `bundle exec rake` from within the
224
- `rails_translation_manager` directory.
56
+ RTM uses [i18n-tasks](https://github.com/glebm/i18n-tasks) under the hood.
57
+ For advanced tasks, refer to the i18n-tasks documentation:
225
58
 
226
- You will need to clone the repository locally and run `bundle install` the
227
- first time you do this, eg.:
59
+ - [Move / rename / merge keys](https://github.com/glebm/i18n-tasks#move--rename--merge-keys)
60
+ - [Delete keys](https://github.com/glebm/i18n-tasks#delete-keys)
228
61
 
229
- ```sh
230
- $ git clone git@github.com:alphagov/rails_translation_manager.git
231
- $ cd rails_translation_manager
232
- $ bundle install
233
- $ bundle exec rake
234
- ...
235
- ```
62
+ Read [why we architected RTM in this way](https://docs.google.com/document/d/1bao4KfXtZOwoUZ4ZGRSKi1TagKMq-zsy8xAc-2GZlgo/edit).
236
63
 
237
64
  ## Licence
238
65
 
239
66
  [MIT License](LICENSE.txt)
240
-
241
- ## Versioning policy
242
-
243
- We use [semantic versioning](http://semver.org/), and bump the version
244
- on master only. Please don't submit your own proposed version numbers.
@@ -0,0 +1,31 @@
1
+ # Adding a language's plural form
2
+
3
+ We maintain our own [plurals.rb](https://github.com/alphagov/rails_translation_manager/blob/master/config/locales/plurals.rb) configuration file. If adding a language to Rails Translation Manager, that doesn't exist in [rails-i18n](https://github.com/svenfuchs/rails-i18n), you may need to edit this file.
4
+
5
+ This config is needed to correctly distinguish the [plural rules](https://www.unicode.org/cldr/cldr-aux/charts/34/supplemental/language_plural_rules.html) for locales not included in `rails-i18n`, as it is needed for plural validation checks.
6
+
7
+ Note that there is currently a [duplicate list in `govuk_app_config](https://github.com/alphagov/govuk_app_config/blob/main/lib/govuk_app_config/govuk_i18n.rb). The intention is for this to be removed and for the Rails Translation Manager version to be the sole version.
8
+
9
+ ## Editing plural forms
10
+
11
+ Keys with incorrect plural forms are more complex to adjust. If there are keys with additional unnecessary plural forms, they can be deleted:
12
+
13
+ ```
14
+ sed -i '' '/^ \+one:/d' config/locales/xx.yml
15
+ ```
16
+
17
+ This commonly occurs for e.g. Chinese and Vietnamese which are expected to have only an `:other` and not a `:one` form.
18
+
19
+ Keys which need a plural form added can be automated with caution. If every key of a specific translation needs the new plural added, it can be done by adding a blank key before every `other:` key:
20
+
21
+ ```
22
+ perl -0777 -p -i -e 's/\n(\s+)other:/\n\1nmany:\n\1other:/g' config/locales/xx.yml
23
+ ```
24
+
25
+ Or if only a specific key needs the plural added:
26
+
27
+ ```
28
+ perl -0777 -p -i -e 's/(key_with_missing_plural:\n)(\s+)/\1\2zero:\n\2/g' config/locales/xx.yml
29
+ ```
30
+
31
+ This commonly occurs for Slavic languages and Arabic, for example, which have plural forms other than just `:one` and `:other`.
@@ -0,0 +1,84 @@
1
+ # Rake command reference
2
+
3
+ ±Export a specific locale to CSV:
4
+
5
+ ```
6
+ rake translation:export[directory,base_locale,target_locale]
7
+ ```
8
+
9
+ ±Export all locales to CSV files:
10
+
11
+ ```
12
+ rake translation:export:all[directory]
13
+ ```
14
+
15
+ > ± These tasks won't work in [multi-file setups](#multi-file-setups).
16
+
17
+ Import a specific locale CSV to YAML within the app:
18
+
19
+ ```
20
+ rake translation:import[locale,path,multiple_files_per_language]
21
+ ```
22
+
23
+ Import all locale CSV files to YAML within the app:
24
+
25
+ ```
26
+ rake translation:import:all[directory,multiple_files_per_language]
27
+ ```
28
+
29
+ > The `multiple_files_per_language` parameter is optional and defaults to `false`. See [multi-file setups](#multi-file-setups).
30
+
31
+ Add missing keys with placeholders:
32
+
33
+ ```
34
+ rake translation:add_missing
35
+ ```
36
+
37
+ Normalize your locales (remove whitespace, sort keys alphabetically, etc):
38
+
39
+ ```
40
+ rake translation:normalize
41
+ ```
42
+
43
+ Remove keys that are not used anywhere in your application:
44
+
45
+ ```
46
+ rake translation:remove_unused
47
+ ```
48
+
49
+ Sometimes RTM might remove keys that actually _are_ used by your application. This happens when the keys are referenced dynamically. You can make RTM ignore these keys by creating a `/config/i18n-tasks.yml` file with an `ignore_unused` key. For example:
50
+
51
+ ```yaml
52
+ ignore_unused:
53
+ - 'content_item.schema_name.*.{one,other,zero,few,many,two}'
54
+ - 'corporate_information_page.*'
55
+ - 'travel_advice.alert_status.*'
56
+ ```
57
+
58
+ ## Multi-file setups
59
+
60
+ Locale files can follow a single-file structure. For example:
61
+
62
+ ```
63
+ config/
64
+ locales/
65
+ en/
66
+ en.yml
67
+ fr/
68
+ fr.yml
69
+ ```
70
+
71
+ They can also follow a multi-file structure. For example:
72
+
73
+ ```
74
+ config/
75
+ locales/
76
+ en/
77
+ bar.yml
78
+ foo.yml
79
+ fr/
80
+ bar.yml
81
+ foo.yml
82
+ ```
83
+
84
+ Some tasks will work with both structures, but others are limited to only one structure type, due to lack of development time. Ideally, all tasks would work with both types.
@@ -0,0 +1,44 @@
1
+ # Translating locale files
2
+
3
+ This document explains how to use Rails Translation Manager to create locale files.
4
+
5
+ Once you have installed the gem into your application, the expected usage is:
6
+
7
+ 1. export translations to a CSV file using:
8
+
9
+ ```
10
+ rake translation:export:all[target_directory]
11
+ ```
12
+
13
+ or
14
+
15
+ ```
16
+ rake translation:export[target_directory,base_locale,target_locale]
17
+ ```
18
+
19
+ 2. send the appropriate CSV file to a translator
20
+
21
+ 3. wait for translation to happen
22
+
23
+ 4. receive translation file back
24
+
25
+ 5. import the translation file using either:
26
+
27
+ ```
28
+ rake translation:import:all[source_directory]
29
+ ```
30
+
31
+ or
32
+
33
+ ```
34
+ rake translation:import[locale,path]
35
+ ```
36
+
37
+ this will generate `.yml` files for each translation
38
+
39
+ 6. commit any changed `.yml` files
40
+
41
+ ```
42
+ git add config/locale
43
+ git commit -m 'added new translations'
44
+ ```
@@ -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)
@@ -24,7 +24,7 @@ class IncompatiblePlurals < BaseChecker
24
24
  plural_form = all_plural_forms[plurals[:locale]]
25
25
 
26
26
  if plural_form.blank?
27
- "- \e[31m[ERROR]\e[0m Please add plural form for '#{plurals[:locale]}' <link to future documentation>"
27
+ "- \e[31m[ERROR]\e[0m Please add plural form for '#{plurals[:locale]}'. See https://github.com/alphagov/rails_translation_manager/tree/master/docs/adding-editing-plural-forms.md"
28
28
  else
29
29
  incompatible_plurals(plurals, plural_form)
30
30
  end
@@ -10,8 +10,9 @@ module RailsTranslationManager
10
10
  MissingForeignLocales,
11
11
  UndeclaredLocaleFiles].freeze
12
12
 
13
- def initialize(locale_path)
13
+ def initialize(locale_path, skip_validation = [])
14
14
  @locale_path = locale_path
15
+ @skip_validation = skip_validation
15
16
  end
16
17
 
17
18
  def validate_locales
@@ -21,7 +22,7 @@ module RailsTranslationManager
21
22
  false
22
23
  end
23
24
 
24
- private
25
+ private
25
26
 
26
27
  def output_result
27
28
  errors = checker_errors.compact
@@ -46,7 +47,7 @@ module RailsTranslationManager
46
47
  end
47
48
 
48
49
  def all_locales
49
- @all_locales ||= AllLocales.new(locale_path).generate
50
+ @all_locales ||= AllLocales.new(locale_path, @skip_validation).generate
50
51
  end
51
52
  end
52
53
  end
@@ -1,3 +1,3 @@
1
1
  module RailsTranslationManager
2
- VERSION = "1.2.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rails_translation_manager/version"
4
- require "rails_translation_manager/railtie" if defined?(Rails)
4
+ require "rails"
5
+ require "rails_translation_manager/railtie"
5
6
  require "rails-i18n"
6
7
 
7
8
  require "rails_translation_manager/locale_checker/base_checker"
@@ -26,4 +27,12 @@ module RailsTranslationManager
26
27
  Dir["#{rails_i18n_path}/rails/pluralization/*.rb"],
27
28
  ["#{rails_translation_manager}/config/locales/plurals.rb"]
28
29
  )
30
+
31
+ def self.locale_root
32
+ if ENV["RAILS_TRANSLATION_MANAGER_LOCALE_ROOT"]
33
+ Pathname.new(ENV["RAILS_TRANSLATION_MANAGER_LOCALE_ROOT"])
34
+ else
35
+ Rails.root.join("config/locales")
36
+ end
37
+ end
29
38
  end
@@ -6,9 +6,10 @@ namespace :translation do
6
6
 
7
7
  desc "Export a specific locale to CSV."
8
8
  task :export, [:directory, :base_locale, :target_locale] => [:environment] do |t, args|
9
+ locale_root = RailsTranslationManager.locale_root
9
10
  FileUtils.mkdir_p(args[:directory]) unless File.exist?(args[:directory])
10
- base_locale = Rails.root.join("config", "locales", args[:base_locale] + ".yml")
11
- target_locale_path = Rails.root.join("config", "locales", args[:target_locale] + ".yml")
11
+ base_locale = locale_root.join(args[:base_locale] + ".yml")
12
+ target_locale_path = locale_root.join(args[:target_locale] + ".yml")
12
13
  exporter = RailsTranslationManager::Exporter.new(args[:directory], base_locale, target_locale_path)
13
14
  exporter.export
14
15
  end
@@ -16,10 +17,11 @@ namespace :translation do
16
17
  namespace :export do
17
18
  desc "Export all locales to CSV files."
18
19
  task :all, [:directory] => [:environment] do |t, args|
20
+ locale_root = RailsTranslationManager.locale_root
19
21
  directory = args[:directory] || "tmp/locale_csv"
20
22
  FileUtils.mkdir_p(directory) unless File.exist?(directory)
21
- locales = Dir[Rails.root.join("config", "locales", "*.yml")]
22
- base_locale = Rails.root.join("config", "locales", "en.yml")
23
+ locales = Dir[locale_root.join("*.yml")]
24
+ base_locale = locale_root.join("en.yml")
23
25
  target_locales = locales - [base_locale.to_s]
24
26
  target_locales.each do |target_locale_path|
25
27
  exporter = RailsTranslationManager::Exporter.new(directory, base_locale, target_locale_path)
@@ -31,37 +33,35 @@ namespace :translation do
31
33
 
32
34
  desc "Import a specific locale CSV to YAML within the app."
33
35
  task :import, [:csv_path, :multiple_files_per_language] => [:environment] do |t, args|
34
- import_dir = Rails.root.join("config", "locales")
35
36
  csv_path = args[:csv_path]
36
37
 
37
38
  importer = RailsTranslationManager::Importer.new(
38
39
  locale: File.basename(args[:csv_path], ".csv"),
39
40
  csv_path: csv_path,
40
- import_directory: Rails.root.join("config", "locales"),
41
+ import_directory: RailsTranslationManager.locale_root,
41
42
  multiple_files_per_language: args[:multiple_files_per_language] || false
42
43
  )
43
44
  importer.import
44
45
 
45
- puts "\nImported CSV from: #{csv_path} to #{import_dir}"
46
+ puts "\nImported CSV from: #{csv_path} to #{RailsTranslationManager.locale_root}"
46
47
  end
47
48
 
48
49
  namespace :import do
49
50
  desc "Import all locale CSV files to YAML within the app."
50
51
  task :all, [:csv_directory, :multiple_files_per_language] => [:environment] do |t, args|
51
52
  directory = args[:csv_directory] || "tmp/locale_csv"
52
- import_dir = Rails.root.join("config", "locales")
53
53
 
54
- Dir[Rails.root.join(directory, "*.csv")].each do |csv_path|
54
+ Dir["#{directory}/*.csv"].each do |csv_path|
55
55
  importer = RailsTranslationManager::Importer.new(
56
56
  locale: File.basename(csv_path, ".csv"),
57
57
  csv_path: csv_path,
58
- import_directory: import_dir,
58
+ import_directory: RailsTranslationManager.locale_root,
59
59
  multiple_files_per_language: args[:multiple_files_per_language] || false
60
60
  )
61
61
  importer.import
62
62
  end
63
63
 
64
- puts "\nImported all CSVs from: #{directory} to #{import_dir}"
64
+ puts "\nImported all CSVs from: #{directory} to #{RailsTranslationManager.locale_root}"
65
65
  end
66
66
  end
67
67
 
@@ -72,7 +72,7 @@ namespace :translation do
72
72
  ).with_optional_locale
73
73
 
74
74
  I18n::Tasks::CLI.start(option_parser)
75
- RailsTranslationManager::Cleaner.new(Rails.root.join("config", "locales")).clean
75
+ RailsTranslationManager::Cleaner.new(RailsTranslationManager.locale_root).clean
76
76
  end
77
77
 
78
78
  desc "Normalize translations"
@@ -82,6 +82,16 @@ namespace :translation do
82
82
  ).with_optional_locale
83
83
 
84
84
  I18n::Tasks::CLI.start(option_parser)
85
- RailsTranslationManager::Cleaner.new(Rails.root.join("config", "locales")).clean
85
+ RailsTranslationManager::Cleaner.new(RailsTranslationManager.locale_root).clean
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(RailsTranslationManager.locale_root).clean
86
96
  end
87
97
  end
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency "rails-i18n"
25
25
 
26
26
  spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "climate_control"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
28
29
  spec.add_development_dependency "minitest"
29
30
  spec.add_development_dependency "rspec"
@@ -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
@@ -82,9 +82,9 @@ RSpec.describe IncompatiblePlurals do
82
82
  <<~OUTPUT.chomp
83
83
  \e[31m[ERROR]\e[0m Incompatible plural forms, for:
84
84
 
85
- - \e[31m[ERROR]\e[0m Please add plural form for 'en' <link to future documentation>
85
+ - \e[31m[ERROR]\e[0m Please add plural form for 'en'. See https://github.com/alphagov/rails_translation_manager/tree/master/docs/adding-editing-plural-forms.md
86
86
 
87
- - \e[31m[ERROR]\e[0m Please add plural form for 'cy' <link to future documentation>
87
+ - \e[31m[ERROR]\e[0m Please add plural form for 'cy'. See https://github.com/alphagov/rails_translation_manager/tree/master/docs/adding-editing-plural-forms.md
88
88
 
89
89
  \e[1mIf the keys reported above are not plurals, rename them avoiding plural keywords: #{LocaleCheckerHelper::PLURAL_KEYS}\e[22m
90
90
  OUTPUT
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RailsTranslationManager do
4
+ describe ".locale_root" do
5
+ it "uses the value of the environment variable RAILS_TRANSLATION_MANAGER_LOCALE_ROOT if this is set" do
6
+ ClimateControl.modify(RAILS_TRANSLATION_MANAGER_LOCALE_ROOT: "/path/to/locales") do
7
+ expect(RailsTranslationManager.locale_root).to eq(Pathname.new("/path/to/locales"))
8
+ end
9
+ end
10
+
11
+ it "will fall back to Rails default locale location if the environment isn't set" do
12
+ allow(Rails).to receive(:root).and_return(Pathname.new("/rails"))
13
+
14
+ ClimateControl.modify(RAILS_TRANSLATION_MANAGER_LOCALE_ROOT: nil) do
15
+ expect(RailsTranslationManager.locale_root).to eq(Pathname.new("/rails/config/locales"))
16
+ end
17
+ end
18
+ end
19
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rails_translation_manager"
4
+ require "climate_control"
4
5
 
5
6
  RSpec.configure do |config|
6
7
  config.before do
@@ -3,9 +3,9 @@ require_relative "../../spec/support/tasks"
3
3
 
4
4
  describe "rake tasks" do
5
5
  before do
6
- fake_rails = double()
7
- fake_rails.stub(:root) { Pathname.new("spec") }
8
- stub_const("Rails", fake_rails)
6
+ allow(RailsTranslationManager)
7
+ .to receive(:locale_root)
8
+ .and_return(Pathname.new("spec/config/locales"))
9
9
  end
10
10
 
11
11
  describe "translation:import", type: :task do
@@ -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("\nImported CSV from: #{csv_path} to #{Rails.root.join("config", "locales")}\n")
18
+ .to output("\nImported CSV from: #{csv_path} to #{RailsTranslationManager.locale_root}\n")
19
19
  .to_stdout
20
20
  end
21
21
 
@@ -26,7 +26,7 @@ describe "rake tasks" do
26
26
  .to have_received(:new)
27
27
  .with(locale: "fr",
28
28
  csv_path: csv_path,
29
- import_directory: Rails.root.join("config", "locales"),
29
+ import_directory: RailsTranslationManager.locale_root,
30
30
  multiple_files_per_language: false)
31
31
  expect(importer_instance).to have_received(:import)
32
32
  end
@@ -34,12 +34,12 @@ describe "rake tasks" do
34
34
 
35
35
  describe "translation:import:all", type: :task do
36
36
  let(:task) { Rake::Task["translation:import:all"] }
37
- let(:csv_directory) { "locales/importer" }
37
+ let(:csv_directory) { "spec/locales/importer" }
38
38
  let!(:importer_instance) { stub_importer }
39
39
 
40
40
  it "outputs to stdout" do
41
41
  expect { task.execute(csv_directory: csv_directory) }
42
- .to output("\nImported all CSVs from: #{csv_directory} to #{Rails.root.join("config", "locales")}\n")
42
+ .to output("\nImported all CSVs from: #{csv_directory} to #{RailsTranslationManager.locale_root}\n")
43
43
  .to_stdout
44
44
  end
45
45
 
@@ -52,7 +52,7 @@ describe "rake tasks" do
52
52
  .to have_received(:new)
53
53
  .with(locale: File.basename(csv_path, ".csv"),
54
54
  csv_path: csv_path,
55
- import_directory: Rails.root.join("config", "locales"),
55
+ import_directory: RailsTranslationManager.locale_root,
56
56
  multiple_files_per_language: true)
57
57
  end
58
58
 
@@ -72,7 +72,7 @@ describe "rake tasks" do
72
72
  task.execute
73
73
  expect(RailsTranslationManager::Cleaner)
74
74
  .to have_received(:new)
75
- .with(Rails.root.join("config", "locales"))
75
+ .with(RailsTranslationManager.locale_root)
76
76
  expect(cleaner_instance).to have_received(:clean)
77
77
  end
78
78
 
@@ -96,7 +96,7 @@ describe "rake tasks" do
96
96
  task.execute(locale_directory: "config/locales")
97
97
  expect(RailsTranslationManager::Cleaner)
98
98
  .to have_received(:new)
99
- .with(Rails.root.join("config", "locales"))
99
+ .with(RailsTranslationManager.locale_root)
100
100
  expect(cleaner_instance).to have_received(:clean)
101
101
  end
102
102
 
@@ -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(RailsTranslationManager.locale_root)
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.2.0
4
+ version: 1.5.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-12-07 00:00:00.000000000 Z
11
+ date: 2022-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: climate_control
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -145,7 +159,6 @@ extra_rdoc_files: []
145
159
  files:
146
160
  - ".gitignore"
147
161
  - ".ruby-version"
148
- - ".travis.yml"
149
162
  - CHANGELOG.md
150
163
  - CONTRIBUTING.md
151
164
  - Gemfile
@@ -154,6 +167,9 @@ files:
154
167
  - README.md
155
168
  - Rakefile
156
169
  - config/locales/plurals.rb
170
+ - docs/adding-editing-plural-forms.md
171
+ - docs/rake-command-reference.md
172
+ - docs/translating-locale-files.md
157
173
  - lib/rails_translation_manager.rb
158
174
  - lib/rails_translation_manager/cleaner.rb
159
175
  - lib/rails_translation_manager/exporter.rb
@@ -193,6 +209,7 @@ files:
193
209
  - spec/rails_translation_manager/locale_checker/plural_forms_spec.rb
194
210
  - spec/rails_translation_manager/locale_checker/undeclared_locale_files_spec.rb
195
211
  - spec/rails_translation_manager/locale_checker_spec.rb
212
+ - spec/rails_translation_manager_spec.rb
196
213
  - spec/spec_helper.rb
197
214
  - spec/support/tasks.rb
198
215
  - spec/tasks/translation_spec.rb
@@ -243,6 +260,7 @@ test_files:
243
260
  - spec/rails_translation_manager/locale_checker/plural_forms_spec.rb
244
261
  - spec/rails_translation_manager/locale_checker/undeclared_locale_files_spec.rb
245
262
  - spec/rails_translation_manager/locale_checker_spec.rb
263
+ - spec/rails_translation_manager_spec.rb
246
264
  - spec/spec_helper.rb
247
265
  - spec/support/tasks.rb
248
266
  - spec/tasks/translation_spec.rb
data/.travis.yml DELETED
@@ -1,2 +0,0 @@
1
- rvm:
2
- - 2.1.4