Class: CSL::Locale
- Extended by:
- Loader
- Includes:
- Comparable
- Defined in:
- lib/csl/locale.rb,
lib/csl/locale/date.rb,
lib/csl/locale/term.rb,
lib/csl/locale/style_options.rb
Overview
CSL::Locales contain locale specific date formatting options, term translations, and a number ordinalizer.
Defined Under Namespace
Classes: Date, DatePart, StyleOptions, Term, Terms
Class Attribute Summary collapse
-
.default ⇒ Object
Returns the value of attribute default.
-
.languages ⇒ Object
readonly
Returns the value of attribute languages.
-
.regions ⇒ Object
readonly
Returns the value of attribute regions.
Instance Attribute Summary collapse
-
#region ⇒ Object
Returns the value of attribute region.
Attributes included from Loader
Attributes inherited from Node
Attributes included from Treelike
Class Method Summary collapse
- .load(input = nil) ⇒ Object
-
.normalize(tag) ⇒ String
Normalizes an IETF tag; adds a language’s default region or a region’s default language.
Instance Method Summary collapse
-
#<=>(other) ⇒ 1, ...
Locales are sorted first by language, then by region; sort order is alphabetical with the following exceptions: the default locale is prioritised; in case of a language match the default region of that language will be prioritised (e.g., de-DE will come before de-AT even though the alphabetical order would be different).
- #added_to(node) ⇒ Object
-
#clear ⇒ self
Sets the locale’s language and region to nil.
- #default? ⇒ Boolean
-
#default_language? ⇒ Boolean
Whether or not the Locale’s language is the default language for its region.
-
#default_region? ⇒ Boolean
Whehter or not the Locale’s region is the default region for its language.
-
#each_date(&block) ⇒ Object
Calls block once for each date format defined by the locale.
-
#each_term(&block) ⇒ Object
Calls block once for each term defined by the locale.
-
#initialize(*arguments) {|_self| ... } ⇒ Locale
constructor
Returns a new locale.
- #initialize_copy(other) ⇒ Object
-
#inspect ⇒ String
A string representation of the Locale.
-
#legacy? ⇒ Boolean
Whether or not the Locale’s version is less than CSL-Ruby’s default version.
- #like?(other) ⇒ Boolean
- #limit_day_ordinals! ⇒ Object
-
#limit_day_ordinals? ⇒ Boolean
True when the option limit-day-ordinals-to-day-1 is true.
- #merge(*others) ⇒ Locale
- #merge!(*others) ⇒ self
-
#ordinalize(number, options = {}) ⇒ String
Ordinalizes the passed-in number using either the ordinal or long-ordinal forms defined by the locale.
- #punctuation_in_quote! ⇒ Object (also: #punctuation_in_quotes!)
-
#punctuation_in_quote? ⇒ Boolean
(also: #punctuation_in_quotes?)
True when the option punctuation-in-quote is true.
-
#quote(string, escape = false) ⇒ String
Puts localized quotes around the passed-in string.
- #replace_with_inner_quotes(string, open, close, escape = false) ⇒ Object
- #reverse_merge(other) ⇒ Object
-
#set(locale) ⇒ self
Sets language and region according to the passed-in locale string.
-
#store(*arguments) ⇒ Object
(also: #learn)
Stores a translation in the locale’s term registry.
-
#to_s ⇒ String
The Locale’s IETF tag.
-
#translate(name, options = {}) ⇒ String?
(also: #t)
The term’s translation.
- #universal? ⇒ Boolean
- #valid? ⇒ Boolean
- #validate ⇒ Object
- #version ⇒ Object
- #version=(version) ⇒ Object
Methods included from Loader
extend_name, extend_path, list, load
Methods inherited from Node
#attribute?, #attributes?, #attributes_for, constantize, create, create_attributes, #custom_attributes, #deep_copy, #default_attribute?, default_attributes, #default_attributes, #each, #exact_match?, #format_page_ranges?, #formatting_options, #has_attributes?, #has_default_attributes?, #has_language?, hide_default_attributes!, hide_default_attributes?, match?, #match?, matches?, #page_range_format, parse, parse!, #quotes?, #reverse_merge!, #save_to, show_default_attributes!, #strip_periods?, #tags, #textnode?, types
Methods included from Extensions::Nesting
Methods included from PrettyPrinter
Methods included from Treelike
#<<, #add_child, #add_children, #ancestors, #closest, #delete_child, #delete_children, #depth, #descendants, #each_ancestor, #each_child, #each_descendant, #each_sibling, #empty?, #find_child, #find_children, #has_children?, #root, #root?, #siblings, #unlink
Constructor Details
#initialize(*arguments) {|_self| ... } ⇒ Locale
Returns a new locale. In the first form, the language/regions is set to the default language and region. In the second form the language/region is set by the passed-in IETF tag. The third form additionally accepts a hash of localize style-options. The fourth form is the standard node attribute initialize signature.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/csl/locale.rb', line 101 def initialize(*arguments) case arguments.length when 0 locale, attributes, = nil, {}, nil when 1 if arguments[0].is_a?(Hash) arguments[0] = arguments[0].symbolize_keys locale = arguments[0].delete(:lang) || arguments[0].delete(:'xml:lang') attributes, = arguments else attributes, locale, = {}, *arguments end when 2 attributes, locale, = {}, *arguments else raise ArgumentError, "wrong number of arguments (#{arguments.length} for 0..2)" end super(attributes, &nil) set(locale) unless locale.blank? unless .nil? children[:'style-options'] = StyleOptions.new() end yield self if block_given? end |
Class Attribute Details
.default ⇒ Object
Returns the value of attribute default.
36 37 38 |
# File 'lib/csl/locale.rb', line 36 def default @default end |
.languages ⇒ Object (readonly)
Returns the value of attribute languages.
37 38 39 |
# File 'lib/csl/locale.rb', line 37 def languages @languages end |
.regions ⇒ Object (readonly)
Returns the value of attribute regions.
37 38 39 |
# File 'lib/csl/locale.rb', line 37 def regions @regions end |
Instance Attribute Details
#region ⇒ Object
Returns the value of attribute region.
81 82 83 |
# File 'lib/csl/locale.rb', line 81 def region @region end |
Class Method Details
.load(input = nil) ⇒ Object
39 40 41 42 43 |
# File 'lib/csl/locale.rb', line 39 def load(input = nil) input ||= Locale.default input = normalize input if input.to_s =~ @tag_pattern super(input) end |
.normalize(tag) ⇒ String
Normalizes an IETF tag; adds a language’s default region or a region’s default language.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/csl/locale.rb', line 56 def normalize(tag) tag = tag.to_s.strip raise ArgumentError, "not a valid IETF tag: #{tag.inspect}" unless tag =~ @tag_pattern language, region = tag.split(/-/) return [language, regions[language.to_sym]].compact.join('-') if region.nil? return [languages[region.to_sym], region].join('-') if language.empty? tag end |
Instance Method Details
#<=>(other) ⇒ 1, ...
Locales are sorted first by language, then by region; sort order is alphabetical with the following exceptions: the default locale is prioritised; in case of a language match the default region of that language will be prioritised (e.g., de-DE will come before de-AT even though the alphabetical order would be different).
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/csl/locale.rb', line 425 def <=>(other) case when !other.is_a?(Locale) nil when [language, region] == [other.language, other.region] 0 when default? -1 when other.default? 1 when language == other.language case when default_region? -1 when other.default_region? 1 else region.to_s <=> other.region.to_s end else language.to_s <=> other.language.to_s end end |
#added_to(node) ⇒ Object
137 138 139 140 |
# File 'lib/csl/locale.rb', line 137 def added_to(node) raise ValidationError, "not allowed to add locale to #{node.nodename}" unless node.nodename == 'style' end |
#clear ⇒ self
Sets the locale’s language and region to nil.
190 191 192 193 |
# File 'lib/csl/locale.rb', line 190 def clear @language, @region = nil self end |
#default? ⇒ Boolean
361 362 363 |
# File 'lib/csl/locale.rb', line 361 def default? to_s == Locale.default end |
#default_language? ⇒ Boolean
Returns whether or not the Locale’s language is the default language for its region.
373 374 375 |
# File 'lib/csl/locale.rb', line 373 def default_language? language && language == Locale.languages[region] end |
#default_region? ⇒ Boolean
Returns whehter or not the Locale’s region is the default region for its language.
367 368 369 |
# File 'lib/csl/locale.rb', line 367 def default_region? region && region == Locale.regions[language] end |
#each_date(&block) ⇒ Object
Calls block once for each date format defined by the locale. If no block is given, an enumerator is returned instead.
348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/csl/locale.rb', line 348 def each_date(&block) if block_given? if date.is_a? CSL::Node yield date else date.each(&block) end else enum_for :each_date end end |
#each_term(&block) ⇒ Object
Calls block once for each term defined by the locale. If no block is given, an enumerator is returned instead.
333 334 335 336 337 338 339 340 |
# File 'lib/csl/locale.rb', line 333 def each_term(&block) if block_given? terms.each(&block) self else enum_for :each_term end end |
#initialize_copy(other) ⇒ Object
132 133 134 135 |
# File 'lib/csl/locale.rb', line 132 def initialize_copy(other) @parent, @ancestors, @descendants, @siblings, @root, @depth = nil initialize(other.attributes.to_hash.merge(:lang => other.to_s)) end |
#inspect ⇒ String
Returns a string representation of the Locale.
455 456 457 |
# File 'lib/csl/locale.rb', line 455 def inspect "#<#{self.class.name} #{to_s}>" end |
#legacy? ⇒ Boolean
Returns whether or not the Locale’s version is less than CSL-Ruby’s default version.
164 165 166 |
# File 'lib/csl/locale.rb', line 164 def legacy? version < Schema.major_version end |
#like?(other) ⇒ Boolean
377 378 379 380 381 382 |
# File 'lib/csl/locale.rb', line 377 def like?(other) return false unless other.is_a? Locale return true if other.universal? language == other.language end |
#limit_day_ordinals! ⇒ Object
267 268 269 270 271 272 273 |
# File 'lib/csl/locale.rb', line 267 def limit_day_ordinals! unless children[:'style-options'] = StyleOptions.new end [:'limit-day-ordinals-to-day-1'] = true end |
#limit_day_ordinals? ⇒ Boolean
Returns true when the option limit-day-ordinals-to-day-1 is true.
262 263 264 265 |
# File 'lib/csl/locale.rb', line 262 def limit_day_ordinals? return false unless && .attribute?(:'limit-day-ordinals-to-day-1') !!([:'limit-day-ordinals-to-day-1'].to_s =~ /^true$/i) end |
#merge(*others) ⇒ Locale
397 398 399 |
# File 'lib/csl/locale.rb', line 397 def merge(*others) deep_copy.merge!(*others) end |
#merge!(*others) ⇒ self
402 403 404 405 406 407 408 409 410 |
# File 'lib/csl/locale.rb', line 402 def merge!(*others) others.each do |other| other merge_dates other merge_terms other end self end |
#ordinalize(number, options = {}) ⇒ String
For CSL 1.0 (and older) locales that do not define an “ordinal-00” term the algorithm specified by CSL 1.0 is used; otherwise uses the CSL 1.0.1 algorithm with improved support for languages other than English.
Ordinalizes the passed-in number using either the ordinal or long-ordinal forms defined by the locale. If a long-ordinal form is requested but not available, the regular ordinal will be returned instead.
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/csl/locale.rb', line 248 def ordinalize(number, = {}) raise ArgumentError, "unable to ordinalize #{number}; integer expected" unless number.respond_to?(:to_i) number = number.to_i ordinal = terms.ordinalize number, return number.to_s if ordinal.nil? return ordinal.to_s() if ordinal.long_ordinal? [number, ordinal.to_s()].join end |
#punctuation_in_quote! ⇒ Object Also known as: punctuation_in_quotes!
282 283 284 285 286 287 288 |
# File 'lib/csl/locale.rb', line 282 def punctuation_in_quote! unless children[:'style-options'] = StyleOptions.new end [:'punctuation-in-quote'] = true end |
#punctuation_in_quote? ⇒ Boolean Also known as: punctuation_in_quotes?
Returns true when the option punctuation-in-quote is true.
276 277 278 279 |
# File 'lib/csl/locale.rb', line 276 def punctuation_in_quote? return false unless && .attribute?(:'punctuation-in-quote') !!([:'punctuation-in-quote'].to_s =~ /^true$/i) end |
#quote(string, escape = false) ⇒ String
Puts localized quotes around the passed-in string.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/csl/locale.rb', line 293 def quote(string, escape = false) oq, cq = t('open-quote'), t('close-quote') return string if oq.nil? || cq.nil? || (oq.empty? && cq.empty?) if escape oq = CSL.encode_xml_text(oq) cq = CSL.encode_xml_text(cq) end string = replace_with_inner_quotes(string, oq, cq, escape) if punctuation_in_quotes? "#{oq}#{string}#{cq}" else string, punctuation = string.split(/([\.,])$/, 2) "#{oq}#{string}#{cq}#{punctuation}" end end |
#replace_with_inner_quotes(string, open, close, escape = false) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/csl/locale.rb', line 314 def replace_with_inner_quotes(string, open, close, escape = false) oq, cq = t('open-inner-quote'), t('close-inner-quote') return string if oq.nil? || cq.nil? || (oq.empty? && cq.empty?) if escape oq = CSL.encode_xml_text(oq) cq = CSL.encode_xml_text(cq) end string.gsub(/(#{open}|"\b)/, oq).gsub(/(#{close}|\b")/, cq) end |
#reverse_merge(other) ⇒ Object
412 413 414 |
# File 'lib/csl/locale.rb', line 412 def reverse_merge(other) other.merge(self) end |
#set(locale) ⇒ self
Sets language and region according to the passed-in locale string. If the region part is not defined by the string, this method will set the region to the default region for the given language.
183 184 185 186 |
# File 'lib/csl/locale.rb', line 183 def set(locale) @language, @region = Locale.normalize(locale).split(/-/).map(&:to_sym) self end |
#store(*arguments) ⇒ Object Also known as: learn
Stores a translation in the locale’s term registry.
206 207 208 209 210 211 212 213 |
# File 'lib/csl/locale.rb', line 206 def store(*arguments) unless has_terms? self << CSL::Locale::Terms.new end terms.store(*arguments) self end |
#to_s ⇒ String
Returns the Locale’s IETF tag.
450 451 452 |
# File 'lib/csl/locale.rb', line 450 def to_s [language, region].compact.join('-') end |
#translate(name, options = {}) ⇒ String? Also known as: t
Returns the term’s translation.
196 197 198 199 200 201 |
# File 'lib/csl/locale.rb', line 196 def translate(name, = {}) return unless has_terms? term = terms.lookup name, term && term.to_s() end |
#universal? ⇒ Boolean
384 385 386 |
# File 'lib/csl/locale.rb', line 384 def universal? language.nil? end |
#valid? ⇒ Boolean
392 393 394 |
# File 'lib/csl/locale.rb', line 392 def valid? validate.empty? end |
#validate ⇒ Object
388 389 390 |
# File 'lib/csl/locale.rb', line 388 def validate Schema.validate self end |
#version ⇒ Object
143 144 145 |
# File 'lib/csl/locale.rb', line 143 def version attributes[:version] end |
#version=(version) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/csl/locale.rb', line 147 def version=(version) raise ArgumentError, "failed to set version to #{version}" unless version.respond_to?(:to_s) version = version.to_s.strip raise ArgumentError, "failed to set version to #{version}: not a version string" unless version =~ /^\d[\d\.]+$/ if version > Schema.version warn "setting version to #{version}; latest supported version is #{Schema.version}" end attributes[:version] = version end |