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/versions.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/transforms.rb,
lib/twitter_cldr/utils/trie.rb,
lib/twitter_cldr/utils/yaml.rb,
lib/twitter_cldr/shared/bidi.rb,
lib/twitter_cldr/shared/unit.rb,
lib/twitter_cldr/data_readers.rb,
lib/twitter_cldr/segmentation.rb,
lib/twitter_cldr/shared/caser.rb,
lib/twitter_cldr/normalization.rb,
lib/twitter_cldr/resources/uli.rb,
lib/twitter_cldr/shared/locale.rb,
lib/twitter_cldr/parsers/parser.rb,
lib/twitter_cldr/shared/numbers.rb,
lib/twitter_cldr/shared/calendar.rb,
lib/twitter_cldr/transforms/rule.rb,
lib/twitter_cldr/utils/range_set.rb,
lib/twitter_cldr/resources/loader.rb,
lib/twitter_cldr/shared/languages.rb,
lib/twitter_cldr/shared/territory.rb,
lib/twitter_cldr/tokenizers/token.rb,
lib/twitter_cldr/utils/regexp_ast.rb,
lib/twitter_cldr/segmentation/rule.rb,
lib/twitter_cldr/shared/casefolder.rb,
lib/twitter_cldr/shared/code_point.rb,
lib/twitter_cldr/shared/currencies.rb,
lib/twitter_cldr/shared/hyphenator.rb,
lib/twitter_cldr/shared/properties.rb,
lib/twitter_cldr/transforms/cursor.rb,
lib/twitter_cldr/utils/code_points.rb,
lib/twitter_cldr/collation/collator.rb,
lib/twitter_cldr/formatters/numbers.rb,
lib/twitter_cldr/formatters/plurals.rb,
lib/twitter_cldr/resources/importer.rb,
lib/twitter_cldr/shared/phone_codes.rb,
lib/twitter_cldr/shared/territories.rb,
lib/twitter_cldr/transforms/filters.rb,
lib/twitter_cldr/segmentation/cursor.rb,
lib/twitter_cldr/segmentation/parser.rb,
lib/twitter_cldr/shared/postal_codes.rb,
lib/twitter_cldr/shared/property_set.rb,
lib/twitter_cldr/transforms/rule_set.rb,
lib/twitter_cldr/formatters/formatter.rb,
lib/twitter_cldr/parsers/symbol_table.rb,
lib/twitter_cldr/resources/properties.rb,
lib/twitter_cldr/shared/unicode_regex.rb,
lib/twitter_cldr/tokenizers/tokenizer.rb,
lib/twitter_cldr/transforms/tokenizer.rb,
lib/twitter_cldr/utils/regexp_sampler.rb,
lib/twitter_cldr/collation/trie_loader.rb,
lib/twitter_cldr/parsers/number_parser.rb,
lib/twitter_cldr/segmentation/rule_set.rb,
lib/twitter_cldr/shared/language_codes.rb,
lib/twitter_cldr/shared/likely_subtags.rb,
lib/twitter_cldr/transforms/rule_match.rb,
lib/twitter_cldr/transforms/transforms.rb,
lib/twitter_cldr/utils/script_detector.rb,
lib/twitter_cldr/collation/trie_builder.rb,
lib/twitter_cldr/resources/requirements.rb,
lib/twitter_cldr/transforms/conversions.rb,
lib/twitter_cldr/transforms/transformer.rb,
lib/twitter_cldr/utils/file_system_trie.rb,
lib/twitter_cldr/formatters/numbers/rbnf.rb,
lib/twitter_cldr/shared/numbering_system.rb,
lib/twitter_cldr/transforms/comment_rule.rb,
lib/twitter_cldr/transforms/transform_id.rb,
lib/twitter_cldr/data_readers/data_reader.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/transforms/variable_rule.rb,
lib/twitter_cldr/formatters/list_formatter.rb,
lib/twitter_cldr/localized/localized_array.rb,
lib/twitter_cldr/resources/import_resolver.rb,
lib/twitter_cldr/resources/readme_renderer.rb,
lib/twitter_cldr/transforms/transliterator.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/shared/properties_database.rb,
lib/twitter_cldr/shared/property_normalizer.rb,
lib/twitter_cldr/tokenizers/composite_token.rb,
lib/twitter_cldr/segmentation/break_iterator.rb,
lib/twitter_cldr/transforms/conversions/side.rb,
lib/twitter_cldr/collation/trie_with_fallback.rb,
lib/twitter_cldr/formatters/numbers/rbnf/rule.rb,
lib/twitter_cldr/localized/localized_datetime.rb,
lib/twitter_cldr/localized/localized_timespan.rb,
lib/twitter_cldr/parsers/unicode_regex_parser.rb,
lib/twitter_cldr/resources/bidi_test_importer.rb,
lib/twitter_cldr/resources/rbnf_test_importer.rb,
lib/twitter_cldr/resources/tailoring_importer.rb,
lib/twitter_cldr/shared/postal_code_generator.rb,
lib/twitter_cldr/shared/property_name_aliases.rb,
lib/twitter_cldr/tokenizers/pattern_tokenizer.rb,
lib/twitter_cldr/transforms/transforms/parser.rb,
lib/twitter_cldr/data_readers/date_data_reader.rb,
lib/twitter_cldr/data_readers/time_data_reader.rb,
lib/twitter_cldr/parsers/unicode_regex/literal.rb,
lib/twitter_cldr/resources/unicode_file_parser.rb,
lib/twitter_cldr/segmentation/rule_set_builder.rb,
lib/twitter_cldr/shared/property_value_aliases.rb,
lib/twitter_cldr/transforms/conversions/parser.rb,
lib/twitter_cldr/formatters/numbers/rbnf/plural.rb,
lib/twitter_cldr/resources/hyphenation_importer.rb,
lib/twitter_cldr/resources/phone_codes_importer.rb,
lib/twitter_cldr/resources/regexp_ast_generator.rb,
lib/twitter_cldr/shared/territories_containment.rb,
lib/twitter_cldr/transforms/conversion_rule_set.rb,
lib/twitter_cldr/transforms/filters/filter_rule.rb,
lib/twitter_cldr/transforms/filters/null_filter.rb,
lib/twitter_cldr/data_readers/number_data_reader.rb,
lib/twitter_cldr/formatters/numbers/helpers/base.rb,
lib/twitter_cldr/parsers/unicode_regex/component.rb,
lib/twitter_cldr/resources/postal_codes_importer.rb,
lib/twitter_cldr/resources/unicode_data_importer.rb,
lib/twitter_cldr/shared/properties/bidi_brackets.rb,
lib/twitter_cldr/transforms/filters/regex_filter.rb,
lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb,
lib/twitter_cldr/resources/segment_tests_importer.rb,
lib/twitter_cldr/shared/properties/arabic_shaping.rb,
lib/twitter_cldr/data_readers/calendar_data_reader.rb,
lib/twitter_cldr/data_readers/timespan_data_reader.rb,
lib/twitter_cldr/resources/language_codes_importer.rb,
lib/twitter_cldr/resources/transform_test_importer.rb,
lib/twitter_cldr/tokenizers/numbers/rbnf_tokenizer.rb,
lib/twitter_cldr/data_readers/date_time_data_reader.rb,
lib/twitter_cldr/formatters/numbers/helpers/integer.rb,
lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb,
lib/twitter_cldr/formatters/numbers/rbnf/rule_group.rb,
lib/twitter_cldr/resources/collation_tries_importer.rb,
lib/twitter_cldr/formatters/numbers/helpers/fraction.rb,
lib/twitter_cldr/formatters/numbers/number_formatter.rb,
lib/twitter_cldr/formatters/numbers/rbnf/rule_parser.rb,
lib/twitter_cldr/formatters/plurals/plural_formatter.rb,
lib/twitter_cldr/parsers/unicode_regex/character_set.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/formatters/numbers/rbnf/substitution.rb,
lib/twitter_cldr/parsers/unicode_regex/unicode_string.rb,
lib/twitter_cldr/resources/casefolder_class_generator.rb,
lib/twitter_cldr/resources/locales_resources_importer.rb,
lib/twitter_cldr/transforms/transforms/null_transform.rb,
lib/twitter_cldr/transforms/transforms/transform_pair.rb,
lib/twitter_cldr/transforms/transforms/transform_rule.rb,
lib/twitter_cldr/collation/implicit_collation_elements.rb,
lib/twitter_cldr/formatters/numbers/currency_formatter.rb,
lib/twitter_cldr/parsers/unicode_regex/character_class.rb,
lib/twitter_cldr/parsers/unicode_regex/character_range.rb,
lib/twitter_cldr/transforms/transforms/named_transform.rb,
lib/twitter_cldr/resources/properties/property_importer.rb,
lib/twitter_cldr/resources/requirements/git_requirement.rb,
lib/twitter_cldr/resources/requirements/icu_requirement.rb,
lib/twitter_cldr/transforms/conversions/conversion_rule.rb,
lib/twitter_cldr/transforms/transforms/casing_transform.rb,
lib/twitter_cldr/formatters/calendars/timespan_formatter.rb,
lib/twitter_cldr/resources/properties/prop_list_importer.rb,
lib/twitter_cldr/resources/requirements/cldr_requirement.rb,
lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb,
lib/twitter_cldr/formatters/calendars/date_time_formatter.rb,
lib/twitter_cldr/tokenizers/calendars/date_time_tokenizer.rb,
lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb,
lib/twitter_cldr/resources/properties/age_property_importer.rb,
lib/twitter_cldr/resources/requirements/unicode_requirement.rb,
lib/twitter_cldr/resources/properties/jamo_property_importer.rb,
lib/twitter_cldr/resources/unicode_property_aliases_importer.rb,
lib/twitter_cldr/data_readers/additional_date_format_selector.rb,
lib/twitter_cldr/resources/properties/blocks_property_importer.rb,
lib/twitter_cldr/resources/properties/script_property_importer.rb,
lib/twitter_cldr/resources/requirements/dependency_requirement.rb,
lib/twitter_cldr/transforms/transforms/normalization_transform.rb,
lib/twitter_cldr/transforms/transforms/break_internal_transform.rb,
lib/twitter_cldr/tokenizers/unicode_regex/unicode_regex_tokenizer.rb,
lib/twitter_cldr/resources/properties/line_break_property_importer.rb,
lib/twitter_cldr/resources/properties/word_break_property_importer.rb,
lib/twitter_cldr/resources/properties/bidi_brackets_property_importer.rb,
lib/twitter_cldr/formatters/numbers/abbreviated/long_decimal_formatter.rb,
lib/twitter_cldr/resources/properties/arabic_shaping_property_importer.rb,
lib/twitter_cldr/resources/properties/derived_core_properties_importer.rb,
lib/twitter_cldr/resources/properties/grapheme_break_property_importer.rb,
lib/twitter_cldr/resources/properties/sentence_break_property_importer.rb,
lib/twitter_cldr/resources/properties/unicode_data_properties_importer.rb,
lib/twitter_cldr/formatters/numbers/abbreviated/short_decimal_formatter.rb,
lib/twitter_cldr/resources/properties/east_asian_width_property_importer.rb,
lib/twitter_cldr/resources/properties/script_extensions_property_importer.rb,
lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb,
lib/twitter_cldr/resources/properties/hangul_syllable_type_property_importer.rb,
lib/twitter_cldr/resources/properties/indic_syllabic_category_property_importer.rb,
lib/twitter_cldr/resources/properties/indic_positional_category_property_importer.rb

Overview

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

Defined Under Namespace

Modules: Collation, DataReaders, Formatters, Localized, Normalization, Parsers, Resources, Segmentation, Shared, Tokenizers, Transforms, Utils, Versions

Constant Summary collapse

DEFAULT_LOCALE =
:en
DEFAULT_CALENDAR_TYPE =
:gregorian
RESOURCES_DIR =
File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'resources')
VENDOR_DIR =
File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'vendor')
LIB_DIR =
File.dirname(File.expand_path(__FILE__))
SPEC_DIR =
File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'spec')
TWITTER_LOCALE_MAP =

TODO: convert this and all other hashes to 1.9 syntax 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 =
'4.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.disable_custom_locale_resourcesObject

Returns the value of attribute disable_custom_locale_resources.



63
64
65
# File 'lib/twitter_cldr.rb', line 63

def disable_custom_locale_resources
  @disable_custom_locale_resources
end

.localeObject



69
70
71
72
73
74
75
# File 'lib/twitter_cldr.rb', line 69

def locale
  # doing all this work in locale getter rather than locale setter makes it possible to use locale fallbacks
  # even if they were configured (or became available) after @locale was already assigned an unsupported 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



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

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

.locale_fallbacksObject



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

def locale_fallbacks
  @locale_fallbacks ||= []
end

.register_locale_fallback(proc_or_locale) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/twitter_cldr.rb', line 90

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



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

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



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

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

.supported_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/twitter_cldr.rb', line 125

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

.supported_localesObject



121
122
123
# File 'lib/twitter_cldr.rb', line 121

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

.twitter_locale(locale) ⇒ Object



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

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

.with_locale(locale) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/twitter_cldr.rb', line 77

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