Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
26 changes: 26 additions & 0 deletions lib/csv_importer/locales/en.yml
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 26 additions & 0 deletions lib/csv_importer/locales/fr.yml
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions lib/csv_importer/railtie.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions lib/csv_importer/report_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,36 @@ 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
report.parser_error
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."
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

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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