csv-i18n 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb2aff10e4006d4b7411bd91965cde7d8384dc51
4
- data.tar.gz: c0facabe99b4bd0b1e3763e0cda56e0409ea33fb
3
+ metadata.gz: 8166823b265db009d02c80b873f2febd25397185
4
+ data.tar.gz: f0be097c2902393f16274851099ed9fd25383f3a
5
5
  SHA512:
6
- metadata.gz: 66a9e00803667076dc8910b1e0d0445b63ae29791d13c00f47f9a488b7a1f52093093783e73e2f4f0f941d4b0f85026c01fa1fb3f925aba7e5dd5a680a6904cc
7
- data.tar.gz: 7057eb96bba63f71fbf7114cdb4f9e5c184c52b63972896fb6f3d2c8ce31412baedf9f88f6c2c1f8fb7eeb2bbf3159b4a3abc1c4b092edbe654966f899b3551c
6
+ metadata.gz: e6667f0c44144199353cc5dd01adaac3a623097b64c427dc9d595e2f70706f5cb3fef1bb9197596948fc2e0b3a683ec9a0767cf10b2cd0cd570fc7edaeebe1f7
7
+ data.tar.gz: b9d81bd5a2b7edfc6f0f9e337a0158fc5085bc585fbf76400e0cdacc7c18b67d8589aba0e29c8ab6ccef95d79c80d4d0922e69be73375a093c54ed1c3b3b8a37
data/.reek ADDED
@@ -0,0 +1,11 @@
1
+ "test":
2
+ IrresponsibleModule:
3
+ enabled: false
4
+ UtilityFunction:
5
+ enabled: false
6
+ Attribute:
7
+ enabled: false
8
+ FeatureEnvy:
9
+ enabled: false
10
+ DataClump:
11
+ enabled: false
@@ -0,0 +1,12 @@
1
+ ### 0.2.0
2
+
3
+ - Gracefully handle unavailable I18n locales
4
+ - Improve the README and add reek
5
+
6
+ ### 0.1.1
7
+
8
+ - Fix an issue with auto-require
9
+
10
+ ### 0.1.0
11
+
12
+ - Initial Release
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # CSV::I18n
2
2
 
3
+ [![Build Status](https://travis-ci.org/jhubert/csv-i18n.svg?branch=master)](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:
@@ -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 defined?(I18n)
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
@@ -1,3 +1,3 @@
1
1
  module CSVI18n
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.1
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