i18n_csv 0.1.0 → 0.1.2

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: 44295b382a5ff909e6948d766834a1981c495390
4
- data.tar.gz: 1d0e5f21d6fcdc5dd70ec5879adb167a1c0faf83
3
+ metadata.gz: 8130fdb7daca841d2d863e861a0811500fb43cf1
4
+ data.tar.gz: 0026398ed693ae1ebac7ae36b7979c96ab6aca6b
5
5
  SHA512:
6
- metadata.gz: df3ecda855e8b39dd9a69b22e6dda3e6645f58fac61edf3ad84276986c253222707f710bce56576ce16ac2c7717de5bb2f2b2b0d1c56c60f2fc10ac9c48a0e9f
7
- data.tar.gz: 0a1b801293dd0e5f7acb1f02841fa634d02c1ed6e26795456e8788a600ebf89bab181ce1e31c0faa8055dda47c653657d0d5b4856adf270a379302245739a1c2
6
+ metadata.gz: d85831f7f8f4253c2d6db208207fc058109d51d1a1ec226b8d7c9795c9ac1e3e0fa966a8f52cc03592e4b03f1e3778af57cf16bf9c408542cd92d2300276cf7f
7
+ data.tar.gz: 27f35615069b065a95bc22f7c91aa68657e12fe775926af560f950784555bed98f5febf710899f963ce21f4f13130f968cc0506a38f83eeb802220a5361f5e60
data/README.md CHANGED
@@ -47,6 +47,21 @@ Here are some examples:
47
47
  irb(main):004:0> I18n.t 'hello, world!'
48
48
  => "хеллоу ворлд =3"
49
49
 
50
+ ## Frontend translations
51
+
52
+ Version `0.1.2` includes javascript translation support.
53
+
54
+ To use it, just add this line to your `app/assets/javascripts/application.js` file:
55
+
56
+ //= require i18n_csv/frontend_translation
57
+
58
+ And you are ready to use the `window.t(key, [locale]);` function on your frontend!
59
+
60
+ For example of above locale files, imagine you have this line in one of your javascript files:
61
+
62
+ alert(t('hello'));
63
+
64
+ You shall see then `привіт` in your alert window!
50
65
 
51
66
  ## Contributing
52
67
 
@@ -0,0 +1,19 @@
1
+ <% locales = I18n.backend.get_translations %>
2
+
3
+ window.t = function(key, locale) {
4
+ var translations = {};
5
+
6
+ <% locales.each do |locale, translations| %>
7
+ translations[<%= locale.to_s.inspect %>] = {};
8
+
9
+ <% translations.each do |key, value| %>
10
+ translations[<%= locale.to_s.inspect %>][<%= key.inspect %>] = <%= value.inspect %>;
11
+ <% end %>
12
+ <% end %>
13
+
14
+ locale = locale || <%= I18n.locale.to_s.inspect || 'en' %>;
15
+
16
+ var result = (translations[locale] && translations[locale][key]) || key;
17
+
18
+ return result;
19
+ };
@@ -1,56 +1,56 @@
1
1
  require 'csv'
2
2
 
3
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)
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]]
17
32
  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
33
 
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
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
44
46
  end
45
- end
46
47
  end
47
- end
48
48
 
49
- class MissingTranslation
50
- module Base
51
- def html_message
52
- key
53
- end
49
+ class MissingTranslation
50
+ module Base
51
+ def html_message
52
+ key
53
+ end
54
+ end
54
55
  end
55
- end
56
56
  end
@@ -1,10 +1,13 @@
1
1
  require 'i18n_csv/i18n_csv'
2
2
 
3
3
  module I18n_CSV
4
- class Railtie < Rails::Railtie
5
- initializer "i18n_csv.configure_rails_initialization" do
6
- I18n::Backend::Simple.include(I18n::Backend::CSV18n)
7
- I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.csv')]
4
+ class Railtie < Rails::Railtie
5
+ initializer "i18n_csv.configure_rails_initialization" do
6
+ I18n::Backend::Simple.include(I18n::Backend::CSV18n)
7
+ I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.csv')]
8
+ end
9
+ end
10
+
11
+ class I18nCSV_Engine < ::Rails::Engine
8
12
  end
9
- end
10
13
  end
@@ -1,3 +1,3 @@
1
1
  module I18nCsv
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/i18n_csv.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require "i18n_csv/version"
2
2
 
3
- require 'i18n_csv/railtie' if defined?(Rails)
3
+ require 'i18n_csv/railtie' if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Shoobovych
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-27 00:00:00.000000000 Z
11
+ date: 2013-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - LICENSE.txt
71
71
  - README.md
72
72
  - Rakefile
73
+ - app/assets/javascripts/i18n_csv/frontend_translation.js.erb
73
74
  - i18n_csv.gemspec
74
75
  - lib/i18n_csv.rb
75
76
  - lib/i18n_csv/i18n_csv.rb