Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic functionality for global locale setter in place

  • Loading branch information...
commit b1f37d766417789ce0973e77e79010d0f698b88a 1 parent 154d2c6
Cameron Dutro authored
Showing with 55 additions and 7 deletions.
  1. +55 −7 lib/twitter_cldr.rb
62 lib/twitter_cldr.rb
View
@@ -54,21 +54,39 @@ module TwitterCldr
class << self
+ attr_accessor :locale
+
def resources
@resources ||= TwitterCldr::Resources::Loader.new
end
- def get_locale
- if defined?(FastGettext)
- locale = FastGettext.locale
- locale = DEFAULT_LOCALE if locale.to_s.empty?
- else
- locale = DEFAULT_LOCALE
- end
+ def locale=(new_locale)
+ @locale = new_locale
+ end
+ def locale
+ locale = @locale ? @locale : find_fallback
+ locale = DEFAULT_LOCALE if locale.to_s.empty?
(supported_locale?(locale) ? locale : DEFAULT_LOCALE).to_sym
end
+ def with_locale(locale)
+ raise "Unsupported locale" unless supported_locale?(locale)
+ old_locale = @locale
+ @locale = locale
+ yield
+ @locale = old_locale
+ end
+
+ def register_locale_fallback(proc_or_locale)
+ case proc_or_locale.class.to_s
+ when "Symbol", "String", "Proc"
+ locale_fallbacks << proc_or_locale
+ else
+ raise "A locale fallback must be of type String, Symbol, or Proc."
+ end
+ end
+
def convert_locale(locale)
locale = locale.to_sym
TWITTER_LOCALE_MAP.fetch(locale, locale)
@@ -86,8 +104,38 @@ def supported_locales
def supported_locale?(locale)
!!locale && supported_locales.include?(convert_locale(locale))
end
+
+ protected
+
+ def find_fallback
+ (locale_fallbacks.size - 1).downto(0).each do |i|
+ result = if locale_fallbacks[i].is_a?(Proc)
+ begin
+ locale_fallbacks[i].call
+ rescue
+ nil
+ end
+ else
+ locale_fallbacks[i]
+ end
+ return result if result
+ end
+ end
+
+ def locale_fallbacks
+ @locale_fallbacks ||= []
+ 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)
+
require 'twitter_cldr/core_ext'
Please sign in to comment.
Something went wrong with that request. Please try again.