localized 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source :rubygems
2
2
 
3
3
  gemspec
4
+
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- localized (0.0.3)
4
+ localized (0.0.4)
5
5
  rails (>= 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -32,9 +32,12 @@ easier than if a user was using YAML.
32
32
 
33
33
  Localized::Convert.to_csv('/a/file.csv')
34
34
 
35
- Coming soon... #from_csv
35
+ ## TODO
36
+
37
+ 1. #from_csv
38
+
39
+ 2. Support for google docs so translations can be synchronized
36
40
 
37
- Comeing
38
41
 
39
42
  ## Contributing
40
43
 
@@ -3,15 +3,16 @@ module Localized::Convert
3
3
 
4
4
  def self.to_csv(file)
5
5
  CSV.open(file, "wb") do |csv|
6
- csv << ["Token", translations.keys.sort].flatten
6
+ csv << ["Token", *locale_columns]
7
7
  locale_cache.keys.sort.each do |token|
8
8
  row = [token]
9
- locale_cache[token].keys.sort.each do |locale|
9
+ locale_columns.each do |locale|
10
10
  row << locale_cache[token][locale]
11
11
  end
12
12
  csv << row
13
13
  end
14
14
  end
15
+ nil
15
16
  end
16
17
 
17
18
  def self.load_cache
@@ -26,12 +27,11 @@ module Localized::Convert
26
27
  def self.locale_cache
27
28
  @locale_cache = begin
28
29
  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)
30
+ columns = Hash[locale_columns.map{|l| [l, nil]}]
31
+ Localized::Config.supported_locales.each do |locale| # each locale
32
+ formalize_keys(nil, (translations[locale] || {})).each do |(key, value)| # each translation
33
+ rows[key] ||= columns
34
+ rows[key] = rows[key].merge(locale => value)
35
35
  end
36
36
  end
37
37
  rows.reject { |k, r| r.values.compact.blank? }
@@ -41,16 +41,22 @@ module Localized::Convert
41
41
  private
42
42
 
43
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)
44
+ pairs = []
45
+ if value.is_a?(Hash)
46
+ value.each do |new_key, new_value|
47
+ pairs << formalize_keys("#{key}#{key && "."}#{new_key}", new_value)
50
48
  end
51
- else
52
- value_string = value
49
+ elsif value.is_a? String
50
+ pairs << [key, value]
53
51
  end
54
- [key_string, value_string]
52
+ Hash[*pairs.flatten].to_a
55
53
  end
54
+
55
+ def self.locale_columns
56
+ @locale_columns ||= [
57
+ Localized::Config.default_locale,
58
+ *Localized::Config.supported_locales.reject { |t| t == Localized::Config.default_locale }
59
+ ]
60
+ end
61
+
56
62
  end
@@ -2,7 +2,7 @@ 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.4'
5
+ s.version = '0.0.5'
6
6
  s.homepage = 'https://github.com/peppyheppy/localized'
7
7
  s.date = '2011-07-09'
8
8
  s.summary = "A ruby on rails gem that provides locale setting support through the subdomain (in a box)"
@@ -2,4 +2,11 @@ en-US:
2
2
  one:
3
3
  two:
4
4
  three: "three"
5
-
5
+ six: "moroni"
6
+ four: "hello"
7
+ five: "special"
8
+ seven:
9
+ eight:
10
+ nine:
11
+ ten:
12
+ eleven: "truth"
@@ -2,7 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe Localized::Convert do
4
4
  include Localized
5
- before :all do
5
+ before :each do
6
+ Rails.stub!(:root).and_return(Pathname.new(File.join(File.dirname(__FILE__),'..', '..')))
6
7
  I18n.load_path = Dir[File.join('spec/config/locales/*.yml')]
7
8
  end
8
9
 
@@ -23,31 +24,66 @@ describe Localized::Convert do
23
24
  Convert.locale_cache['one.two.three'][:'es-ES'].should be_nil
24
25
  end
25
26
 
27
+ it "should have values set for one deep" do
28
+ Convert.locale_cache['five'][:'en-US'].should == 'special'
29
+ end
30
+
31
+ it "should have values set for two deep" do
32
+ Convert.locale_cache['one.four'][:'en-US'].should == 'hello'
33
+ end
34
+
35
+ it "should have values set for three deep" do
36
+ Convert.locale_cache['one.two.six'][:'en-US'].should == 'moroni'
37
+ end
38
+
39
+ it "should have values set for six deep" do
40
+ Convert.locale_cache['seven.eight.nine.ten.eleven'][:'en-US'].should == 'truth'
41
+ end
42
+
26
43
  it "should not have empty valueless keys" do
27
44
  Convert.locale_cache.should_not have_key 'one.two'
28
45
  end
46
+
47
+ it "should have the same keys that are listed in the localized config file" do
48
+ Convert.locale_cache['one.two.three'].keys.sort.should == Localized::Config.supported_locales.sort
49
+ end
29
50
  end
30
51
 
31
52
  describe "CSV export" do
32
53
  it "should convert the locale cache to a CSV file" do
33
54
  file = 'spec/test_csv.csv'
34
- File.should_not exist file
55
+ #File.should_not exist file
35
56
  Convert.to_csv(file)
36
57
  File.should exist file
37
58
  rows = CSV.read(file)
38
59
  # header row
60
+ # ["Token", "en-US", "de-DE", "en-AU", "en-CA", "en-GB", "es-ES", "fr-FR", "it-IT", "ja-JP", "nl-NL", "sv-SE"]
39
61
  rows.first[0].should == "Token"
40
62
  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
63
+ rows.first[2].should == "de-DE"
64
+ rows.first[3].should == "en-AU"
65
+ rows.first[4].should == "en-CA"
66
+ rows.first[5].should == "en-GB"
67
+ rows.first[6].should == "es-ES"
68
+ rows.first[7].should == "fr-FR"
69
+ rows.first[8].should == "it-IT"
70
+ rows.first[9].should == "ja-JP"
71
+ rows.first[10].should == "nl-NL"
72
+ rows.first[11].should == "sv-SE"
44
73
 
45
74
  # data row
46
- rows.last[0].should == "one.two.three"
47
- rows.last[1].should == "three"
75
+ # ["one.two.three", "three", nil, nil, nil, nil, nil, nil, nil, nil, nil, "another", nil]
76
+ rows.last[0].should == "seven.eight.nine.ten.eleven"
77
+ rows.last[1].should == "truth"
48
78
  rows.last[2].should be_nil
49
- rows.last[3].should == "another"
79
+ rows.last[3].should be_nil
50
80
  rows.last[4].should be_nil
81
+ rows.last[5].should be_nil
82
+ rows.last[6].should be_nil
83
+ rows.last[7].should be_nil
84
+ rows.last[8].should be_nil
85
+ rows.last[9].should be_nil
86
+ rows.last[11].should be_nil
51
87
 
52
88
  File.delete(file)
53
89
  end
@@ -4,6 +4,8 @@ require "ruby-debug"
4
4
  require "localized"
5
5
  require "action_controller/railtie"
6
6
  require 'rspec/rails'
7
+ require 'rspec/mocks'
8
+ require 'rspec/rails/mocks'
7
9
 
8
10
  class Application < Rails::Application
9
11
  # abusing locale for site/locale
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: localized
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paul Hepworth
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-09 00:00:00 -07:00
13
+ date: 2011-07-09 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency