diff --git a/Appraisals b/Appraisals
new file mode 100644
index 00000000..00018b99
--- /dev/null
+++ b/Appraisals
@@ -0,0 +1,5 @@
+['2', '3'].each do |version|
+ appraise "rails.#{version}" do
+ gem "rails", "~>#{version}.0"
+ end
+end
diff --git a/CHANGELOG b/CHANGELOG
index 89f7f0d7..80bddd82 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,4 @@
+0.7.0 -- set_locale resets to default locale if none of the available locales was tried to set
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
0.4.14 -- "" is translated as "", not as gettext meta information
0.4.0 -- pluralisation_rules is no longer stored in each repository, only retrived. Added Chain and Logger repository.
diff --git a/Gemfile b/Gemfile
index 9e1f39f6..b62cf0cf 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,9 +1,10 @@
-source :rubygems
+source "https://rubygems.org"
gemspec
+gem 'appraisal'
gem 'rake'
gem 'sqlite3'
-gem 'rspec', '~>2'
-gem 'activerecord', ENV['AR']
+gem 'rspec'
+gem 'activerecord'
gem 'i18n'
gem 'bump'
diff --git a/Gemfile.lock b/Gemfile.lock
index 347273a8..74f1174b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,48 +1,52 @@
PATH
remote: .
specs:
- fast_gettext (0.6.11)
+ fast_gettext (0.7.1)
GEM
- remote: http://rubygems.org/
+ remote: https://rubygems.org/
specs:
- activemodel (3.2.7)
- activesupport (= 3.2.7)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
builder (~> 3.0.0)
- activerecord (3.2.7)
- activemodel (= 3.2.7)
- activesupport (= 3.2.7)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
- activesupport (3.2.7)
+ activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
+ appraisal (0.5.1)
+ bundler
+ rake
arel (3.0.2)
- builder (3.0.0)
- bump (0.3.3)
+ builder (3.0.4)
+ bump (0.3.5)
diff-lcs (1.1.3)
- i18n (0.6.0)
- multi_json (1.3.6)
- rake (0.9.2)
- rspec (2.6.0)
- rspec-core (~> 2.6.0)
- rspec-expectations (~> 2.6.0)
- rspec-mocks (~> 2.6.0)
- rspec-core (2.6.4)
- rspec-expectations (2.6.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.6.0)
+ i18n (0.6.1)
+ multi_json (1.3.7)
+ rake (0.9.2.2)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.3)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.3)
sqlite3 (1.3.6)
- tzinfo (0.3.33)
+ tzinfo (0.3.35)
PLATFORMS
ruby
DEPENDENCIES
activerecord
+ appraisal
bump
fast_gettext!
i18n
rake
- rspec (~> 2)
+ rspec
sqlite3
diff --git a/Rakefile b/Rakefile
index 998c6c01..2238e9fa 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,11 +1,14 @@
+require 'bundler/setup'
require 'bundler/gem_tasks'
require 'bump/tasks'
+require 'appraisal'
task :default do
- ['~>2', '~>3'].each do |version|
- sh "export AR='#{version}' && (bundle check || bundle install) && bundle exec rspec spec"
- end
- sh "git checkout Gemfile.lock"
+ sh "rake appraisal:install && rake appraisal spec"
+end
+
+task :spec do
+ sh "rspec spec"
end
task :benchmark do
@@ -20,20 +23,3 @@ task :namespaces do
puts `ruby benchmark/namespace/original.rb`
puts `ruby benchmark/namespace/fast_gettext.rb`
end
-
-# extracted from https://github.com/grosser/project_template
-rule /^version:bump:.*/ do |t|
- sh "git status | grep 'nothing to commit'" # ensure we are not dirty
- index = ['major', 'minor','patch'].index(t.name.split(':').last)
- file = 'lib/fast_gettext/version.rb'
-
- version_file = File.read(file)
- old_version, *version_parts = version_file.match(/(\d+)\.(\d+)\.(\d+)/).to_a
- version_parts[index] = version_parts[index].to_i + 1
- version_parts[2] = 0 if index < 2 # remove patch for minor
- version_parts[1] = 0 if index < 1 # remove minor for major
- new_version = version_parts * '.'
- File.open(file,'w'){|f| f.write(version_file.sub(old_version, new_version)) }
-
- sh "bundle && git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
-end
diff --git a/Readme.md b/Readme.md
index dd3301b3..7e827156 100644
--- a/Readme.md
+++ b/Readme.md
@@ -83,6 +83,7 @@ Do this once in every Thread. (e.g. Rails -> ApplicationController)
_('not-found') == 'not-found'
s_('Namespace|no-found') == 'not-found'
n_('Axis','Axis',3) == 'Achsen' #German plural of Axis
+ _('Hello %{name}!') % {:name => "Pete"} == 'Hello Pete!'
Managing translations
@@ -127,7 +128,8 @@ since it runs in a different thread then e.g. controllers, so set them inside yo
def set_locale
FastGettext.available_locales = ['de','en',...]
FastGettext.text_domain = 'frontend'
- session[:locale] = I18n.locale = FastGettext.set_locale(params[:locale] || session[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
+ FastGettext.set_locale(params[:locale] || session[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'])
+ session[:locale] = I18n.locale = FastGettext.locale
end
@@ -224,8 +226,10 @@ Mo/Po-file parsing from Masao Mutoh, see vendor/README
- [Hoang Nghiem](https://github.com/hoangnghiem)
- [Costa Shapiro](https://github.com/costa)
- [Jamie Dyer](https://github.com/kernow)
+ - [Stephan Kulow](https://github.com/coolo)
+ - [Fotos Georgiadis](https://github.com/fotos)
[Michael Grosser](http://grosser.it)
michael@grosser.it
-License: MIT
-[](http://travis-ci.org/grosser/fast_gettext)
+License: MIT, some vendor parts under the same license terms as Ruby (see headers)
+[](https://travis-ci.org/grosser/fast_gettext)
diff --git a/fast_gettext.gemspec b/fast_gettext.gemspec
index ec98f4fd..802ec741 100644
--- a/fast_gettext.gemspec
+++ b/fast_gettext.gemspec
@@ -8,5 +8,5 @@ Gem::Specification.new name, FastGettext::VERSION do |s|
s.email = "michael@grosser.it"
s.homepage = "http://github.com/grosser/#{name}"
s.files = `git ls-files`.split("\n")
- s.license = "MIT"
+ s.licenses = ["MIT", "Ruby"]
end
diff --git a/gemfiles/rails.2.gemfile b/gemfiles/rails.2.gemfile
new file mode 100644
index 00000000..ca1d3270
--- /dev/null
+++ b/gemfiles/rails.2.gemfile
@@ -0,0 +1,14 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "appraisal"
+gem "rake"
+gem "sqlite3"
+gem "rspec"
+gem "activerecord"
+gem "i18n"
+gem "bump"
+gem "rails", "~>2.0"
+
+gemspec :path=>"../"
\ No newline at end of file
diff --git a/gemfiles/rails.2.gemfile.lock b/gemfiles/rails.2.gemfile.lock
new file mode 100644
index 00000000..2c6be0c2
--- /dev/null
+++ b/gemfiles/rails.2.gemfile.lock
@@ -0,0 +1,56 @@
+PATH
+ remote: /Users/mgrosser/code/tools/fast_gettext
+ specs:
+ fast_gettext (0.7.0)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ actionmailer (2.3.14)
+ actionpack (= 2.3.14)
+ actionpack (2.3.14)
+ activesupport (= 2.3.14)
+ rack (~> 1.1.0)
+ activerecord (2.3.14)
+ activesupport (= 2.3.14)
+ activeresource (2.3.14)
+ activesupport (= 2.3.14)
+ activesupport (2.3.14)
+ appraisal (0.5.1)
+ bundler
+ rake
+ bump (0.3.5)
+ diff-lcs (1.1.3)
+ i18n (0.6.1)
+ rack (1.1.3)
+ rails (2.3.14)
+ actionmailer (= 2.3.14)
+ actionpack (= 2.3.14)
+ activerecord (= 2.3.14)
+ activeresource (= 2.3.14)
+ activesupport (= 2.3.14)
+ rake (>= 0.8.3)
+ rake (0.9.2.2)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.3)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.3)
+ sqlite3 (1.3.6)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activerecord
+ appraisal
+ bump
+ fast_gettext!
+ i18n
+ rails (~> 2.0)
+ rake
+ rspec
+ sqlite3
diff --git a/gemfiles/rails.3.gemfile b/gemfiles/rails.3.gemfile
new file mode 100644
index 00000000..2bbdb6c8
--- /dev/null
+++ b/gemfiles/rails.3.gemfile
@@ -0,0 +1,14 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "appraisal"
+gem "rake"
+gem "sqlite3"
+gem "rspec"
+gem "activerecord"
+gem "i18n"
+gem "bump"
+gem "rails", "~>3.0"
+
+gemspec :path=>"../"
\ No newline at end of file
diff --git a/gemfiles/rails.3.gemfile.lock b/gemfiles/rails.3.gemfile.lock
new file mode 100644
index 00000000..c950024a
--- /dev/null
+++ b/gemfiles/rails.3.gemfile.lock
@@ -0,0 +1,112 @@
+PATH
+ remote: /Users/mgrosser/code/tools/fast_gettext
+ specs:
+ fast_gettext (0.7.0)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
+ mail (~> 2.4.4)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ appraisal (0.5.1)
+ bundler
+ rake
+ arel (3.0.2)
+ builder (3.0.4)
+ bump (0.3.5)
+ diff-lcs (1.1.3)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.1)
+ journey (1.0.4)
+ json (1.7.5)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.19)
+ multi_json (1.3.7)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.2)
+ rack (>= 1.0)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
+ bundler (~> 1.0)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (>= 0.14.6, < 2.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.3)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.3)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.6)
+ thor (0.16.0)
+ tilt (1.3.3)
+ treetop (1.4.12)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.35)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activerecord
+ appraisal
+ bump
+ fast_gettext!
+ i18n
+ rails (~> 3.0)
+ rake
+ rspec
+ sqlite3
diff --git a/lib/fast_gettext/storage.rb b/lib/fast_gettext/storage.rb
index 3d95b283..385099e7 100644
--- a/lib/fast_gettext/storage.rb
+++ b/lib/fast_gettext/storage.rb
@@ -98,6 +98,11 @@ def current_ext2=(cache)
Thread.current[:fast_gettext_current_ext2] = cache
end
+ def reload!
+ self.current_cache = {}
+ translation_repositories.values.each(&:reload)
+ end
+
#global, since re-parsing whole folders takes too much time...
@@translation_repositories={}
def translation_repositories
@@ -181,15 +186,15 @@ def set_ext2(new_locale)
end
def locale=(new_locale)
- new_locale = best_locale_in(new_locale)
- self._locale = new_locale if new_locale
+ set_locale(new_locale)
end
# for chaining: puts set_locale('xx') == 'xx' ? 'applied' : 'rejected'
# returns the current locale, not the one that was supplied
# like locale=(), whoes behavior cannot be changed
def set_locale(new_locale)
- self.locale = new_locale
+ new_locale = best_locale_in(new_locale)
+ self._locale = new_locale
locale
end
@@ -268,7 +273,7 @@ def update_current_cache
self.current_ext1 = nil
end
- if ext2
+ if ext2 && ext2.to_s.length > 0
#commented by rizwan. will see it later
#version = 0
#lang = TranslationLanguage.find_by_locale(ext2)
diff --git a/lib/fast_gettext/translation_repository/base.rb b/lib/fast_gettext/translation_repository/base.rb
index 3c80cc48..6c55c64c 100644
--- a/lib/fast_gettext/translation_repository/base.rb
+++ b/lib/fast_gettext/translation_repository/base.rb
@@ -25,6 +25,10 @@ def plural(*keys)
current_translations.plural(*keys)
end
+ def reload
+ true
+ end
+
protected
def current_translations
diff --git a/lib/fast_gettext/translation_repository/chain.rb b/lib/fast_gettext/translation_repository/chain.rb
index 93b2ee7a..8a993732 100644
--- a/lib/fast_gettext/translation_repository/chain.rb
+++ b/lib/fast_gettext/translation_repository/chain.rb
@@ -38,6 +38,11 @@ def plural(*keys)
end
[]
end
+
+ def reload
+ chain.each(&:reload)
+ super
+ end
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/fast_gettext/translation_repository/db_models/translation_key.rb b/lib/fast_gettext/translation_repository/db_models/translation_key.rb
index 4cfe053f..d7fe008c 100644
--- a/lib/fast_gettext/translation_repository/db_models/translation_key.rb
+++ b/lib/fast_gettext/translation_repository/db_models/translation_key.rb
@@ -1,15 +1,21 @@
class TranslationKey < ActiveRecord::Base
has_many :translations, :class_name => 'TranslationText', :dependent => :destroy
-
+
+ xss_terminate :sanitize => [:key]
+
accepts_nested_attributes_for :translations, :allow_destroy => true
validates_uniqueness_of :key
validates_presence_of :key
- attr_accessible :key, :translations, :translations_attributes
+ attr_accessible :key, :translations, :translations_attributes, :override, :front_end, :custom
before_save :normalize_newlines
+ def to_label
+ self.key.to_s
+ end
+
def self.translation(key, locale)
# p "#{key} #{locale}"
return if !key || key.to_s.length == 0
@@ -40,7 +46,9 @@ def self.load_all(locale)
translation_texts
map = {}
for text in translation_texts
- map[text.translation_key.key] = text.text
+ if text && text.translation_key
+ map[text.translation_key.key] = text.text
+ end
end
return map
end
diff --git a/lib/fast_gettext/translation_repository/db_models/translation_text.rb b/lib/fast_gettext/translation_repository/db_models/translation_text.rb
index 3132ecfb..3a152cbd 100644
--- a/lib/fast_gettext/translation_repository/db_models/translation_text.rb
+++ b/lib/fast_gettext/translation_repository/db_models/translation_text.rb
@@ -3,6 +3,7 @@ class TranslationText < ActiveRecord::Base
validates_presence_of :locale
validates_uniqueness_of :locale, :scope=>:translation_key_id
attr_accessible :text, :locale, :translation_key, :translation_key_id
+ xss_terminate :sanitize => [:text]
after_update :expire_cache
protected
diff --git a/lib/fast_gettext/translation_repository/mo.rb b/lib/fast_gettext/translation_repository/mo.rb
index 9a3bd353..5cc0e7b5 100644
--- a/lib/fast_gettext/translation_repository/mo.rb
+++ b/lib/fast_gettext/translation_repository/mo.rb
@@ -6,8 +6,8 @@ module TranslationRepository
# - provide access to translations in mo files
class Mo < Base
def initialize(name,options={})
- find_and_store_files(name,options)
super
+ reload
end
def available_locales
@@ -18,6 +18,11 @@ def pluralisation_rule
current_translations.pluralisation_rule
end
+ def reload
+ find_and_store_files(@name, @options)
+ super
+ end
+
protected
def find_and_store_files(name,options)
diff --git a/lib/fast_gettext/translation_repository/yaml.rb b/lib/fast_gettext/translation_repository/yaml.rb
index 3aafac52..6643e1bd 100644
--- a/lib/fast_gettext/translation_repository/yaml.rb
+++ b/lib/fast_gettext/translation_repository/yaml.rb
@@ -8,8 +8,8 @@ module TranslationRepository
# - provide access to translations in yaml files
class Yaml < Base
def initialize(name,options={})
- find_and_store_files(options)
super
+ reload
end
def available_locales
@@ -26,6 +26,11 @@ def pluralisation_rule
self['pluralisation_rule'] ? lambda{|n| eval(self['pluralisation_rule']) } : nil
end
+ def reload
+ find_and_store_files(@options)
+ super
+ end
+
protected
MAX_FIND_DEPTH = 10
@@ -69,4 +74,4 @@ def yaml_dot_notation(a,b)
end
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/fast_gettext/version.rb b/lib/fast_gettext/version.rb
index 3c056ba1..4c16aabb 100644
--- a/lib/fast_gettext/version.rb
+++ b/lib/fast_gettext/version.rb
@@ -1,3 +1,3 @@
module FastGettext
- VERSION = Version = '0.6.11'
+ VERSION = Version = '0.7.1'
end
diff --git a/spec/fast_gettext/storage_spec.rb b/spec/fast_gettext/storage_spec.rb
index e563bff5..e6a97c7a 100644
--- a/spec/fast_gettext/storage_spec.rb
+++ b/spec/fast_gettext/storage_spec.rb
@@ -23,14 +23,14 @@
text_domain.should == 'xxx'
end
- def thread_save(method, value_a, value_b)
+ def thread_safe(method, value_a, value_b)
send("#{method}=",value_a)
# mess around with other threads
100.times do
Thread.new {FastGettext.send("#{method}=",value_b)}
end
- sleep 0.1 # Ruby 1.9 cannot switch threads fat enough <-> spec fails without this WTF!
+ sleep 0.1 # Ruby 1.9 cannot switch threads fast enough <-> spec fails without this WTF!
!!(send(method) == value_a)
end
@@ -41,16 +41,22 @@ def thread_save(method, value_a, value_b)
:text_domain=>['xx','yy'],
:pluralisation_rule=>[lambda{|x|x==4},lambda{|x|x==1}]
}.each do |method, (value_a, value_b)|
- it "stores #{method} thread-save" do
- thread_save(method, value_a, value_b).should == true
+ it "stores #{method} thread safe" do
+ thread_safe(method, value_a, value_b).should == true
end
end
- it "stores translation_repositories non-thread-safe" do
- self.translation_repositories[:x]=1
- t = Thread.new{self.translation_repositories[:x]=2}
- t.join
- self.translation_repositories[:x].should == 2
+ context "non-thread safe" do
+ after do
+ self.translation_repositories.clear
+ end
+
+ it "stores translation_repositories" do
+ self.translation_repositories[:x]=1
+ t = Thread.new{self.translation_repositories[:x]=2}
+ t.join
+ self.translation_repositories[:x].should == 2
+ end
end
describe :pluralisation_rule do
@@ -65,7 +71,7 @@ def thread_save(method, value_a, value_b)
describe :default_locale do
it "stores default_locale non-thread-safe" do
- thread_save(:default_locale, 'de', 'en').should == false
+ thread_safe(:default_locale, 'de', 'en').should == false
end
it "does not overwrite locale" do
@@ -93,8 +99,8 @@ def thread_save(method, value_a, value_b)
end
describe :default_text_domain do
- it "stores default_text_domain non-thread-safe" do
- thread_save(:default_text_domain, 'xx', 'en').should == false
+ it "stores default_text_domain non-thread safe" do
+ thread_safe(:default_text_domain, 'xx', 'en').should == false
end
it "uses default_text_domain when text_domain is not set" do
@@ -112,7 +118,7 @@ def thread_save(method, value_a, value_b)
describe :default_available_locales do
it "stores default_available_locales non-thread-safe" do
- thread_save(:default_available_locales, ['xx'], ['yy']).should == false
+ thread_safe(:default_available_locales, ['xx'], ['yy']).should == false
end
it "converts locales to s" do
@@ -168,6 +174,12 @@ def thread_save(method, value_a, value_b)
self.set_locale('en').should == 'de'
end
+ it "set_locale resets to default with :reset_on_unknown" do
+ self.locale = 'de'
+ self.available_locales = ['fr']
+ self.set_locale('en').should == 'fr'
+ end
+
{
'Opera' => "de-DE,de;q=0.9,en;q=0.8",
'Firefox' => "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3",
@@ -210,6 +222,10 @@ def thread_save(method, value_a, value_b)
self.text_domain = rand(99999).to_s
end
+ after do
+ self.translation_repositories.clear
+ end
+
it "raises when a textdomain was empty" do
begin
FastGettext._('x')
@@ -282,6 +298,22 @@ def thread_save(method, value_a, value_b)
end
end
+ describe :reload! do
+ it "reloads all repositories" do
+ FastGettext.translation_repositories.each do |name, repository|
+ repository.should_receive(:reload)
+ end
+
+ FastGettext.reload!
+ end
+
+ it "clears the cache" do
+ FastGettext.should_receive(:current_cache=).with({})
+
+ FastGettext.reload!
+ end
+ end
+
describe :key_exist? do
it "does not find default keys" do
FastGettext._('abcde')
diff --git a/spec/fast_gettext/translation_repository/base_spec.rb b/spec/fast_gettext/translation_repository/base_spec.rb
index 7cc4f8a5..5e134ee4 100644
--- a/spec/fast_gettext/translation_repository/base_spec.rb
+++ b/spec/fast_gettext/translation_repository/base_spec.rb
@@ -17,4 +17,8 @@
it "cannot pluralize" do
@rep.plural('Axis','Axis').should == []
end
+
+ it "can be reloaded" do
+ @rep.reload.should be_true
+ end
end
diff --git a/spec/fast_gettext/translation_repository/chain_spec.rb b/spec/fast_gettext/translation_repository/chain_spec.rb
index c69c1d92..e7c5b568 100644
--- a/spec/fast_gettext/translation_repository/chain_spec.rb
+++ b/spec/fast_gettext/translation_repository/chain_spec.rb
@@ -27,6 +27,10 @@ def [](key)#should_receive :[] does not work so well...
it "has no pluralisation rule" do
@rep.pluralisation_rule.should == nil
end
+
+ it "returns true on reload" do
+ @rep.reload.should be_true
+ end
end
describe "filled chain" do
@@ -77,5 +81,18 @@ def [](key)#should_receive :[] does not work so well...
@rep.pluralisation_rule.should == 'x'
end
end
+
+ describe :reload do
+ it "reloads all repositories" do
+ @one.should_receive(:reload)
+ @two.should_receive(:reload)
+ @rep.reload
+ end
+
+ it "returns true" do
+ @rep.chain.each { |c| c.stub(:reload) }
+ @rep.reload.should be_true
+ end
+ end
end
end
diff --git a/spec/fast_gettext/translation_repository/mo_spec.rb b/spec/fast_gettext/translation_repository/mo_spec.rb
index 1b58f76d..e4048f1d 100644
--- a/spec/fast_gettext/translation_repository/mo_spec.rb
+++ b/spec/fast_gettext/translation_repository/mo_spec.rb
@@ -20,6 +20,29 @@
@rep.plural('Axis','Axis').should == ['Achse','Achsen']
end
+ describe :reload do
+ before do
+ mo_file = FastGettext::MoFile.new('spec/locale/de/LC_MESSAGES/test2.mo')
+
+ FastGettext::MoFile.stub(:new).and_return(FastGettext::MoFile.empty)
+ FastGettext::MoFile.stub(:new).with('spec/locale/de/LC_MESSAGES/test.mo').and_return(mo_file)
+ end
+
+ it "can reload" do
+ FastGettext.locale = 'de'
+
+ @rep['Untranslated'].should be_nil
+
+ @rep.reload
+
+ @rep['Untranslated'].should == 'Translated'
+ end
+
+ it "returns true" do
+ @rep.reload.should be_true
+ end
+ end
+
it "has access to the mo repositories pluralisation rule" do
FastGettext.locale = 'en'
rep = FastGettext::TranslationRepository.build('plural_test',:path=>File.join('spec','locale'))
diff --git a/spec/fast_gettext/translation_repository/yaml_spec.rb b/spec/fast_gettext/translation_repository/yaml_spec.rb
index 0aaaf1fe..69c3c8c0 100644
--- a/spec/fast_gettext/translation_repository/yaml_spec.rb
+++ b/spec/fast_gettext/translation_repository/yaml_spec.rb
@@ -33,6 +33,29 @@
@rep.plural('cars.axis').should == ['Achse', 'Achsen', nil, nil]
end
+ describe :reload do
+ before do
+ yaml = YAML.load_file('spec/locale/yaml/de2.yml')
+
+ YAML.stub(:load_file).and_return('en' => {}, 'de' => {})
+ YAML.stub(:load_file).with('spec/locale/yaml/de.yml').and_return(yaml)
+ end
+
+ it "can reload" do
+ FastGettext.locale = 'de'
+
+ @rep['cars.car'].should == 'Auto'
+
+ @rep.reload
+
+ @rep['cars.car'].should == 'Aufzugskabine'
+ end
+
+ it "returns true" do
+ @rep.reload.should be_true
+ end
+ end
+
it "handles unfound plurals with nil" do
@rep.plural('cars.xxx').should == [nil, nil, nil, nil]
end
diff --git a/spec/fast_gettext_spec.rb b/spec/fast_gettext_spec.rb
index 7a337288..45d32a8f 100644
--- a/spec/fast_gettext_spec.rb
+++ b/spec/fast_gettext_spec.rb
@@ -24,6 +24,9 @@ def self.xx
it "provides access to FastGettext::Translations methods" do
FastGettext._('car').should == 'Auto'
_('car').should == 'Auto'
+ _("%{relative_time} ago").should == "vor %{relative_time}"
+ (_("%{relative_time} ago") % {:relative_time => 1}).should == "vor 1"
+ (N_("%{relative_time} ago") % {:relative_time => 1}).should == "1 ago"
s_("XXX|not found").should == "not found"
n_('Axis','Axis',1).should == 'Achse'
N_('XXXXX').should == 'XXXXX'
diff --git a/spec/locale/de/LC_MESSAGES/test2.mo b/spec/locale/de/LC_MESSAGES/test2.mo
new file mode 100644
index 00000000..d27b4530
Binary files /dev/null and b/spec/locale/de/LC_MESSAGES/test2.mo differ
diff --git a/spec/locale/de/test2.po b/spec/locale/de/test2.po
new file mode 100644
index 00000000..27f5588a
--- /dev/null
+++ b/spec/locale/de/test2.po
@@ -0,0 +1,71 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR , YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: version 0.0.1\n"
+"POT-Creation-Date: 2009-02-26 19:50+0100\n"
+"PO-Revision-Date: 2011-12-04 18:54+0900\n"
+"Last-Translator: FULL NAME \n"
+"Language-Team: LANGUAGE \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
+
+#: app/helpers/translation_helper.rb:3
+msgid "%{relative_time} ago"
+msgstr "vor %{relative_time}"
+
+#: app/views/cars/show.html.erb:5
+msgid "Axis"
+msgid_plural "Axis"
+msgstr[0] "Achse"
+msgstr[1] "Achsen"
+
+#: app/controllers/cars_controller.rb:47
+msgid "Car was successfully created."
+msgstr "Auto wurde erfolgreich gespeichert"
+
+#: app/controllers/cars_controller.rb:64
+msgid "Car was successfully updated."
+msgstr "Auto wurde erfolgreich aktualisiert"
+
+#: app/views/cars/show.html.erb:1 locale/model_attributes.rb:3
+msgid "Car|Model"
+msgstr "Modell"
+
+msgid "Untranslated"
+msgstr "Translated"
+
+#: app/views/cars/show.html.erb:3 locale/model_attributes.rb:4
+msgid "Car|Wheels count"
+msgstr "Räderzahl"
+
+#: app/views/cars/show.html.erb:7
+msgid "Created"
+msgstr "Erstellt"
+
+#: app/views/cars/show.html.erb:9
+msgid "Month"
+msgstr "Monat"
+
+#: locale/model_attributes.rb:2
+msgid "car"
+msgstr "Auto"
+
+#: locale/testlog_phrases.rb:2
+msgid "this is a dynamic translation which was found thorugh gettext_test_log!"
+msgstr ""
+"Dies ist eine dynamische Übersetzung, die durch gettext_test_log "
+"gefunden wurde!"
+
+#: locale/test_escape.rb:2
+msgid "You should escape '\\' as '\\\\'."
+msgstr "Du solltest '\\' als '\\\\' escapen."
+
+msgid "Umläüte"
+msgstr "Umlaute"
diff --git a/spec/locale/yaml/de2.yml b/spec/locale/yaml/de2.yml
new file mode 100644
index 00000000..50b33ba2
--- /dev/null
+++ b/spec/locale/yaml/de2.yml
@@ -0,0 +1,25 @@
+de:
+ simple: einfach
+ date:
+ relative: "vor %{relative_time}"
+
+ cars:
+ axis:
+ one: "Achse"
+ other: "Achsen"
+ silly:
+ one: '0'
+ other: '1'
+ plural2: '2'
+ plural3: '3'
+ model: "Modell"
+ wheel_count: "Räderzahl"
+ created: "Erstellt"
+ month: "Monat"
+ car: "Aufzugskabine"
+ messages:
+ created: "Auto wurde erfolgreich gespeichert"
+ updated: "Auto wurde erfolgreich aktualisiert"
+
+ test_log:
+ phrases: "Dies ist eine dynamische Übersetzung, die durch gettext_test_log gefunden wurde!"
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index df355c77..1d9f7463 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -7,8 +7,9 @@
# ---- revert to defaults
RSpec.configure do |config|
config.before do
- FastGettext.locale = 'de'
+ FastGettext.default_available_locales = nil
FastGettext.available_locales = nil
+ FastGettext.locale = 'de'
end
end