Skip to content

Commit 72f0ae8

Browse files
committed
Translate strings in en/fr in lib/csv_importer/report_message.rb
1 parent 97ec554 commit 72f0ae8

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ You can handle exotic encodings with the `encoding` option.
444444
ImportUserCSV.new(content: "メール,氏名".encode('SJIS'), encoding: 'SJIS:UTF-8')
445445
```
446446

447+
### Internationalization
448+
449+
To translate a csv column header (e.g. for report messages), simply add this in a .yml file.
450+
451+
```yaml
452+
en:
453+
csv_importer:
454+
my_column: "My column"
455+
```
456+
457+
447458
## Development
448459
449460
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.

lib/csv_importer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require "csv_importer/runner"
1313
require "csv_importer/config"
1414
require "csv_importer/dsl"
15+
require "csv_importer/railtie" if defined?(Rails::Railtie)
1516

1617
# A class that includes CSVImporter inherit its DSL and methods.
1718
#

lib/csv_importer/locales/en.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
en:
2+
csv_importer:
3+
created_rows:
4+
one: "%{count} created"
5+
other: "%{count} created"
6+
create_skipped_rows:
7+
one: "%{count} create skipped"
8+
other: "%{count} create skipped"
9+
email: "email"
10+
failed_to_create_rows:
11+
one: "%{count} failed to create"
12+
other: "%{count} failed to create"
13+
failed_to_update_rows:
14+
one: "%{count} failed to update"
15+
other: "%{count} failed to update"
16+
report_aborted: "Import aborted"
17+
report_done: "Import completed: "
18+
report_in_progress: "Import in progress"
19+
report_invalid_header: "The following columns are required:"
20+
report_pending: "Import hasn't started yet"
21+
updated_rows:
22+
one: "%{count} updated"
23+
other: "%{count} updated"
24+
update_skipped_rows:
25+
one: "%{count} update skipped"
26+
other: "%{count} updates skipped"

lib/csv_importer/locales/fr.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
fr:
2+
csv_importer:
3+
created_rows:
4+
one: "%{count} créée"
5+
other: "%{count} créées"
6+
create_skipped_rows:
7+
one: "%{count} ignorée pour la création"
8+
other: "%{count} ignorées pour la création"
9+
email: "email"
10+
failed_to_create_rows:
11+
one: "%{count} échec de création"
12+
other: "%{count} échecs de création"
13+
failed_to_update_rows:
14+
one: "%{count} échec de modification"
15+
other: "%{count} échecs de modification"
16+
report_aborted: "Import avorté"
17+
report_done: "Import terminé : "
18+
report_in_progress: "Import en cours"
19+
report_invalid_header: "Les colonnes suivantes sont obligatoires :"
20+
report_pending: "L'import n'a pas encore commencé"
21+
updated_rows:
22+
one: "%{count} mise à jour"
23+
other: "%{count} mises à jour"
24+
update_skipped_rows:
25+
one: "%{count} ignorée pour la modification"
26+
other: "%{count} ignorées pour la modification"

lib/csv_importer/railtie.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module CSVImporter
2+
class Railtie < ::Rails::Railtie
3+
config.to_prepare do
4+
I18n.load_path.concat(Dir.glob(File.join(File.dirname(__FILE__), 'locales/*.yml')))
5+
end
6+
end
7+
end

lib/csv_importer/report_message.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,36 @@ def to_s
1818
private
1919

2020
def report_pending
21-
"Import hasn't started yet"
21+
I18n.t('csv_importer.report_pending')
2222
end
2323

2424
def report_in_progress
25-
"Import in progress"
25+
I18n.t('csv_importer.report_in_progress')
2626
end
2727

2828
def report_done
29-
"Import completed: " + import_details
29+
I18n.t('csv_importer.report_done') + import_details
3030
end
3131

3232
def report_invalid_header
33-
"The following columns are required: #{report.missing_columns.join(", ")}"
33+
I18n.t('csv_importer.report_invalid_header') + ' ' +
34+
report.missing_columns.map {|c| I18n.t("csv_importer.#{c}") }.join(", ")
3435
end
3536

3637
def report_invalid_csv_file
3738
report.parser_error
3839
end
3940

4041
def report_aborted
41-
"Import aborted"
42+
I18n.t('csv_importer.report_aborted')
4243
end
4344

4445
# Generate something like: "3 created. 4 updated. 1 failed to create. 2 failed to update."
4546
def import_details
4647
report.attributes
4748
.select { |name, _| name["_rows"] }
4849
.select { |_, instances| instances.size > 0 }
49-
.map { |bucket, instances| "#{instances.size} #{bucket.to_s.gsub('_rows', '').gsub('_', ' ')}" }
50+
.map { |bucket, instances| I18n.t("csv_importer.#{bucket.to_s}", count: instances.size) }
5051
.join(", ")
5152
end
5253

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
require 'csv_importer'
99

10+
I18n.load_path << Dir[File.expand_path("lib/csv_importer/locales") + "/*.yml"]
11+
1012
RSpec.configure do |c|
1113
c.example_status_persistence_file_path = "./spec/examples.txt"
1214
end

0 commit comments

Comments
 (0)