diff --git a/README.md b/README.md index 9e9937c..0b28b3e 100644 --- a/README.md +++ b/README.md @@ -444,6 +444,17 @@ You can handle exotic encodings with the `encoding` option. ImportUserCSV.new(content: "メール,氏名".encode('SJIS'), encoding: 'SJIS:UTF-8') ``` +### Internationalization + +To translate a csv column header (e.g. for report messages), simply add this in a .yml file. + +```yaml +en: + csv_importer: + my_column: "My column" +``` + + ## Development 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. diff --git a/lib/csv_importer.rb b/lib/csv_importer.rb index 3ccff5d..b034282 100644 --- a/lib/csv_importer.rb +++ b/lib/csv_importer.rb @@ -12,6 +12,7 @@ require "csv_importer/runner" require "csv_importer/config" require "csv_importer/dsl" +require "csv_importer/railtie" if defined?(Rails::Railtie) # A class that includes CSVImporter inherit its DSL and methods. # diff --git a/lib/csv_importer/locales/en.yml b/lib/csv_importer/locales/en.yml new file mode 100644 index 0000000..5a2514a --- /dev/null +++ b/lib/csv_importer/locales/en.yml @@ -0,0 +1,26 @@ +en: + csv_importer: + created_rows: + one: "%{count} created" + other: "%{count} created" + create_skipped_rows: + one: "%{count} create skipped" + other: "%{count} create skipped" + email: "email" + failed_to_create_rows: + one: "%{count} failed to create" + other: "%{count} failed to create" + failed_to_update_rows: + one: "%{count} failed to update" + other: "%{count} failed to update" + report_aborted: "Import aborted" + report_done: "Import completed: " + report_in_progress: "Import in progress" + report_invalid_header: "The following columns are required:" + report_pending: "Import hasn't started yet" + updated_rows: + one: "%{count} updated" + other: "%{count} updated" + update_skipped_rows: + one: "%{count} update skipped" + other: "%{count} updates skipped" diff --git a/lib/csv_importer/locales/fr.yml b/lib/csv_importer/locales/fr.yml new file mode 100644 index 0000000..db517f0 --- /dev/null +++ b/lib/csv_importer/locales/fr.yml @@ -0,0 +1,26 @@ +fr: + csv_importer: + created_rows: + one: "%{count} créée" + other: "%{count} créées" + create_skipped_rows: + one: "%{count} ignorée pour la création" + other: "%{count} ignorées pour la création" + email: "email" + failed_to_create_rows: + one: "%{count} échec de création" + other: "%{count} échecs de création" + failed_to_update_rows: + one: "%{count} échec de modification" + other: "%{count} échecs de modification" + report_aborted: "Import avorté" + report_done: "Import terminé : " + report_in_progress: "Import en cours" + report_invalid_header: "Les colonnes suivantes sont obligatoires :" + report_pending: "L'import n'a pas encore commencé" + updated_rows: + one: "%{count} mise à jour" + other: "%{count} mises à jour" + update_skipped_rows: + one: "%{count} ignorée pour la modification" + other: "%{count} ignorées pour la modification" diff --git a/lib/csv_importer/railtie.rb b/lib/csv_importer/railtie.rb new file mode 100644 index 0000000..20e2e1e --- /dev/null +++ b/lib/csv_importer/railtie.rb @@ -0,0 +1,7 @@ +module CSVImporter + class Railtie < ::Rails::Railtie + config.to_prepare do + I18n.load_path.concat(Dir.glob(File.join(File.dirname(__FILE__), 'locales/*.yml'))) + end + end +end diff --git a/lib/csv_importer/report_message.rb b/lib/csv_importer/report_message.rb index 6a8899f..e2febb4 100644 --- a/lib/csv_importer/report_message.rb +++ b/lib/csv_importer/report_message.rb @@ -18,19 +18,20 @@ def to_s private def report_pending - "Import hasn't started yet" + I18n.t('csv_importer.report_pending') end def report_in_progress - "Import in progress" + I18n.t('csv_importer.report_in_progress') end def report_done - "Import completed: " + import_details + I18n.t('csv_importer.report_done') + import_details end def report_invalid_header - "The following columns are required: #{report.missing_columns.join(", ")}" + I18n.t('csv_importer.report_invalid_header') + ' ' + + report.missing_columns.map {|c| I18n.t("csv_importer.#{c}") }.join(", ") end def report_invalid_csv_file @@ -38,7 +39,7 @@ def report_invalid_csv_file end def report_aborted - "Import aborted" + I18n.t('csv_importer.report_aborted') end # Generate something like: "3 created. 4 updated. 1 failed to create. 2 failed to update." @@ -46,7 +47,7 @@ def import_details report.attributes .select { |name, _| name["_rows"] } .select { |_, instances| instances.size > 0 } - .map { |bucket, instances| "#{instances.size} #{bucket.to_s.gsub('_rows', '').gsub('_', ' ')}" } + .map { |bucket, instances| I18n.t("csv_importer.#{bucket.to_s}", count: instances.size) } .join(", ") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 29319d9..35c9718 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,8 @@ require 'csv_importer' +I18n.load_path << Dir[File.expand_path("lib/csv_importer/locales") + "/*.yml"] + RSpec.configure do |c| c.example_status_persistence_file_path = "./spec/examples.txt" end