Module: TwitterCldr

Extended by:
SingleForwardable
Defined in:
lib/twitter_cldr/utils.rb,
lib/twitter_cldr.rb,
lib/twitter_cldr/shared.rb,
lib/twitter_cldr/parsers.rb,
lib/twitter_cldr/version.rb,
lib/twitter_cldr/collation.rb,
lib/twitter_cldr/localized.rb,
lib/twitter_cldr/resources.rb,
lib/twitter_cldr/formatters.rb,
lib/twitter_cldr/tokenizers.rb,
lib/twitter_cldr/utils/yaml.rb,
lib/twitter_cldr/shared/bidi.rb,
lib/twitter_cldr/normalization.rb,
lib/twitter_cldr/collation/trie.rb,
lib/twitter_cldr/shared/numbers.rb,
lib/twitter_cldr/formatters/base.rb,
lib/twitter_cldr/shared/calendar.rb,
lib/twitter_cldr/tokenizers/base.rb,
lib/twitter_cldr/resources/loader.rb,
lib/twitter_cldr/shared/languages.rb,
lib/twitter_cldr/tokenizers/token.rb,
lib/twitter_cldr/normalization/nfc.rb,
lib/twitter_cldr/normalization/nfd.rb,
lib/twitter_cldr/shared/code_point.rb,
lib/twitter_cldr/shared/currencies.rb,
lib/twitter_cldr/utils/code_points.rb,
lib/twitter_cldr/utils/territories.rb,
lib/twitter_cldr/collation/collator.rb,
lib/twitter_cldr/formatters/numbers.rb,
lib/twitter_cldr/formatters/plurals.rb,
lib/twitter_cldr/normalization/base.rb,
lib/twitter_cldr/normalization/nfkc.rb,
lib/twitter_cldr/normalization/nfkd.rb,
lib/twitter_cldr/resources/download.rb,
lib/twitter_cldr/shared/phone_codes.rb,
lib/twitter_cldr/shared/territories.rb,
lib/twitter_cldr/shared/postal_codes.rb,
lib/twitter_cldr/utils/interpolation.rb,
lib/twitter_cldr/normalization/hangul.rb,
lib/twitter_cldr/collation/trie_loader.rb,
lib/twitter_cldr/parsers/number_parser.rb,
lib/twitter_cldr/shared/language_codes.rb,
lib/twitter_cldr/collation/trie_builder.rb,
lib/twitter_cldr/formatters/plurals/rules.rb,
lib/twitter_cldr/localized/localized_date.rb,
lib/twitter_cldr/localized/localized_hash.rb,
lib/twitter_cldr/localized/localized_time.rb,
lib/twitter_cldr/formatters/list_formatter.rb,
lib/twitter_cldr/localized/localized_array.rb,
lib/twitter_cldr/collation/sort_key_builder.rb,
lib/twitter_cldr/localized/localized_number.rb,
lib/twitter_cldr/localized/localized_object.rb,
lib/twitter_cldr/localized/localized_string.rb,
lib/twitter_cldr/localized/localized_symbol.rb,
lib/twitter_cldr/tokenizers/composite_token.rb,
lib/twitter_cldr/collation/trie_with_fallback.rb,
lib/twitter_cldr/localized/localized_datetime.rb,
lib/twitter_cldr/localized/localized_timespan.rb,
lib/twitter_cldr/resources/bidi_test_importer.rb,
lib/twitter_cldr/resources/tailoring_importer.rb,
lib/twitter_cldr/resources/phone_codes_importer.rb,
lib/twitter_cldr/formatters/numbers/helpers/base.rb,
lib/twitter_cldr/resources/postal_codes_importer.rb,
lib/twitter_cldr/resources/unicode_data_importer.rb,
lib/twitter_cldr/resources/collation_tries_dumper.rb,
lib/twitter_cldr/resources/language_codes_importer.rb,
lib/twitter_cldr/formatters/numbers/helpers/integer.rb,
lib/twitter_cldr/formatters/calendars/date_formatter.rb,
lib/twitter_cldr/formatters/calendars/time_formatter.rb,
lib/twitter_cldr/formatters/numbers/helpers/fraction.rb,
lib/twitter_cldr/formatters/numbers/number_formatter.rb,
lib/twitter_cldr/formatters/plurals/plural_formatter.rb,
lib/twitter_cldr/tokenizers/calendars/date_tokenizer.rb,
lib/twitter_cldr/tokenizers/calendars/time_tokenizer.rb,
lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb,
lib/twitter_cldr/formatters/numbers/decimal_formatter.rb,
lib/twitter_cldr/formatters/numbers/percent_formatter.rb,
lib/twitter_cldr/resources/locales_resources_importer.rb,
lib/twitter_cldr/collation/implicit_collation_elements.rb,
lib/twitter_cldr/formatters/numbers/currency_formatter.rb,
lib/twitter_cldr/formatters/calendars/datetime_formatter.rb,
lib/twitter_cldr/formatters/calendars/timespan_formatter.rb,
lib/twitter_cldr/tokenizers/calendars/datetime_tokenizer.rb,
lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb,
lib/twitter_cldr/resources/canonical_compositions_updater.rb,
lib/twitter_cldr/resources/composition_exclusions_importer.rb,
lib/twitter_cldr/resources/custom_locales_resources_importer.rb,
lib/twitter_cldr/tokenizers/calendars/additional_date_format_selector.rb,
lib/twitter_cldr/formatters/numbers/abbreviated/long_decimal_formatter.rb,
lib/twitter_cldr/formatters/numbers/abbreviated/short_decimal_formatter.rb,
lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb

Overview

Copyright 2012 Twitter, Inc www.apache.org/licenses/LICENSE-2.0

Defined Under Namespace

Modules: Collation, Formatters, Localized, Normalization, Parsers, Resources, Shared, Tokenizers, Utils

Constant Summary collapse

CLDR_VERSION =

version of CLDR that was used for generating YAML files in the resources/ directory

'21.0'
DEFAULT_LOCALE =

release date: 2012-02-10

:en
DEFAULT_CALENDAR_TYPE =
:gregorian
RESOURCES_DIR =
File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'resources')
TWITTER_LOCALE_MAP =

maps twitter locales to cldr locales

{
    :msa     => :ms,
    :'zh-cn' => :zh,
    :'zh-tw' => :'zh-Hant',
    :no      => :nb
}
CLDR_LOCALE_MAP =

maps cldr locales to twitter locales

TWITTER_LOCALE_MAP.invert
VERSION =
"2.4.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.disable_custom_locale_resourcesObject

Returns the value of attribute disable_custom_locale_resources.



59
60
61
# File 'lib/twitter_cldr.rb', line 59

def disable_custom_locale_resources
  @disable_custom_locale_resources
end

.localeObject



65
66
67
68
69
# File 'lib/twitter_cldr.rb', line 65

def locale
  locale = supported_locale?(@locale) ? @locale : find_fallback
  locale = DEFAULT_LOCALE if locale.to_s.empty?
  (supported_locale?(locale) ? locale : DEFAULT_LOCALE).to_sym
end

Class Method Details

.convert_locale(locale) ⇒ Object



104
105
106
107
# File 'lib/twitter_cldr.rb', line 104

def convert_locale(locale)
  locale = locale.to_sym if locale.respond_to?(:to_sym)
  TWITTER_LOCALE_MAP.fetch(locale, locale)
end

.locale_fallbacksObject



100
101
102
# File 'lib/twitter_cldr.rb', line 100

def locale_fallbacks
  @locale_fallbacks ||= []
end

.register_locale_fallback(proc_or_locale) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/twitter_cldr.rb', line 84

def register_locale_fallback(proc_or_locale)
  case proc_or_locale
    when Symbol, String, Proc
      locale_fallbacks << proc_or_locale
    else
      raise "A locale fallback must be of type String, Symbol, or Proc."
  end
  nil
end

.reset_locale_fallbacksObject



94
95
96
97
98
# File 'lib/twitter_cldr.rb', line 94

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

.resourcesObject



61
62
63
# File 'lib/twitter_cldr.rb', line 61

def resources
  @resources ||= TwitterCldr::Resources::Loader.new
end

.supported_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/twitter_cldr.rb', line 118

def supported_locale?(locale)
  !!locale && supported_locales.include?(convert_locale(locale))
end

.supported_localesObject



114
115
116
# File 'lib/twitter_cldr.rb', line 114

def supported_locales
  @supported_locales ||= Dir.glob(File.join(RESOURCES_DIR, 'locales', '*')).map { |f| File.basename(f).to_sym }
end

.twitter_locale(locale) ⇒ Object



109
110
111
112
# File 'lib/twitter_cldr.rb', line 109

def twitter_locale(locale)
  locale = locale.to_sym
  CLDR_LOCALE_MAP.fetch(locale, locale)
end

.with_locale(locale) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/twitter_cldr.rb', line 71

def with_locale(locale)
  raise "Unsupported locale" unless supported_locale?(locale)

  begin
    old_locale = @locale
    @locale = locale
    result = yield
  ensure
    @locale = old_locale
    result
  end
end