Skip to content

Commit c4c8924

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

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
require "csv_importer/runner"
1313
require "csv_importer/config"
1414
require "csv_importer/dsl"
15+
require 'i18n'
16+
require "csv_importer/railtie" if defined?(Rails::Railtie)
1517

1618
# A class that includes CSVImporter inherit its DSL and methods.
1719
#

lib/csv_importer/locales/en.yml

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

lib/csv_importer/locales/fr.yml

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

0 commit comments

Comments
 (0)