localized 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- localized (0.0.2)
4
+ localized (0.0.3)
5
5
  rails (>= 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ### Localized
1
+ # Localized
2
2
 
3
3
  A simple gem for Rails 3 that helps set the locale for your application from a
4
4
  subdomain. It also contains a way for overriding the "site" in url helpers.
@@ -23,6 +23,19 @@ subdomain. It also contains a way for overriding the "site" in url helpers.
23
23
 
24
24
  root_url # => http://www.<whatever the current site is>.mysite.com
25
25
 
26
+ ## Other Features
27
+
28
+ In addition to the runtime features, there is a feature for
29
+ converting all of the locale files into a csv so it can be
30
+ updated to Google docs (or Excel) and translations can be made
31
+ easier than if a user was using YAML.
32
+
33
+ Localized::Convert.to_csv('/a/file.csv')
34
+
35
+ Coming soon... #from_csv
36
+
37
+ Comeing
38
+
26
39
  ## Contributing
27
40
 
28
41
  This is a very simple gem and I am sure you could think of something you could
@@ -2,9 +2,11 @@ require 'rails'
2
2
  require 'action_view'
3
3
  require 'action_controller'
4
4
  require 'active_model'
5
+ require 'i18n'
5
6
  module Localized; end
6
7
  require 'localized/config'
7
8
  require 'localized/helper'
9
+ require 'localized/convert'
8
10
 
9
11
  # load the modules into the rails world
10
12
  [
@@ -25,8 +25,8 @@ module Localized::Config
25
25
 
26
26
  def self.default_site
27
27
  @default_site ||= self.configuration[:default_site]
28
- end
29
-
28
+ end
29
+
30
30
  def self.supported_sites
31
31
  @supported_sites ||= site_to_locale_map.symbolize_keys.keys.sort
32
32
  end
@@ -0,0 +1,56 @@
1
+ require 'csv'
2
+ module Localized::Convert
3
+
4
+ def self.to_csv(file)
5
+ CSV.open(file, "wb") do |csv|
6
+ csv << ["Token", translations.keys.sort].flatten
7
+ locale_cache.keys.sort.each do |token|
8
+ row = [token]
9
+ locale_cache[token].keys.sort.each do |locale|
10
+ row << locale_cache[token][locale]
11
+ end
12
+ csv << row
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.load_cache
18
+ I18n.backend.send(:init_translations)
19
+ end
20
+
21
+ def self.translations
22
+ load_cache
23
+ @translations ||= I18n.backend.send(:translations)
24
+ end
25
+
26
+ def self.locale_cache
27
+ @locale_cache = begin
28
+ rows = {}
29
+ columns = Hash[translations.keys.map{|l| [l, nil]}]
30
+ translations.keys.each do |locale| # each locale
31
+ translations[locale].each do |key, value| # each translation
32
+ key_string, value_string = formalize_keys(key, value)
33
+ rows[key_string] ||= columns
34
+ rows[key_string] = rows[key_string].merge(locale => value_string)
35
+ end
36
+ end
37
+ rows.reject { |k, r| r.values.compact.blank? }
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def self.formalize_keys(key, value)
44
+ key_string = key.to_s
45
+ value_string = nil
46
+ if value.is_a? Hash
47
+ value.each do |key, new_value|
48
+ key_string += ".#{key}"
49
+ key_string, value_string = formalize_keys(key_string, new_value)
50
+ end
51
+ else
52
+ value_string = value
53
+ end
54
+ [key_string, value_string]
55
+ end
56
+ end
@@ -2,7 +2,8 @@ Gem::Specification.new do |s|
2
2
  s.name = 'localized'
3
3
  s.author = 'Paul Hepworth'
4
4
  s.email = 'paul<dot>hepworth<at>peppyheppy<dot>com'
5
- s.version = '0.0.3'
5
+ s.version = '0.0.4'
6
+ s.homepage = 'https://github.com/peppyheppy/localized'
6
7
  s.date = '2011-07-09'
7
8
  s.summary = "A ruby on rails gem that provides locale setting support through the subdomain (in a box)"
8
9
  s.description = "This gem allows you to set locale using a subdomain and url helper support for switching sites."
@@ -0,0 +1,5 @@
1
+ en-US:
2
+ one:
3
+ two:
4
+ three: "three"
5
+
@@ -0,0 +1,4 @@
1
+ es-ES:
2
+ one:
3
+ two:
4
+
@@ -0,0 +1,5 @@
1
+ nl-NL:
2
+ one:
3
+ two:
4
+ three: "another"
5
+
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Localized::Convert do
4
+ include Localized
5
+ before :all do
6
+ I18n.load_path = Dir[File.join('spec/config/locales/*.yml')]
7
+ end
8
+
9
+ describe "Locale cache" do
10
+ it "should return cache keys for each unique key path" do
11
+ Convert.locale_cache.detect { |k,v| k == 'one.two.three' }.should_not be_nil
12
+ end
13
+
14
+ it "should return a list of each locale for each key" do
15
+ Convert.translations.keys.each do |key|
16
+ Convert.locale_cache['one.two.three'].should include(key)
17
+ end
18
+ end
19
+
20
+ it "should have an empty value for languages whose files do not have key" do
21
+ Convert.locale_cache['one.two.three'][:'en-US'].should == 'three'
22
+ Convert.locale_cache['one.two.three'][:'nl-NL'].should == 'another'
23
+ Convert.locale_cache['one.two.three'][:'es-ES'].should be_nil
24
+ end
25
+
26
+ it "should not have empty valueless keys" do
27
+ Convert.locale_cache.should_not have_key 'one.two'
28
+ end
29
+ end
30
+
31
+ describe "CSV export" do
32
+ it "should convert the locale cache to a CSV file" do
33
+ file = 'spec/test_csv.csv'
34
+ File.should_not exist file
35
+ Convert.to_csv(file)
36
+ File.should exist file
37
+ rows = CSV.read(file)
38
+ # header row
39
+ rows.first[0].should == "Token"
40
+ rows.first[1].should == "en-US"
41
+ rows.first[2].should == "es-ES"
42
+ rows.first[3].should == "nl-NL"
43
+ rows.first[4].should be_nil
44
+
45
+ # data row
46
+ rows.last[0].should == "one.two.three"
47
+ rows.last[1].should == "three"
48
+ rows.last[2].should be_nil
49
+ rows.last[3].should == "another"
50
+ rows.last[4].should be_nil
51
+
52
+ File.delete(file)
53
+ end
54
+ end
55
+
56
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: localized
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paul Hepworth
@@ -76,11 +76,16 @@ files:
76
76
  - config/defaults.yml
77
77
  - lib/localized.rb
78
78
  - lib/localized/config.rb
79
+ - lib/localized/convert.rb
79
80
  - lib/localized/helper.rb
80
81
  - localized.gemspec
82
+ - spec/config/locales/en-US.yml
83
+ - spec/config/locales/es-ES.yml
84
+ - spec/config/locales/nl-NL.yml
81
85
  - spec/config/localized.yml
82
86
  - spec/config/routes.rb
83
87
  - spec/lib/localized/config_spec.rb
88
+ - spec/lib/localized/convert_spec.rb
84
89
  - spec/lib/localized/helper_spec.rb
85
90
  - spec/spec_helper.rb
86
91
  - spec/views/widget.erb
@@ -88,7 +93,7 @@ files:
88
93
  - spec/views/widget_invalid.erb
89
94
  - spec/views/widget_views_spec.rb
90
95
  has_rdoc: true
91
- homepage:
96
+ homepage: https://github.com/peppyheppy/localized
92
97
  licenses: []
93
98
 
94
99
  post_install_message: