Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Modifying with_locale to not blow up if the user passes an unsupporte…

…d locale
  • Loading branch information...
commit 87cf183e1a892550013c01596c82fa832ced8b7a 1 parent e205037
Cameron Dutro authored
Showing with 16 additions and 16 deletions.
  1. +9 −16 lib/twitter_cldr.rb
  2. +7 −0 spec/twitter_cldr_spec.rb
View
25 lib/twitter_cldr.rb
@@ -72,12 +72,15 @@ def locale
def with_locale(locale)
raise "Unsupported locale" unless supported_locale?(locale)
- old_locale = @locale
- @locale = locale
- result = yield
- ensure
- @locale = old_locale
- result
+
+ begin
+ old_locale = @locale
+ @locale = locale
+ result = yield
+ ensure
+ @locale = old_locale
+ result
+ end
end
def register_locale_fallback(proc_or_locale)
@@ -100,16 +103,6 @@ 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
7 spec/twitter_cldr_spec.rb
@@ -278,6 +278,13 @@
TwitterCldr::Shared::Languages.from_code(:es).should == "Spanish"
end
+ it "doesn't mess up if the given locale isn't supported" do
+ TwitterCldr.locale = :pt
+ TwitterCldr.locale.should == :pt
+ lambda { TwitterCldr.with_locale(:xx) {} }.should raise_error
+ TwitterCldr.locale.should == :pt
+ end
+
it "switches the locale back to the original if the block raises an error" do
TwitterCldr.locale.should == :en
locale_inside_block = nil
Please sign in to comment.
Something went wrong with that request. Please try again.