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/timezones.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/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/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/supported_locales.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/cldr_dtd.rb,
lib/twitter_cldr/resources/importer.rb,
lib/twitter_cldr/shared/day_periods.rb,
lib/twitter_cldr/shared/territories.rb,
lib/twitter_cldr/shared/unicode_set.rb,
lib/twitter_cldr/timezones/location.rb,
lib/twitter_cldr/timezones/timezone.rb,
lib/twitter_cldr/transforms/filters.rb,
lib/twitter_cldr/segmentation/cursor.rb,
lib/twitter_cldr/shared/postal_codes.rb,
lib/twitter_cldr/shared/property_set.rb,
lib/twitter_cldr/timezones/zone_meta.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/resources/cldr_locale.rb,
lib/twitter_cldr/segmentation/metadata.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/timezones/gmt_location.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/segmentation/dictionary.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/resources/units_importer.rb,
lib/twitter_cldr/segmentation/state_table.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/segmentation/status_table.rb,
lib/twitter_cldr/segmentation/suppressions.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/resources/aliases_importer.rb,
lib/twitter_cldr/segmentation/line_iterator.rb,
lib/twitter_cldr/segmentation/possible_word.rb,
lib/twitter_cldr/segmentation/state_machine.rb,
lib/twitter_cldr/segmentation/word_iterator.rb,
lib/twitter_cldr/shared/properties_database.rb,
lib/twitter_cldr/shared/property_normalizer.rb,
lib/twitter_cldr/timezones/generic_location.rb,
lib/twitter_cldr/timezones/iso8601_location.rb,
lib/twitter_cldr/resources/cldr_data_builder.rb,
lib/twitter_cldr/resources/cldr_document_set.rb,
lib/twitter_cldr/segmentation/break_iterator.rb,
lib/twitter_cldr/segmentation/category_table.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/calendars_importer.rb,
lib/twitter_cldr/resources/rbnf_test_importer.rb,
lib/twitter_cldr/resources/tailoring_importer.rb,
lib/twitter_cldr/resources/timezones_importer.rb,
lib/twitter_cldr/segmentation/cj_break_engine.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/filtered_rule_set.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/transforms_importer.rb,
lib/twitter_cldr/resources/unicode_file_parser.rb,
lib/twitter_cldr/segmentation/lao_break_engine.rb,
lib/twitter_cldr/segmentation/segment_iterator.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/regexp_ast_generator.rb,
lib/twitter_cldr/resources/territories_importer.rb,
lib/twitter_cldr/segmentation/null_suppressions.rb,
lib/twitter_cldr/segmentation/thai_break_engine.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/list_formats_importer.rb,
lib/twitter_cldr/resources/postal_codes_importer.rb,
lib/twitter_cldr/resources/unicode_data_importer.rb,
lib/twitter_cldr/segmentation/khmer_break_engine.rb,
lib/twitter_cldr/segmentation/possible_word_list.rb,
lib/twitter_cldr/shared/properties/bidi_brackets.rb,
lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb,
lib/twitter_cldr/resources/segment_rules_importer.rb,
lib/twitter_cldr/resources/segment_tests_importer.rb,
lib/twitter_cldr/resources/validity_data_importer.rb,
lib/twitter_cldr/segmentation/korean_break_engine.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/number_formats_importer.rb,
lib/twitter_cldr/resources/parent_locales_importer.rb,
lib/twitter_cldr/resources/timezone_tests_importer.rb,
lib/twitter_cldr/segmentation/brahmic_break_engine.rb,
lib/twitter_cldr/segmentation/burmese_break_engine.rb,
lib/twitter_cldr/tokenizers/numbers/rbnf_tokenizer.rb,
lib/twitter_cldr/transforms/filters/unicode_filter.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_tests_importer.rb,
lib/twitter_cldr/resources/collation_tries_importer.rb,
lib/twitter_cldr/resources/requirements/pom_manager.rb,
lib/twitter_cldr/resources/transform_tests_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/resources/currency_symbols_importer.rb,
lib/twitter_cldr/resources/day_period_rules_importer.rb,
lib/twitter_cldr/resources/properties/emoji_importer.rb,
lib/twitter_cldr/resources/timezone_regions_importer.rb,
lib/twitter_cldr/segmentation/unhandled_break_engine.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/segmentation/dictionary_break_engine.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/blank_transform.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/resources/requirements/uca_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/resources/segment_dictionaries_importer.rb,
lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb,
lib/twitter_cldr/formatters/calendars/date_time_formatter.rb,
lib/twitter_cldr/resources/requirements/emoji_requirement.rb,
lib/twitter_cldr/tokenizers/calendars/date_time_tokenizer.rb,
lib/twitter_cldr/resources/bcp47_timezone_metadata_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/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/resources/properties/east_asian_width_property_importer.rb,
lib/twitter_cldr/resources/properties/script_extensions_property_importer.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, Timezones, 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 =
'6.11.2'
SUPPORTED_LOCALES_FILE =
File.expand_path('../../resources/supported_locales.yml', __dir__)
SUPPORTED_LOCALES =
YAML.load_file(SUPPORTED_LOCALES_FILE).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.disable_custom_locale_resourcesObject

Returns the value of attribute disable_custom_locale_resources.



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

def disable_custom_locale_resources
  @disable_custom_locale_resources
end

.localeObject



73
74
75
76
77
78
79
# File 'lib/twitter_cldr.rb', line 73

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/twitter_cldr.rb', line 114

def convert_locale(locale)
  locale = normalize_locale(locale)

  unless supported_locale?(locale)
    loc = TwitterCldr::Shared::Locale.parse(locale)
    max_supported = loc.max_supported

    if loc.dasherized == 'und' || !max_supported
      return locale
    end

    locale = normalize_locale(max_supported.dasherized)
  end

  locale
end

.locale_fallbacksObject



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

def locale_fallbacks
  @locale_fallbacks ||= []
end

.normalize_locale(locale) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/twitter_cldr.rb', line 131

def normalize_locale(locale)
  return locale unless (locale.is_a?(String) || locale.is_a?(Symbol))

  locale = locale.to_sym
  locale = lowercase_locales_map.fetch(locale, locale)
  TWITTER_LOCALE_MAP.fetch(locale.downcase, locale)
end

.register_locale_fallback(proc_or_locale) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/twitter_cldr.rb', line 94

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



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

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



69
70
71
# File 'lib/twitter_cldr.rb', line 69

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

.supported_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/twitter_cldr.rb', line 148

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

.supported_localesObject



144
145
146
# File 'lib/twitter_cldr.rb', line 144

def supported_locales
  TwitterCldr::SUPPORTED_LOCALES
end

.twitter_locale(locale) ⇒ Object



139
140
141
142
# File 'lib/twitter_cldr.rb', line 139

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

.with_locale(locale) ⇒ Object



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

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