Browse files

Adding a global locale setter

  • Loading branch information...
1 parent b1f37d7 commit 3a540ca2203cfe91a5d69c9462cc9b0bc40bf5bf @camertron camertron committed with Cameron Dutro Dec 8, 2012
View
2 README.md
@@ -613,7 +613,7 @@ TwitterCldr.convert_locale(:msa) # :ms
TwitterCldr.convert_locale(:ms) # :ms
```
-There are a few functions in TwitterCLDR that don't require a locale code, and instead use the default locale by calling `TwitterCldr.get_locale`. The `get_locale` function defers to `FastGettext.locale` when the FastGettext library is available, and falls back on :en (English) when it's not. (Twitter uses the FastGettext gem to retrieve translations efficiently in Ruby).
+There are a few functions in TwitterCLDR that don't require a locale code, and instead use the default locale by calling `TwitterCldr.locale`. The `locale` function defers to `FastGettext.locale` when the FastGettext library is available, and falls back on :en (English) when it's not. (Twitter uses the FastGettext gem to retrieve translations efficiently in Ruby).
```ruby
TwitterCldr.get_locale # will return :en
View
30 lib/twitter_cldr.rb
@@ -16,7 +16,8 @@
require 'twitter_cldr/version'
Enumerator = Enumerable::Enumerator unless defined?(Enumerator)
-
+require 'pry'
+require 'pry-nav'
module TwitterCldr
autoload :Formatters, 'twitter_cldr/formatters'
@@ -65,7 +66,7 @@ def locale=(new_locale)
end
def locale
- locale = @locale ? @locale : find_fallback
+ locale = supported_locale?(@locale) ? @locale : find_fallback
locale = DEFAULT_LOCALE if locale.to_s.empty?
(supported_locale?(locale) ? locale : DEFAULT_LOCALE).to_sym
end
@@ -87,8 +88,18 @@ def register_locale_fallback(proc_or_locale)
end
end
+ def reset_locale_fallbacks
+ locale_fallbacks.clear
+ TwitterCldr.register_locale_fallback(lambda { I18n.locale if defined?(I18n) && I18n.respond_to?(:locale) })
+ TwitterCldr.register_locale_fallback(lambda { FastGettext.locale if defined?(FastGettext) && FastGettext.respond_to?(:locale) })
+ end
+
+ def locale_fallbacks
+ @locale_fallbacks ||= []
+ end
+
def convert_locale(locale)
- locale = locale.to_sym
+ locale = locale.to_sym if locale.respond_to?(:to_sym)
TWITTER_LOCALE_MAP.fetch(locale, locale)
end
@@ -120,22 +131,13 @@ def find_fallback
end
return result if result
end
- end
-
- def locale_fallbacks
- @locale_fallbacks ||= []
+ nil
end
end
end
-TwitterCldr.register_locale_fallback(lambda do
- I18n.locale if defined?(I18n) && I18n.respond_to?(:locale)
-end)
-
-TwitterCldr.register_locale_fallback(lambda do
- FastGettext.locale if defined?(FastGettext) && FastGettext.respond_to?(:locale)
-end)
+TwitterCldr.reset_locale_fallbacks
require 'twitter_cldr/core_ext'
View
4 lib/twitter_cldr/formatters/plurals/rules.rb
@@ -11,7 +11,7 @@ module Rules
class << self
def all
- all_for(TwitterCldr.get_locale)
+ all_for(TwitterCldr.locale)
end
def all_for(locale)
@@ -20,7 +20,7 @@ def all_for(locale)
nil
end
- def rule_for(number, locale = TwitterCldr.get_locale)
+ def rule_for(number, locale = TwitterCldr.locale)
get_resource(locale)[:rule].call(number)
rescue
:other
View
2 lib/twitter_cldr/localized/localized_object.rb
@@ -27,7 +27,7 @@ def formatter_const
def self.localize(klass)
klass.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
- def localize(locale = TwitterCldr.get_locale, options = {})
+ def localize(locale = TwitterCldr.locale, options = {})
#{self}.new(self, locale, options)
end
LOCALIZE
View
2 lib/twitter_cldr/localized/localized_timespan.rb
@@ -8,7 +8,7 @@ module Localized
class LocalizedTimespan < LocalizedObject
def initialize(seconds, options = {})
- super(seconds, options[:locale] || TwitterCldr.get_locale, options)
+ super(seconds, options[:locale] || TwitterCldr.locale, options)
end
def to_s(options = {})
View
2 lib/twitter_cldr/shared/calendar.rb
@@ -32,7 +32,7 @@ class Calendar
attr_reader :locale, :calendar_type
- def initialize(locale = TwitterCldr.get_locale, calendar_type = TwitterCldr::DEFAULT_CALENDAR_TYPE)
+ def initialize(locale = TwitterCldr.locale, calendar_type = TwitterCldr::DEFAULT_CALENDAR_TYPE)
@locale = TwitterCldr.convert_locale(locale)
@calendar_type = calendar_type
end
View
8 lib/twitter_cldr/shared/languages.rb
@@ -10,7 +10,7 @@ module Languages
class << self
def all
- all_for(TwitterCldr.get_locale)
+ all_for(TwitterCldr.locale)
end
def all_for(code)
@@ -20,16 +20,16 @@ def all_for(code)
end
def from_code(code)
- from_code_for_locale(code, TwitterCldr.get_locale)
+ from_code_for_locale(code, TwitterCldr.locale)
end
- def from_code_for_locale(code, locale = TwitterCldr.get_locale)
+ def from_code_for_locale(code, locale = TwitterCldr.locale)
get_resource(locale)[:languages][TwitterCldr.convert_locale(code)]
rescue
nil
end
- def translate_language(language, source_locale = :en, dest_locale = TwitterCldr.get_locale)
+ def translate_language(language, source_locale = :en, dest_locale = TwitterCldr.locale)
lang_code = get_resource(source_locale)[:languages].detect { |_, val| val.downcase == language.downcase }.first
get_resource(dest_locale)[:languages][lang_code] if lang_code
rescue
View
2 lib/twitter_cldr/shared/numbers.rb
@@ -9,7 +9,7 @@ module Numbers
class << self
- def symbols(locale = TwitterCldr.get_locale)
+ def symbols(locale = TwitterCldr.locale)
get_resource(TwitterCldr.convert_locale(locale))[:symbols] rescue nil
end
View
2 spec/formatters/plurals/rules_spec.rb
@@ -60,7 +60,7 @@
describe "#all" do
it "gets rules for the default locale (usually supplied by FastGettext)" do
- mock(TwitterCldr).get_locale { :ru }
+ mock(TwitterCldr).locale { :ru }
Rules.all.should =~ [:one, :few, :many, :other]
end
end
View
2 spec/localized/localized_object_spec.rb
@@ -81,7 +81,7 @@ def formatter_const
end
it 'uses default locale and empty options hash by default' do
- mock(LocalizedClass).new(localizable_object, TwitterCldr.get_locale, {})
+ mock(LocalizedClass).new(localizable_object, TwitterCldr.locale, {})
localizable_object.localize
end
end
View
2 spec/localized/localized_symbol_spec.rb
@@ -12,7 +12,7 @@
describe "#as_language_code" do
it "returns the correct localized language from the symbol" do
:es.localize.as_language_code.should == "Spanish"
- FastGettext.locale = :es
+ TwitterCldr.locale = :es
:es.localize.as_language_code.should == "español"
end
View
5 spec/readme_spec.rb
@@ -273,8 +273,9 @@ def spaces(str)
end
it "verifies locale defaults" do
- TwitterCldr.get_locale.should == :en
+ TwitterCldr.locale.should == :en
+ TwitterCldr.locale = nil
FastGettext.locale = "ru"
- TwitterCldr.get_locale.should == :ru
+ TwitterCldr.locale.should == :ru
end
end
View
2 spec/shared/calendar_spec.rb
@@ -18,7 +18,7 @@
describe '#initialize' do
it 'returns calendar for default locale and type' do
- stub(TwitterCldr).get_locale { :fr }
+ stub(TwitterCldr).locale { :fr }
cal = Calendar.new
cal.locale.should == :fr
View
6 spec/shared/languages_spec.rb
@@ -28,7 +28,7 @@
it "defaults source and destination language to English if not given" do
Languages.translate_language("Russian").should == "Russian"
- FastGettext.locale = :es
+ TwitterCldr.locale = :es
Languages.translate_language("Russian").should == "ruso"
end
@@ -53,7 +53,7 @@
it "should return the language in the default locale for the given locale code" do
Languages.from_code(:es).should == "Spanish"
Languages.from_code(:ru).should == "Russian"
- FastGettext.locale = :es
+ TwitterCldr.locale = :es
Languages.from_code(:es).should == "español"
end
end
@@ -78,7 +78,7 @@
langs[:ru].should == "Russian"
langs[:de].should == "German"
- FastGettext.locale = :es
+ TwitterCldr.locale = :es
langs = Languages.all
langs.should be_a(Hash)
langs[:ru].should == "ruso"
View
2 spec/shared/numbers_spec.rb
@@ -12,7 +12,7 @@
let(:symbols) { { :nan => 'NaN', :minus_sign => '-' } }
it 'returns numerical symbols for default locale' do
- stub(TwitterCldr).get_locale { :jp }
+ stub(TwitterCldr).locale { :jp }
stub(TwitterCldr).get_locale_resource(:jp, :numbers) { { :jp => { :numbers => { :symbols => symbols } } } }
TwitterCldr::Shared::Numbers.symbols.should == symbols
end
View
21 spec/spec_helper.rb
@@ -21,8 +21,22 @@ def locale
@@locale
end
- def locale=(value)
- @@locale = value
+ def locale=(new_locale)
+ @@locale = new_locale
+ end
+ end
+end
+
+class I18n
+ class << self
+ @@locale = :en
+
+ def locale
+ @@locale
+ end
+
+ def locale=(new_locale)
+ @@locale = new_locale
end
end
end
@@ -35,7 +49,10 @@ def locale=(value)
config.filter_run_excluding(:slow => true) unless ENV['FULL_SPEC']
config.before(:each) do
+ TwitterCldr.reset_locale_fallbacks
+ TwitterCldr.locale = :en
FastGettext.locale = :en
+ I18n.locale = :en
end
end
View
102 spec/twitter_cldr_spec.rb
@@ -62,16 +62,102 @@
end
end
- describe "#get_locale" do
- context "with FastGettext locale" do
- it "should return the FastGettext locale" do
- mock(FastGettext).locale { "es" }
- TwitterCldr.get_locale.should == :es
+ describe "#locale" do
+ context "with explicit locale" do
+ it "should return the same locale the user sets (if it's supported)" do
+ TwitterCldr.locale = :es
+ TwitterCldr.locale.should == :es
end
- it "should return the default locale if the FastGettext locale is unsupported" do
- mock(FastGettext).locale { "bogus" }
- TwitterCldr.get_locale.should == TwitterCldr::DEFAULT_LOCALE
+ it "should convert strings to symbols" do
+ TwitterCldr.locale = "es"
+ TwitterCldr.locale.should == :es
+ end
+
+ it "should fall back if the user sets an unsupported locale" do
+ FastGettext.locale = :ko
+ TwitterCldr.locale = "blarg"
+ TwitterCldr.locale.should == :ko
+
+ FastGettext.locale = nil
+ I18n.locale = :hu
+ TwitterCldr.locale.should == :hu
+ end
+ end
+
+ context "with implicit locale (fallbacks)" do
+ before(:each) do
+ TwitterCldr.locale = nil
+ end
+
+ it "should return FastGettext locale before I18n locale and fall back gracefully" do
+ FastGettext.locale = :pt
+ I18n.locale = :ar
+ TwitterCldr.locale.should == :pt
+
+ FastGettext.locale = nil
+ TwitterCldr.locale.should == :ar
+
+ I18n.locale = nil
+ TwitterCldr.locale.should == :en
+ end
+
+ context "with only FastGettext locale" do
+ before(:each) do
+ I18n.locale = nil # disable I18n fallback
+ end
+
+ it "should return the FastGettext locale if it's supported" do
+ FastGettext.locale = "vi"
+ TwitterCldr.locale.should == :vi
+ end
+
+ it "should return the default locale if the FastGettext locale is unsupported" do
+ FastGettext.locale = "bogus"
+ TwitterCldr.locale.should == TwitterCldr::DEFAULT_LOCALE
+ end
+ end
+
+ context "with only I18n locale" do
+ before(:each) do
+ FastGettext.locale = nil # disable FastGettext fallback
+ end
+
+ it "should return the I18n locale if it's supported" do
+ I18n.locale = "ru"
+ TwitterCldr.locale.should == :ru
+ end
+
+ it "should return the default locale if the I18n locale is unsupported" do
+ I18n.locale = "bogus"
+ TwitterCldr.locale.should == TwitterCldr::DEFAULT_LOCALE
+ end
+ end
+
+ context "with a custom fallback" do
+ before(:each) do
+ @allow = false
+ TwitterCldr.register_locale_fallback(lambda { @allow ? :uk : nil })
+ end
+
+ it "should fall back to the custom locale" do
+ TwitterCldr.locale.should == :en
+ @allow = true
+ TwitterCldr.locale.should == :uk
+ end
+
+ it "should fall back to the next fallback option if the custom one returns nil" do
+ FastGettext.locale = :lv
+ TwitterCldr.locale.should == :lv
+ @allow = true
+ TwitterCldr.locale.should == :uk
+ end
+
+ it "should not return the fallback locale if it's unsupported" do
+ TwitterCldr.reset_locale_fallbacks
+ TwitterCldr.register_locale_fallback(lambda { :zzz })
+ TwitterCldr.locale.should == :en
+ end
end
end
end

0 comments on commit 3a540ca

Please sign in to comment.