Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding a global locale setter

  • Loading branch information...
commit 4cc804649149595643a6f4a2ef20befc3952f94d 1 parent 21f4945
@camertron camertron authored Cameron Dutro committed
Showing with 99 additions and 2 deletions.
  1. +12 −1 lib/twitter_cldr.rb
  2. +87 −1 spec/twitter_cldr_spec.rb
View
13 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'
@@ -100,6 +101,16 @@ def locale_fallbacks
@locale_fallbacks ||= []
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 if locale.respond_to?(:to_sym)
TWITTER_LOCALE_MAP.fetch(locale, locale)
View
88 spec/twitter_cldr_spec.rb
@@ -159,6 +159,92 @@
TwitterCldr.locale.should == :en
end
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
@@ -213,4 +299,4 @@
TwitterCldr.get_locale_resource(:de, :numbers).should == 'result'
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.