translation_panel 0.2.2 → 0.2.3

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.
data/README.textile ADDED
@@ -0,0 +1,44 @@
1
+ h1. TranslationPanel
2
+
3
+ h3. Using with Redis:
4
+
5
+ # Run redis-server
6
+
7
+ # Set up redis connection settings and register RedisTranslator::Backend as
8
+ i18n backend in initializers:
9
+
10
+ I18n.backend = TranslationPanel::RedisBackend.new(Redis.new)
11
+ # or in chain with another backend
12
+
13
+ # Create predicate method +translation_panel?+ in ApplicationController,
14
+ define in it, when panel can be shown and when translates can be saved. By
15
+ default always.
16
+
17
+ def translation_panel?
18
+ params[:translator].present?
19
+ end
20
+
21
+ h3. Dependencies
22
+
23
+ TranslationPanel uses assets, scss, coffeescript, as all its work in Rails 3.1.
24
+
25
+ If you want to use panel with Rails 3.0, you need to use sprockets gem. Also you
26
+ need at least i18n-0.6.0 for proper work with i18n keys
27
+
28
+ h3. Pluralization
29
+
30
+ Redis backen� support pluralization.
31
+
32
+ 1. You need to use Simple backend with included I18n::Pluralization in chain:
33
+
34
+ I18n::Backend::Simple.include I18n::Backend::Pluralization
35
+ redis_backend = TranslationPanel::RedisBackend.new(Redis.new)
36
+ I18n.backend = I18n::Backend::Chain.new(redis_backend, I18n::Backend::Simple.new)
37
+
38
+ 2. You need to add in +config/locales/*.yml+ files rules for pluralization:
39
+
40
+ i18n:
41
+ plural:
42
+ keys:
43
+ - one
44
+ - other
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -1,5 +1,7 @@
1
1
  module TranslationPanel
2
2
  class RedisBackend < I18n::Backend::KeyValue
3
+ include I18n::Backend::Pluralization
4
+
3
5
  def initialize(store)
4
6
  @subtrees = false
5
7
  @store = store
@@ -13,7 +15,7 @@ module TranslationPanel
13
15
  TranslationPanel.push key if TranslationPanel.show?
14
16
  ActiveSupport::JSON.decode(value) if value
15
17
  else # look in namespace
16
- store_count_keys(locale, key) if count
18
+ pluralization_result = count ? get_count_keys(locale, key) : {}
17
19
  full_keys = @store.keys "#{locale}.#{key}.*"
18
20
  if full_keys.empty?
19
21
  TranslationPanel.push key if TranslationPanel.show?
@@ -26,11 +28,14 @@ module TranslationPanel
26
28
  value = ActiveSupport::JSON.decode(value) if value
27
29
  result.merge full_key.partition("#{locale}.#{key}.")[2] => value
28
30
  end
29
- expand_keys(flatten_result).deep_symbolize_keys
31
+ expand_keys(pluralization_result.merge(flatten_result)).deep_symbolize_keys
30
32
  end
31
33
  end
32
34
  end
33
35
 
36
+ # Transforms flatten hash into nested hash
37
+ # expand_keys "some.one" => "a", "some.another" => "b"
38
+ # # => "some" => {"one" => "a", "another" => "b"}
34
39
  def expand_keys(flatten_hash)
35
40
  expanded_hash = {}
36
41
  flatten_hash.each do |key, value|
@@ -44,22 +49,27 @@ module TranslationPanel
44
49
  expanded_hash
45
50
  end
46
51
 
47
- def store_count_keys(locale, key)
48
- if TranslationPanel.show?
49
- I18n.t!('i18n.plural.keys', :locale => locale).each do |k|
50
- unless translate_present?(locale, "#{key}.#{k}")
51
- I18n.backend.store_translations locale, {"#{key}.#{k}" => ""}, :escape => false
52
- end
52
+ # Creates empty translations for absented pluralization keys.
53
+ # Returns hash with plural keys and their values (even from another backend)
54
+ def get_count_keys(locale, key)
55
+ I18n.t!('i18n.plural.keys', :locale => locale).inject({}) do |result, plural_key|
56
+ full_key = "#{key}.#{plural_key}"
57
+ value = get_translate(locale, full_key)
58
+ unless value
59
+ I18n.backend.store_translations locale, {full_key => ""}, :escape => false
60
+ value = ""
53
61
  end
62
+ result.merge plural_key.to_s => value
54
63
  end
55
64
  rescue
56
- nil
65
+ {}
57
66
  end
58
67
 
59
- def translate_present?(locale, key)
68
+ # returns translation key if any, otherwise nil
69
+ def get_translate(locale, key)
60
70
  I18n.t!(key, :locale => locale)
61
71
  rescue
62
- false
72
+ nil
63
73
  end
64
74
  end
65
75
  end
@@ -3,6 +3,7 @@ require 'translation_panel'
3
3
 
4
4
  MultiJson.engine = :yajl
5
5
 
6
- redis = Redis.new(YAML::load_file(File.join(Rails.root, '/config/redis.yml'))[Rails.env].symbolize_keys)
7
-
8
- I18n.backend = TranslationPanel::RedisBackend.new(redis)
6
+ I18n::Backend::Simple.include I18n::Backend::Pluralization
7
+ r_conf = YAML::load_file(File.join(Rails.root, '/config/redis.yml'))[Rails.env].symbolize_keys
8
+ Redis_backend = TranslationPanel::RedisBackend.new Redis.new(r_conf)
9
+ I18n.backend = I18n::Backend::Chain.new Redis_backend, I18n::Backend::Simple.new
@@ -0,0 +1,5 @@
1
+ en:
2
+ some:
3
+ test:
4
+ one: One test
5
+ other: %{count} tests
@@ -0,0 +1,17 @@
1
+ {
2
+ :en => {
3
+ :i18n => {
4
+ :plural => {
5
+ :keys => [:one, :other],
6
+ :rule => lambda { |n| n == 1 ? :one : :other } } } },
7
+ :ru => {
8
+ :i18n => {
9
+ :plural => {
10
+ :keys => [:one, :few, :other],
11
+ :rule => lambda { |n|
12
+ (n % 10) == 1 && (n % 100) !=11 ?
13
+ :one :
14
+ [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100) ?
15
+ :few :
16
+ :other } } } }
17
+ }
@@ -0,0 +1,8 @@
1
+ ru:
2
+ some:
3
+ test:
4
+ one: %{count} тест
5
+ few: %{count} теста
6
+ other: %{count} тестов
7
+ missing:
8
+ one: %{count} пропажа
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe TranslationPanel::RedisBackend do
5
5
  before :all do
6
- I18n.backend.store.flushdb
6
+ Redis_backend.store.flushdb
7
7
  end
8
8
 
9
9
  it "stores data, represented by pair key-value" do
@@ -54,11 +54,33 @@ describe TranslationPanel::RedisBackend do
54
54
  it "pluralizes translations" do
55
55
  I18n.backend.store_translations "ru", :bug => {:one => "1 bug", :other => "%{count} bugs"}
56
56
  I18n.translate(:bug, :count => 1).should == "1 bug"
57
- I18n.translate(:bug, :count => 2).should == "2 bugs"
57
+ I18n.translate(:bug, :count => 5).should == "5 bugs"
58
58
  end
59
59
 
60
60
  it 'deletes existing translation with nil value' do
61
61
  I18n.backend.store_translations "ru", "simple.key" => nil
62
- I18n.backend.store["simple.key"].should be_nil
62
+ Redis_backend.store["simple.key"].should be_nil
63
+ end
64
+
65
+ describe "#pluralisation" do
66
+ it "translates from simple, if no keys in redis" do
67
+ I18n.translate("some.test", :count => 21).should == "21 тест"
68
+ I18n.translate("some.test", :count => 23).should == "23 теста"
69
+ I18n.translate("some.test", :count => 11).should == "11 тестов"
70
+ end
71
+
72
+ it "translates from redis, but absented keys takes from simple" do
73
+ I18n.backend.store_translations "ru", {"some.test.few" => "%{count} испытания"}, :escape => false
74
+ I18n.translate("some.test", :count => 21).should == "21 тест"
75
+ I18n.translate("some.test", :count => 23).should == "23 испытания"
76
+ I18n.translate("some.test", :count => 11).should == "11 тестов"
77
+ end
78
+
79
+ it "creates empty keys in redis if no such keys in both backends" do
80
+ I18n.backend.store_translations "ru", {"some.missing.few" => "%{count} пропажи"}, :escape => false
81
+ I18n.translate("some.missing", :count => 21).should == "21 пропажа"
82
+ I18n.translate("some.missing", :count => 23).should == "23 пропажи"
83
+ I18n.translate("some.missing", :count => 11).should == ""
84
+ end
63
85
  end
64
86
  end
@@ -10,7 +10,7 @@ describe HomeController, :type => :controller do
10
10
  render_views
11
11
  describe "index" do
12
12
  before :all do
13
- I18n.backend.store.flushdb
13
+ Redis_backend.store.flushdb
14
14
  end
15
15
 
16
16
  describe "normal processing" do
@@ -54,7 +54,7 @@ describe HomeController, :type => :controller do
54
54
  describe "without_head" do
55
55
  render_views
56
56
  before :all do
57
- I18n.backend.store.flushdb
57
+ Redis_backend.store.flushdb
58
58
  end
59
59
 
60
60
  it "is successfull" do
@@ -5,15 +5,15 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{translation_panel}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mik-die"]
12
- s.date = %q{2011-08-09}
12
+ s.date = %q{2011-08-13}
13
13
  s.description = %q{I18n backend, based on redis, with frontend panel for translations}
14
14
  s.email = %q{MikDiet@gmail.com}
15
15
  s.extra_rdoc_files = [
16
- "README.rdoc"
16
+ "README.textile"
17
17
  ]
18
18
  s.files = [
19
19
  ".rspec",
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  "Gemfile",
22
22
  "Gemfile.lock",
23
23
  "MIT-LICENSE",
24
- "README.rdoc",
24
+ "README.textile",
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "app/assets/javascripts/translation_panel.js.coffee",
@@ -48,6 +48,9 @@ Gem::Specification.new do |s|
48
48
  "spec/app/config/initializers/i18n.rb",
49
49
  "spec/app/config/initializers/secret_token.rb",
50
50
  "spec/app/config/initializers/session_store.rb",
51
+ "spec/app/config/locales/en.yml",
52
+ "spec/app/config/locales/plural.rb",
53
+ "spec/app/config/locales/ru.yml",
51
54
  "spec/app/config/redis.yml",
52
55
  "spec/app/config/redis/test.conf",
53
56
  "spec/app/config/routes.rb",
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: translation_panel
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.2
5
+ version: 0.2.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mik-die
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-09 00:00:00 +08:00
13
+ date: 2011-08-13 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -86,14 +86,14 @@ executables: []
86
86
  extensions: []
87
87
 
88
88
  extra_rdoc_files:
89
- - README.rdoc
89
+ - README.textile
90
90
  files:
91
91
  - .rspec
92
92
  - .rvmrc
93
93
  - Gemfile
94
94
  - Gemfile.lock
95
95
  - MIT-LICENSE
96
- - README.rdoc
96
+ - README.textile
97
97
  - Rakefile
98
98
  - VERSION
99
99
  - app/assets/javascripts/translation_panel.js.coffee
@@ -120,6 +120,9 @@ files:
120
120
  - spec/app/config/initializers/i18n.rb
121
121
  - spec/app/config/initializers/secret_token.rb
122
122
  - spec/app/config/initializers/session_store.rb
123
+ - spec/app/config/locales/en.yml
124
+ - spec/app/config/locales/plural.rb
125
+ - spec/app/config/locales/ru.yml
123
126
  - spec/app/config/redis.yml
124
127
  - spec/app/config/redis/test.conf
125
128
  - spec/app/config/routes.rb
@@ -143,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
146
  requirements:
144
147
  - - ">="
145
148
  - !ruby/object:Gem::Version
146
- hash: -923747487
149
+ hash: -593371821
147
150
  segments:
148
151
  - 0
149
152
  version: "0"
data/README.rdoc DELETED
@@ -1,25 +0,0 @@
1
- = TranslationPanel
2
-
3
- = Using with Redis:
4
-
5
- # Run redis-server
6
-
7
- # Set up redis connection settings and register RedisTranslator::Backend as
8
- i18n backend in initializers:
9
-
10
- I18n.backend = RedisTranslator::Backend.new(Redis.new)
11
- # or in chain with another backend
12
-
13
- # Create predicate method +translation_panel?+ in ApplicationController,
14
- define in it, when panel can be shown and when translates can be saved. By
15
- default always.
16
-
17
- def translation_panel?
18
- params[:translator].present?
19
- end
20
-
21
- = Dependencies
22
-
23
- TranslationPanel uses asset pipeline, so it works nice with Rails 3.1.
24
-
25
- If you want to use panel with Rails 3.0, you need to use sprockets gem.