i18n_csv 0.0.1 → 0.0.2
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/README.md +1 -1
- data/i18n_csv.gemspec +1 -0
- data/lib/i18n_csv/i18n_csv.rb +59 -0
- data/lib/i18n_csv/railtie.rb +65 -0
- data/lib/i18n_csv/version.rb +1 -1
- data/lib/i18n_csv.rb +1 -59
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aebb9bd92b897b5fae2bf122018169da25a553e0
|
4
|
+
data.tar.gz: 541f64428f4b71059bfe65dbf1d4578081dcab31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec5d7f1e63390a5186f8f8e0e5cd619e272c214fc8b7b80692fee72166cd76efe631c401838770e6a83ba0fe1b7db24a39f74545c1c4252e5aca235018a7152a
|
7
|
+
data.tar.gz: 2d33879cd7ed204f4ab0a0d9e3f90fbe347c6663f814d0769e4a211504718b602c9e82a7deaedd780efa45a049d78da5b8583817d431e3bddc8f5c602ab4e325
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# I18nCsv
|
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
5
|
## Installation
|
6
6
|
|
data/i18n_csv.gemspec
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Backend
|
5
|
+
module CSV18n
|
6
|
+
def get_translations
|
7
|
+
tmp_translations = translations()
|
8
|
+
|
9
|
+
def moo(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
|
+
moo(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[*moo(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
|
57
|
+
|
58
|
+
I18n::Backend::Simple.include(I18n::Backend::CSV18n)
|
59
|
+
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.csv')]
|
@@ -0,0 +1,65 @@
|
|
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
|
57
|
+
|
58
|
+
module I18n_CSV
|
59
|
+
class Railtie < Rails::Railtie
|
60
|
+
initializer "i18n_csv.configure_rails_initialization" do
|
61
|
+
I18n::Backend::Simple.include(I18n::Backend::CSV18n)
|
62
|
+
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.csv')]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/i18n_csv/version.rb
CHANGED
data/lib/i18n_csv.rb
CHANGED
@@ -1,61 +1,3 @@
|
|
1
1
|
require "i18n_csv/version"
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
module I18n
|
6
|
-
module Backend
|
7
|
-
module CSV18n
|
8
|
-
def get_translations
|
9
|
-
tmp_translations = translations()
|
10
|
-
|
11
|
-
def moo(hash, prefix = nil)
|
12
|
-
prefix = prefix.to_s
|
13
|
-
|
14
|
-
if hash.is_a? Hash then
|
15
|
-
hash.collect do |k, v|
|
16
|
-
k = k.to_s
|
17
|
-
|
18
|
-
moo(v, (prefix.blank? ? k : prefix + '.' + k)).flatten(1)
|
19
|
-
end
|
20
|
-
elsif hash.is_a? Array
|
21
|
-
[ prefix, [ hash.reject { |e| e.blank? } ] ]
|
22
|
-
else
|
23
|
-
[ prefix, hash ]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
reload!
|
28
|
-
|
29
|
-
tmp = tmp_translations.collect do |locale, values|
|
30
|
-
[locale, Hash[*moo(values).flatten(1)]]
|
31
|
-
end
|
32
|
-
|
33
|
-
Hash[[*tmp]]
|
34
|
-
end
|
35
|
-
|
36
|
-
protected
|
37
|
-
|
38
|
-
def load_csv(filename)
|
39
|
-
begin
|
40
|
-
lines = CSV.read(filename)
|
41
|
-
locale = ::File.basename(filename, '.csv').to_sym
|
42
|
-
{ locale => Hash[ lines.map { |l| l if l.size == 2 } ] }
|
43
|
-
rescue Exception => e
|
44
|
-
Rails.logger.error "I18n CSV error: #{e.message} :: #{e.backtrace}"
|
45
|
-
nil
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class MissingTranslation
|
52
|
-
module Base
|
53
|
-
def html_message
|
54
|
-
key
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
I18n::Backend::Simple.include(I18n::Backend::CSV18n)
|
61
|
-
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.csv')]
|
3
|
+
require 'i18n_csv/railtie' if defined?(Rails)
|
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.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artem Shoobovych
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: i18n
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: 'One extends standard I18n so that you could store your translations
|
42
56
|
in a Comma-Separated Value files (CSV) in a key-value manner, where the key is a
|
43
57
|
word or a phrase or even a poem if you wish. No limits here (except be aware to
|
@@ -58,6 +72,8 @@ files:
|
|
58
72
|
- Rakefile
|
59
73
|
- i18n_csv.gemspec
|
60
74
|
- lib/i18n_csv.rb
|
75
|
+
- lib/i18n_csv/i18n_csv.rb
|
76
|
+
- lib/i18n_csv/railtie.rb
|
61
77
|
- lib/i18n_csv/version.rb
|
62
78
|
homepage: ''
|
63
79
|
licenses:
|