fast_gettext 0.5.13 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,7 @@
1
+ 0.6.0 -- plurals use singular translations as fallack e.g. you translated 'Axis' then n_('Axis','Axis',1) would return the translation for 'Axis' if no plural translation was found
1
2
  0.4.14 -- "" is translated as "", not as gettext meta information
2
3
  0.4.0 -- pluralisation_rules is no longer stored in each repository, only retrived. Added Chain and Logger repository.
3
4
  0.3.6 -- FastGettext.default_locale=
4
5
  0.3.5 -- FastGettext.default_text_domain=
5
6
  0.3.4 -- Exceptions are thrown, not returned when translating without text domain
6
- 0.3 -- pluralisation methods accept/return n plural forms, contrary to singular/plural before
7
+ 0.3 -- pluralisation methods accept/return n plural forms, contrary to singular/plural before
data/Gemfile CHANGED
@@ -5,5 +5,5 @@ group :dev do
5
5
  gem 'sqlite3'
6
6
  gem 'rspec', '~>2'
7
7
  gem 'jeweler'
8
- gem 'activerecord'
9
- end
8
+ gem 'activerecord', ENV['AR']
9
+ end
data/Gemfile.lock CHANGED
@@ -1,35 +1,39 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.0.9)
5
- activesupport (= 3.0.9)
6
- builder (~> 2.1.2)
7
- i18n (~> 0.5.0)
8
- activerecord (3.0.9)
9
- activemodel (= 3.0.9)
10
- activesupport (= 3.0.9)
11
- arel (~> 2.0.10)
12
- tzinfo (~> 0.3.23)
13
- activesupport (3.0.9)
14
- arel (2.0.10)
15
- builder (2.1.2)
16
- diff-lcs (1.1.2)
4
+ activemodel (3.1.0)
5
+ activesupport (= 3.1.0)
6
+ bcrypt-ruby (~> 3.0.0)
7
+ builder (~> 3.0.0)
8
+ i18n (~> 0.6)
9
+ activerecord (3.1.0)
10
+ activemodel (= 3.1.0)
11
+ activesupport (= 3.1.0)
12
+ arel (~> 2.2.1)
13
+ tzinfo (~> 0.3.29)
14
+ activesupport (3.1.0)
15
+ multi_json (~> 1.0)
16
+ arel (2.2.1)
17
+ bcrypt-ruby (3.0.1)
18
+ builder (3.0.0)
19
+ diff-lcs (1.1.3)
17
20
  git (1.2.5)
18
- i18n (0.5.0)
19
- jeweler (1.5.2)
20
- bundler (~> 1.0.0)
21
+ i18n (0.6.0)
22
+ jeweler (1.6.4)
23
+ bundler (~> 1.0)
21
24
  git (>= 1.2.5)
22
25
  rake
23
- rake (0.8.7)
24
- rspec (2.5.0)
25
- rspec-core (~> 2.5.0)
26
- rspec-expectations (~> 2.5.0)
27
- rspec-mocks (~> 2.5.0)
28
- rspec-core (2.5.1)
29
- rspec-expectations (2.5.0)
26
+ multi_json (1.0.3)
27
+ rake (0.9.2)
28
+ rspec (2.6.0)
29
+ rspec-core (~> 2.6.0)
30
+ rspec-expectations (~> 2.6.0)
31
+ rspec-mocks (~> 2.6.0)
32
+ rspec-core (2.6.4)
33
+ rspec-expectations (2.6.0)
30
34
  diff-lcs (~> 1.1.2)
31
- rspec-mocks (2.5.0)
32
- sqlite3 (1.3.3)
35
+ rspec-mocks (2.6.0)
36
+ sqlite3 (1.3.4)
33
37
  tzinfo (0.3.29)
34
38
 
35
39
  PLATFORMS
data/Rakefile CHANGED
@@ -1,7 +1,6 @@
1
- task :default => :spec
2
- require "rspec/core/rake_task"
3
- RSpec::Core::RakeTask.new(:spec) do |t|
4
- t.rspec_opts = '--backtrace --color'
1
+ task :default do
2
+ sh "AR='~>2' bundle && bundle exec rspec spec" # ActiveRecord 2
3
+ sh "AR='~>3' bundle && bundle exec rspec spec" # ActiveRecord 3
5
4
  end
6
5
 
7
6
  task :benchmark do
@@ -29,5 +28,5 @@ begin
29
28
 
30
29
  Jeweler::GemcutterTasks.new
31
30
  rescue LoadError
32
- puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
31
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
33
32
  end
data/Readme.md CHANGED
@@ -125,10 +125,20 @@ since it runs in a different thread then e.g. controllers, so set them inside yo
125
125
 
126
126
  Advanced features
127
127
  =================
128
- ###Abnormal pluralisation
129
- Pluralisation rules can be set directly via a lambda (see specs/), or by using the Gettext
130
- plural definition (see spec/locale/en/test_plural.po or [Plural expressions for all languages](http://translate.sourceforge.net/wiki/l10n/pluralforms).
128
+ ### Abnormal pluralisation
129
+ Plurals are selected by index, think of it as `['car', 'cars'][index]`<br/>
130
+ A pluralisation rules decides which form to use e.g. in english its `count == 1 ? 0 : 1`.<br/>
131
+ If you have any languages that do not fit this rule, you have to add a custom pluralisation rule.
131
132
 
133
+ Via Ruby:
134
+
135
+ FastGettext.pluralisation_rule = lamda{|count| count > 5 ? 1 : (count > 2 ? 0 : 2)}
136
+
137
+ Via mo/pofile:
138
+
139
+ Plural-Forms: nplurals=2; plural=n==2?3:4;
140
+
141
+ [Plural expressions for all languages](http://translate.sourceforge.net/wiki/l10n/pluralforms).
132
142
 
133
143
  ###default_text_domain
134
144
  If you only use one text domain, setting `FastGettext.default_text_domain = 'app'`
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.13
1
+ 0.6.0
data/fast_gettext.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fast_gettext}
8
- s.version = "0.5.13"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2011-06-30}
12
+ s.date = %q{2011-10-03}
13
13
  s.email = %q{michael@grosser.it}
14
14
  s.files = [
15
15
  "CHANGELOG",
@@ -55,6 +55,8 @@ Gem::Specification.new do |s|
55
55
  "lib/fast_gettext/vendor/poparser.rb",
56
56
  "lib/fast_gettext/vendor/string.rb",
57
57
  "spec/aa_unconfigued_spec.rb",
58
+ "spec/cases/interpolate_i18n_after_fast_gettext.rb",
59
+ "spec/cases/interpolate_i18n_before_fast_gettext.rb",
58
60
  "spec/fast_gettext/mo_file_spec.rb",
59
61
  "spec/fast_gettext/storage_spec.rb",
60
62
  "spec/fast_gettext/translation_repository/base_spec.rb",
@@ -89,27 +91,6 @@ Gem::Specification.new do |s|
89
91
  s.require_paths = ["lib"]
90
92
  s.rubygems_version = %q{1.6.2}
91
93
  s.summary = %q{A simple, fast, memory-efficient and threadsafe implementation of GetText}
92
- s.test_files = [
93
- "examples/db/migration.rb",
94
- "examples/missing_translation_logger.rb",
95
- "spec/aa_unconfigued_spec.rb",
96
- "spec/fast_gettext/mo_file_spec.rb",
97
- "spec/fast_gettext/storage_spec.rb",
98
- "spec/fast_gettext/translation_repository/base_spec.rb",
99
- "spec/fast_gettext/translation_repository/chain_spec.rb",
100
- "spec/fast_gettext/translation_repository/db_spec.rb",
101
- "spec/fast_gettext/translation_repository/logger_spec.rb",
102
- "spec/fast_gettext/translation_repository/mo_spec.rb",
103
- "spec/fast_gettext/translation_repository/po_spec.rb",
104
- "spec/fast_gettext/translation_repository/yaml_spec.rb",
105
- "spec/fast_gettext/translation_repository_spec.rb",
106
- "spec/fast_gettext/translation_spec.rb",
107
- "spec/fast_gettext/vendor/fake_load_path/iconv.rb",
108
- "spec/fast_gettext/vendor/iconv_spec.rb",
109
- "spec/fast_gettext/vendor/string_spec.rb",
110
- "spec/fast_gettext_spec.rb",
111
- "spec/spec_helper.rb"
112
- ]
113
94
 
114
95
  if s.respond_to? :specification_version then
115
96
  s.specification_version = 3
@@ -20,10 +20,9 @@ module FastGettext
20
20
  end
21
21
 
22
22
  #returns the plural forms or all singular translations that where found
23
+ # Car, Cars => [Auto,Autos] or []
23
24
  def plural(*msgids)
24
- translations = plural_translations(msgids)
25
- return translations unless translations.empty?
26
- msgids.map{|msgid| self[msgid] || msgid} #try to translate each id
25
+ split_plurals(self[msgids*PLURAL_SEPERATOR].to_s)
27
26
  end
28
27
 
29
28
  def pluralisation_rule
@@ -57,11 +56,5 @@ module FastGettext
57
56
  def split_plurals(singular_plural)
58
57
  singular_plural.split(PLURAL_SEPERATOR)
59
58
  end
60
-
61
- # Car, Cars => [Auto,Autos] or []
62
- def plural_translations(msgids)
63
- plurals = self[msgids*PLURAL_SEPERATOR]
64
- if plurals then split_plurals(plurals) else [] end
65
- end
66
59
  end
67
60
  end
@@ -95,15 +95,21 @@ module FastGettext
95
95
 
96
96
  def cached_find(key)
97
97
  translation = current_cache[key]
98
- return translation if translation or translation == false #found or was not found before
99
- current_cache[key] = current_repository[key] || false
98
+ if translation.nil? # uncached
99
+ current_cache[key] = current_repository[key] || false
100
+ else
101
+ translation
102
+ end
100
103
  end
101
104
 
102
105
  def cached_plural_find(*keys)
103
106
  key = '||||' + keys * '||||'
104
107
  translation = current_cache[key]
105
- return translation if translation or translation == false #found or was not found before
106
- current_cache[key] = current_repository.plural(*keys) || false
108
+ if translation.nil? # uncached
109
+ current_cache[key] = current_repository.plural(*keys) || false
110
+ else
111
+ translation
112
+ end
107
113
  end
108
114
 
109
115
  def locale
@@ -28,9 +28,18 @@ module FastGettext
28
28
  def n_(*keys)
29
29
  count = keys.pop
30
30
  translations = FastGettext.cached_plural_find *keys
31
+
31
32
  selected = FastGettext.pluralisation_rule.call(count)
32
- selected = selected ? 1 : 0 unless selected.is_a? Numeric #convert booleans to numbers
33
- translations[selected] || keys[selected] || keys.last
33
+ selected = (selected ? 1 : 0) unless selected.is_a? Numeric #convert booleans to numbers
34
+
35
+ result = translations[selected]
36
+ if result
37
+ result
38
+ elsif keys[selected]
39
+ _(keys[selected])
40
+ else
41
+ keys.last
42
+ end
34
43
  end
35
44
 
36
45
  #translate, but discard namespace if nothing was found
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'fast_gettext'
3
+ raise unless "%{a}" % {:a => 1} == '1'
4
+ require 'i18n/core_ext/string/interpolate'
5
+ require 'active_support/core_ext/string/output_safety'
6
+ raise unless "%{a}" % {:a => 1} == '1'
7
+ raise unless "%{a}".html_safe % {:a => 1} == '1'
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+
3
+ require 'i18n/core_ext/string/interpolate'
4
+ require 'active_support/core_ext/string/output_safety'
5
+ raise unless "%{a}" %{:a => 1} == '1'
6
+ raise unless "%{a}".html_safe % {:a => 1} == '1'
7
+ require 'fast_gettext'
8
+ raise unless "%{a}" %{:a => 1} == '1'
9
+ raise unless "%{a}".html_safe % {:a => 1} == '1'
@@ -20,15 +20,11 @@ describe FastGettext::MoFile do
20
20
  de.plural('Axis','Axis').should == ['Achse','Achsen']
21
21
  end
22
22
 
23
- it "returns singular translations when pluralisation could not be found" do
24
- de.plural('Axis','Axis','Axis').should == ['Achse','Achse','Achse']
25
- end
26
-
27
- it "returns ids when not plural and singular translations could not be found" do
28
- de.plural('Axis','Axis','NOTFOUND').should == ['Achse','Achse','NOTFOUND']
23
+ it "returns empty array when pluralisation could not be found" do
24
+ de.plural('Axis','Axis','Axis').should == []
29
25
  end
30
26
 
31
27
  it "can access plurals through []" do
32
28
  de['Axis'].should == 'Achse' #singular
33
29
  end
34
- end
30
+ end
@@ -15,6 +15,6 @@ describe 'FastGettext::TranslationRepository::Base' do
15
15
  end
16
16
 
17
17
  it "cannot pluralize" do
18
- @rep.plural('Axis','Axis').should == ['Axis','Axis']
18
+ @rep.plural('Axis','Axis').should == []
19
19
  end
20
- end
20
+ end
@@ -56,7 +56,7 @@ describe 'FastGettext::TranslationRepository::Chain' do
56
56
  end
57
57
 
58
58
  it "uses the second repo in the chain if the first does not respond" do
59
- @one.should_receive(:plural).with('a','b').and_return [nil,nil]
59
+ @one.should_receive(:plural).with('a','b').and_return []
60
60
  @two.should_receive(:plural).with('a','b').and_return ['A','B']
61
61
  @rep.plural('a','b').should == ['A','B']
62
62
  end
@@ -78,4 +78,4 @@ describe 'FastGettext::TranslationRepository::Chain' do
78
78
  end
79
79
  end
80
80
  end
81
- end
81
+ end
@@ -63,7 +63,11 @@ describe FastGettext::Translation do
63
63
  n_('singular','plural','c','d',2).should == 'plural'
64
64
  end
65
65
  end
66
-
66
+
67
+ it "returns a simple translation when no combined was found" do
68
+ n_('Axis','NOTFOUNDs',1).should == 'Achse'
69
+ end
70
+
67
71
  it "returns the appropriate key if no translation was found" do
68
72
  n_('NOTFOUND','NOTFOUNDs',1).should == 'NOTFOUND'
69
73
  n_('NOTFOUND','NOTFOUNDs',2).should == 'NOTFOUNDs'
@@ -155,4 +159,4 @@ describe FastGettext::Translation do
155
159
  _('car').should == 'Auto'
156
160
  end
157
161
  end
158
- end
162
+ end
@@ -1,6 +1,5 @@
1
1
  require File.expand_path('spec/spec_helper')
2
2
 
3
- #just to make sure we did not mess up while copying...
4
3
  describe String do
5
4
  before :all do
6
5
  if "i18n gem overwrites % method".respond_to?(:interpolate_without_ruby_19_syntax)
@@ -83,4 +82,18 @@ describe String do
83
82
  ("%{typo} xxx" % {:something=>1}).should == "%{typo} xxx"
84
83
  end
85
84
  end
86
- end
85
+
86
+ describe 'with i18n loaded' do
87
+ it "interpolates if i18n is loaded before" do
88
+ pending do
89
+ system("bundle exec ruby spec/cases/interpolate_i18n_before_fast_gettext.rb > /dev/null 2>&1").should == true
90
+ end
91
+ end
92
+
93
+ it "interpolates if i18n is loaded before" do
94
+ pending do
95
+ system("bundle exec ruby spec/cases/interpolate_i18n_after_fast_gettext.rb > /dev/null 2>&1").should == true
96
+ end
97
+ end
98
+ end
99
+ end
data/spec/spec_helper.rb CHANGED
@@ -11,13 +11,6 @@ RSpec.configure do |config|
11
11
  end
12
12
  end
13
13
 
14
- # ---- Helpers
15
- def pending_it(text,&block)
16
- it text do
17
- pending(&block)
18
- end
19
- end
20
-
21
14
  def default_setup
22
15
  FastGettext.add_text_domain('test',:path=>File.join(File.dirname(__FILE__),'locale'))
23
16
  FastGettext.text_domain = 'test'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_gettext
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 13
10
- version: 0.5.13
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-30 00:00:00 +02:00
18
+ date: 2011-10-03 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -71,6 +71,8 @@ files:
71
71
  - lib/fast_gettext/vendor/poparser.rb
72
72
  - lib/fast_gettext/vendor/string.rb
73
73
  - spec/aa_unconfigued_spec.rb
74
+ - spec/cases/interpolate_i18n_after_fast_gettext.rb
75
+ - spec/cases/interpolate_i18n_before_fast_gettext.rb
74
76
  - spec/fast_gettext/mo_file_spec.rb
75
77
  - spec/fast_gettext/storage_spec.rb
76
78
  - spec/fast_gettext/translation_repository/base_spec.rb
@@ -134,23 +136,5 @@ rubygems_version: 1.6.2
134
136
  signing_key:
135
137
  specification_version: 3
136
138
  summary: A simple, fast, memory-efficient and threadsafe implementation of GetText
137
- test_files:
138
- - examples/db/migration.rb
139
- - examples/missing_translation_logger.rb
140
- - spec/aa_unconfigued_spec.rb
141
- - spec/fast_gettext/mo_file_spec.rb
142
- - spec/fast_gettext/storage_spec.rb
143
- - spec/fast_gettext/translation_repository/base_spec.rb
144
- - spec/fast_gettext/translation_repository/chain_spec.rb
145
- - spec/fast_gettext/translation_repository/db_spec.rb
146
- - spec/fast_gettext/translation_repository/logger_spec.rb
147
- - spec/fast_gettext/translation_repository/mo_spec.rb
148
- - spec/fast_gettext/translation_repository/po_spec.rb
149
- - spec/fast_gettext/translation_repository/yaml_spec.rb
150
- - spec/fast_gettext/translation_repository_spec.rb
151
- - spec/fast_gettext/translation_spec.rb
152
- - spec/fast_gettext/vendor/fake_load_path/iconv.rb
153
- - spec/fast_gettext/vendor/iconv_spec.rb
154
- - spec/fast_gettext/vendor/string_spec.rb
155
- - spec/fast_gettext_spec.rb
156
- - spec/spec_helper.rb
139
+ test_files: []
140
+