csv-i18n 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.reek +11 -0
- data/CHANGELOG.md +12 -0
- data/README.md +25 -0
- data/lib/csv_i18n/ext.rb +9 -1
- data/lib/csv_i18n/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8166823b265db009d02c80b873f2febd25397185
|
4
|
+
data.tar.gz: f0be097c2902393f16274851099ed9fd25383f3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6667f0c44144199353cc5dd01adaac3a623097b64c427dc9d595e2f70706f5cb3fef1bb9197596948fc2e0b3a683ec9a0767cf10b2cd0cd570fc7edaeebe1f7
|
7
|
+
data.tar.gz: b9d81bd5a2b7edfc6f0f9e337a0158fc5085bc585fbf76400e0cdacc7c18b67d8589aba0e29c8ab6ccef95d79c80d4d0922e69be73375a093c54ed1c3b3b8a37
|
data/.reek
ADDED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# CSV::I18n
|
2
2
|
|
3
|
+
[](https://travis-ci.org/jhubert/csv-i18n)
|
4
|
+
|
3
5
|
This gem makes it possible to translate the parsing error messages returned from the CSV core module using the [I18n](https://github.com/svenfuchs/i18n) library.
|
4
6
|
|
5
7
|
## Installation
|
@@ -20,6 +22,29 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
## Usage
|
22
24
|
|
25
|
+
Here is a very basic example:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'csv'
|
29
|
+
require 'csv-i18n'
|
30
|
+
require 'i18n'
|
31
|
+
|
32
|
+
I18n.backend.store_translations(:en, csv: { exception: { unclosed_quoted_field: "custom error on line %{line_number}" } })
|
33
|
+
|
34
|
+
CSV.parse('"')
|
35
|
+
# lib/ruby/2.3.0/csv.rb:1898:in `block in shift': custom error on line 1 (CSV::MalformedCSVError)
|
36
|
+
# from lib/ruby/2.3.0/csv.rb:1805:in `loop'
|
37
|
+
# from lib/ruby/2.3.0/csv.rb:1805:in `shift'
|
38
|
+
# from lib/ruby/gems/2.3.0/gems/csv-i18n-0.1.1/lib/csv_i18n/ext.rb:6:in `shift'
|
39
|
+
# from lib/ruby/2.3.0/csv.rb:1747:in `each'
|
40
|
+
# from lib/ruby/2.3.0/csv.rb:1761:in `to_a'
|
41
|
+
# from lib/ruby/2.3.0/csv.rb:1761:in `read'
|
42
|
+
# from lib/ruby/2.3.0/csv.rb:1307:in `parse'
|
43
|
+
# from app.rb:7:in `<main>'
|
44
|
+
```
|
45
|
+
|
46
|
+
The library is not dependent on I18n, so you can include it before you start internationalizing your code. If I18n isn't found, it will just fall back to the default error messages. Same behavior if a translation or locale isn't found.
|
47
|
+
|
23
48
|
### Translations
|
24
49
|
|
25
50
|
Once you have installed the gem, you can add your translations to the language files like this:
|
data/lib/csv_i18n/ext.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
module CSVI18n
|
2
|
+
# Extension to the core ruby CSV class
|
2
3
|
module Ext
|
3
4
|
# Redefine the shift method to catch the exception and attempt to return
|
4
5
|
# a translated version of the error message using I18n.t
|
@@ -23,8 +24,10 @@ module CSVI18n
|
|
23
24
|
# unclosed_quoted_field:
|
24
25
|
# unquoted_fields_do_not_allow_r_or_n:
|
25
26
|
#
|
27
|
+
# :reek:FeatureEnvy because it's clear enough to leave as is
|
28
|
+
# :reek:NilCheck because we want certainty with the RegExp match
|
26
29
|
def translated_exception_message(msg)
|
27
|
-
return msg unless
|
30
|
+
return msg unless translatable?
|
28
31
|
|
29
32
|
# This will break if the CSV class changes it's error messages
|
30
33
|
matches = msg.match(/\s[ion]{2}?\s?\(?line (\d)\)?\.?/i)
|
@@ -38,6 +41,11 @@ module CSVI18n
|
|
38
41
|
)
|
39
42
|
end
|
40
43
|
|
44
|
+
# :reek:UtilityFunction for readability
|
45
|
+
def translatable?
|
46
|
+
defined?(::I18n) && ::I18n.locale_available?(::I18n.locale)
|
47
|
+
end
|
48
|
+
|
41
49
|
# :reek:UtilityFunction but could use parameterize('_') if rails is present
|
42
50
|
def keyify(msg)
|
43
51
|
msg.gsub(/[^a-z]+/i, ' ').strip.gsub(' ', '_').downcase
|
data/lib/csv_i18n/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv-i18n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Baker
|
@@ -60,7 +60,9 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
|
+
- ".reek"
|
63
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
64
66
|
- Gemfile
|
65
67
|
- LICENSE.txt
|
66
68
|
- README.md
|