i18n_csv 0.0.2 → 0.1.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: aebb9bd92b897b5fae2bf122018169da25a553e0
4
- data.tar.gz: 541f64428f4b71059bfe65dbf1d4578081dcab31
3
+ metadata.gz: 44295b382a5ff909e6948d766834a1981c495390
4
+ data.tar.gz: 1d0e5f21d6fcdc5dd70ec5879adb167a1c0faf83
5
5
  SHA512:
6
- metadata.gz: ec5d7f1e63390a5186f8f8e0e5cd619e272c214fc8b7b80692fee72166cd76efe631c401838770e6a83ba0fe1b7db24a39f74545c1c4252e5aca235018a7152a
7
- data.tar.gz: 2d33879cd7ed204f4ab0a0d9e3f90fbe347c6663f814d0769e4a211504718b602c9e82a7deaedd780efa45a049d78da5b8583817d431e3bddc8f5c602ab4e325
6
+ metadata.gz: df3ecda855e8b39dd9a69b22e6dda3e6645f58fac61edf3ad84276986c253222707f710bce56576ce16ac2c7717de5bb2f2b2b0d1c56c60f2fc10ac9c48a0e9f
7
+ data.tar.gz: 0a1b801293dd0e5f7acb1f02841fa634d02c1ed6e26795456e8788a600ebf89bab181ce1e31c0faa8055dda47c653657d0d5b4856adf270a379302245739a1c2
data/README.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  One extends standard I18n so that you could store your translations in a Comma-Separated Value files (*CSV*) in a **key-value** manner, where the **key** is a word or a phrase or even a poem if you wish. No limits here (except be aware to escape symbols so the CSV format is kept). And the **value** is the same text as the key but translated to a language, specified by a file name you are using. For example, you could write one line to a `sp.csv` file: `"hello!","hola!"` and use `t 'hello!'` with a *spanish locale* to get the `"hola!"` text.
4
4
 
5
+ ## A brief of history
6
+
7
+ See, I used to be a Magento developer for some time. And I really liked their localization approach. In comparison with the Rails, I18n technique with all those horrible YAML keys, Magento internationalization style looked clearly and... well, more usable.
8
+
9
+ If Magento did not find the translated key' value - it just put the key itself. And if you try to keep all your keys in English, you did not face huge trouble when you did not set the translation for some text. But with the default Rails configuration, you get the HTML saying *I could not find the translation for moo.foo.bar string!*. And that's horrible, I think.
10
+
11
+ So from my point of view, this approach (*CSV one*) is more flexible and user-friendly. So even customer could customize your locales.
12
+
5
13
  ## Installation
6
14
 
7
15
  Add this line to your application's Gemfile:
@@ -18,7 +26,27 @@ Or install it yourself as:
18
26
 
19
27
  ## Usage
20
28
 
21
- TODO: Write usage instructions here
29
+ Let's say you want Ukrainian localization for your application.
30
+
31
+ First you create file `config/locales/uk.csv` with the following content:
32
+
33
+ "hello","привіт"
34
+ "world","світ"
35
+ "hello, world!","хеллоу ворлд =3"
36
+
37
+ Then you add the `gem 'i18n_csv'` line to your `Gemfile` and run `bundle install`.
38
+
39
+ Now you are allowed to use the CSV translations in your application!
40
+
41
+ Here are some examples:
42
+
43
+ irb(main):002:0> I18n.locale = :uk
44
+ => :uk
45
+ irb(main):003:0> I18n.t 'hello'
46
+ => "привіт"
47
+ irb(main):004:0> I18n.t 'hello, world!'
48
+ => "хеллоу ворлд =3"
49
+
22
50
 
23
51
  ## Contributing
24
52
 
data/i18n_csv.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["shybovycha@gmail.com"]
11
11
  spec.description = %q{One extends standard I18n so that you could store your translations in a Comma-Separated Value files (CSV) in a key-value manner, where the key is a word or a phrase or even a poem if you wish. No limits here (except be aware to escape symbols so the CSV format is kept). And the value is the same text as the key but translated to a language, specified by a file name you are using (for example, you could write one line to a sp.csv file: `"hello!","hola!"` and use `t 'hello!'` with a spanish locale to get the "hola!" text).}
12
12
  spec.summary = %q{Extends I18n to use Magento-style CSV translation storage.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/shybovycha/i18n_csv"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.rubyforge_project = "i18n_csv"
@@ -6,14 +6,14 @@ module I18n
6
6
  def get_translations
7
7
  tmp_translations = translations()
8
8
 
9
- def moo(hash, prefix = nil)
9
+ def yaml_to_key_string(hash, prefix = nil)
10
10
  prefix = prefix.to_s
11
11
 
12
12
  if hash.is_a? Hash then
13
13
  hash.collect do |k, v|
14
14
  k = k.to_s
15
15
 
16
- moo(v, (prefix.blank? ? k : prefix + '.' + k)).flatten(1)
16
+ yaml_to_key_string(v, (prefix.blank? ? k : prefix + '.' + k)).flatten(1)
17
17
  end
18
18
  elsif hash.is_a? Array
19
19
  [ prefix, [ hash.reject { |e| e.blank? } ] ]
@@ -25,7 +25,7 @@ module I18n
25
25
  reload!
26
26
 
27
27
  tmp = tmp_translations.collect do |locale, values|
28
- [locale, Hash[*moo(values).flatten(1)]]
28
+ [locale, Hash[*yaml_to_key_string(values).flatten(1)]]
29
29
  end
30
30
 
31
31
  Hash[[*tmp]]
@@ -53,7 +53,4 @@ module I18n
53
53
  end
54
54
  end
55
55
  end
56
- end
57
-
58
- I18n::Backend::Simple.include(I18n::Backend::CSV18n)
59
- I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.csv')]
56
+ end
@@ -1,59 +1,4 @@
1
- require 'csv'
2
-
3
- module I18n
4
- module Backend
5
- module CSV18n
6
- def get_translations
7
- tmp_translations = translations()
8
-
9
- def yaml_to_key_string(hash, prefix = nil)
10
- prefix = prefix.to_s
11
-
12
- if hash.is_a? Hash then
13
- hash.collect do |k, v|
14
- k = k.to_s
15
-
16
- yaml_to_key_string(v, (prefix.blank? ? k : prefix + '.' + k)).flatten(1)
17
- end
18
- elsif hash.is_a? Array
19
- [ prefix, [ hash.reject { |e| e.blank? } ] ]
20
- else
21
- [ prefix, hash ]
22
- end
23
- end
24
-
25
- reload!
26
-
27
- tmp = tmp_translations.collect do |locale, values|
28
- [locale, Hash[*yaml_to_key_string(values).flatten(1)]]
29
- end
30
-
31
- Hash[[*tmp]]
32
- end
33
-
34
- protected
35
-
36
- def load_csv(filename)
37
- begin
38
- lines = CSV.read(filename)
39
- locale = ::File.basename(filename, '.csv').to_sym
40
- { locale => Hash[ lines.map { |l| l if l.size == 2 } ] }
41
- rescue Exception => e
42
- Rails.logger.error "I18n CSV error: #{e.message} :: #{e.backtrace}"
43
- nil
44
- end
45
- end
46
- end
47
- end
48
-
49
- class MissingTranslation
50
- module Base
51
- def html_message
52
- key
53
- end
54
- end
55
- end
56
- end
1
+ require 'i18n_csv/i18n_csv'
57
2
 
58
3
  module I18n_CSV
59
4
  class Railtie < Rails::Railtie
@@ -1,3 +1,3 @@
1
1
  module I18nCsv
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Shoobovych
@@ -75,7 +75,7 @@ files:
75
75
  - lib/i18n_csv/i18n_csv.rb
76
76
  - lib/i18n_csv/railtie.rb
77
77
  - lib/i18n_csv/version.rb
78
- homepage: ''
78
+ homepage: https://github.com/shybovycha/i18n_csv
79
79
  licenses:
80
80
  - MIT
81
81
  metadata: {}