Class: R18n::Locale
- Inherits:
-
Object
- Object
- R18n::Locale
- Defined in:
- lib/r18n-core/locale.rb
Overview
Information about locale (language, country and other special variant preferences). Locale was named by RFC 3066. For example, locale for French speaking people in Canada will be fr-CA
.
Locale classes are placed in R18n::Locales
module and storage install locales/
dir.
Each locale has sublocales
– often known languages for people from this locale. For example, many Belorussians know Russian and English. If there is’t translation for Belorussian, it will be searched in Russian and next in English translations.
Usage
Get Russian locale and print it information
ru = R18n::Locale.load('ru')
ru.title #=> "Русский"
ru.code #=> "ru"
ru.ltr? #=> true
Available data
-
code
– locale RFC 3066 code; -
title
– locale name on it language; -
ltr?
– true on left-to-right writing direction, false for Arabic and Hebrew); -
sublocales
– often known languages for people from this locale; -
week_start
– does week start from:monday
or:sunday
.
You can see more available data about locale in samples in locales/
dir.
Constant Summary collapse
- LOCALES_DIR =
Pathname(__FILE__).dirname. + '../../locales/'
- @@loaded =
{}
Class Method Summary collapse
-
.available ⇒ Object
Codes of all available locales.
-
.exists?(locale) ⇒ Boolean
Is
locale
has info file. -
.load(code) ⇒ Object
Load locale by RFC 3066
code
. -
.set(properties) ⇒ Object
Set locale
properties
.
Instance Method Summary collapse
-
#==(locale) ⇒ Object
Is another locale has same code.
-
#code ⇒ Object
Locale RFC 3066 code.
-
#format_date_full(date, year = true, *params) ⇒ Object
Format
date
in most official form. -
#format_date_human(date, i18n, now = Date.today, *params) ⇒ Object
Format
date
in human usable form. -
#format_date_standard(date, *params) ⇒ Object
Format
date
in compact form. -
#format_float(float) ⇒ Object
Returns the float in String form, according to the rules of the locale.
-
#format_integer(integer) ⇒ Object
Returns the integer in String form, according to the rules of the locale.
-
#format_time(time) ⇒ Object
Format
time
without date. -
#format_time_full(time, *params) ⇒ Object
Format
time
in most official form. -
#format_time_human(time, i18n, now = Time.now, *params) ⇒ Object
Format
time
in human usable form. -
#format_time_standard(time, *params) ⇒ Object
Format
time
in compact form. -
#inspect ⇒ Object
Human readable locale code and title.
-
#localize(obj, format = nil, *params) ⇒ Object
Convert
object
to String. -
#ltr? ⇒ Boolean
Is locale has left-to-right write direction.
- #month_abbrs ⇒ Object
- #month_standalone ⇒ Object
-
#pluralize(n) ⇒ Object
Return pluralization type for
n
items. -
#strftime(time, format) ⇒ Object
Same that
Time.strftime
, but translate months and week days names. -
#supported? ⇒ Boolean
Is locale has information file.
Class Method Details
.available ⇒ Object
Codes of all available locales.
63 64 65 66 67 |
# File 'lib/r18n-core/locale.rb', line 63 def self.available Dir.glob(File.join(LOCALES_DIR, '*.rb')).map do |i| File.basename(i, '.rb') end end |
.exists?(locale) ⇒ Boolean
Is locale
has info file.
70 71 72 |
# File 'lib/r18n-core/locale.rb', line 70 def self.exists?(locale) File.exists?(File.join(LOCALES_DIR, locale.to_s + '.rb')) end |
.load(code) ⇒ Object
Load locale by RFC 3066 code
.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/r18n-core/locale.rb', line 75 def self.load(code) original = code.to_s.gsub(/[^-_a-zA-Z]/, '') code = original.gsub('_', '-').downcase @@loaded[code] ||= begin if exists? code require LOCALES_DIR + "#{code}.rb" name = code.gsub(/[\w\d]+/) { |i| i.capitalize }.gsub('-', '') eval('R18n::Locales::' + name).new else UnsupportedLocale.new(original) end end end |
.set(properties) ⇒ Object
101 102 103 104 105 |
# File 'lib/r18n-core/locale.rb', line 101 def self.set(properties) properties.each_pair do |key, value| define_method(key) { value } end end |
Instance Method Details
#==(locale) ⇒ Object
Is another locale has same code.
127 128 129 |
# File 'lib/r18n-core/locale.rb', line 127 def ==(locale) self.class == locale.class end |
#code ⇒ Object
Locale RFC 3066 code.
108 109 110 |
# File 'lib/r18n-core/locale.rb', line 108 def code self.class.name.split('::').last.downcase end |
#format_date_full(date, year = true, *params) ⇒ Object
Format date
in most official form. For example, “December 31st, 2009”. For special cases you can replace it in locale’s class. If year
is false date will be without year.
284 285 286 287 288 |
# File 'lib/r18n-core/locale.rb', line 284 def format_date_full(date, year = true, *params) format = full_format format = year_format.sub('_', format) if year strftime(date, format) end |
#format_date_human(date, i18n, now = Date.today, *params) ⇒ Object
Format date
in human usable form. For example “5 days ago” or “yesterday”. In now
you can set base time, which be used to get relative time. For special cases you can replace it in locale’s class.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/r18n-core/locale.rb', line 258 def format_date_human(date, i18n, now = Date.today, *params) days = (date - now).to_i case days when -6..-2 i18n.human_time.days_ago(days.abs) when -1 i18n.human_time.yesterday when 0 i18n.human_time.today when 1 i18n.human_time.tomorrow when 2..6 i18n.human_time.after_days(days) else format_date_full(date, date.year != now.year) end end |
#format_date_standard(date, *params) ⇒ Object
Format date
in compact form. For example, “12/31/09”.
277 278 279 |
# File 'lib/r18n-core/locale.rb', line 277 def format_date_standard(date, *params) strftime(date, date_format) end |
#format_float(float) ⇒ Object
Returns the float in String form, according to the rules of the locale. It will also put real typographic minus.
186 187 188 189 |
# File 'lib/r18n-core/locale.rb', line 186 def format_float(float) decimal = number_decimal self.format_integer(float.to_i) + decimal + float.to_s.split('.').last end |
#format_integer(integer) ⇒ Object
Returns the integer in String form, according to the rules of the locale. It will also put real typographic minus.
174 175 176 177 178 179 180 181 182 |
# File 'lib/r18n-core/locale.rb', line 174 def format_integer(integer) str = integer.to_s str[0] = '−' if 0 > integer # Real typographic minus group = number_group str.gsub(/(\d)(?=(\d\d\d)+(?!\d))/) do |match| match + group end end |
#format_time(time) ⇒ Object
Format time
without date. For example, “12:59”.
216 217 218 |
# File 'lib/r18n-core/locale.rb', line 216 def format_time(time) strftime(time, time_format) end |
#format_time_full(time, *params) ⇒ Object
Format time
in most official form. For example, “December 31st, 2009 12:59”. For special cases you can replace it in locale’s class.
251 252 253 |
# File 'lib/r18n-core/locale.rb', line 251 def format_time_full(time, *params) format_date_full(time) + format_time(time) end |
#format_time_human(time, i18n, now = Time.now, *params) ⇒ Object
Format time
in human usable form. For example “5 minutes ago” or “yesterday”. In now
you can set base time, which be used to get relative time. For special cases you can replace it in locale’s class.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/r18n-core/locale.rb', line 223 def format_time_human(time, i18n, now = Time.now, *params) minutes = (time - now) / 60.0 diff = minutes.abs if (diff > 24 * 60) or (time.mday != now.mday and diff > 12 * 24) format_date_human(R18n::Utils.to_date(time), i18n, R18n::Utils.to_date(now)) + format_time(time) else if -1 < minutes and 1 > minutes i18n.human_time.now elsif 60 <= minutes i18n.human_time.after_hours((diff / 60.0).floor) elsif -60 >= minutes i18n.human_time.hours_ago((diff / 60.0).floor) elsif 0 < minutes i18n.human_time.after_minutes(minutes.round) else i18n.human_time.minutes_ago(minutes.round.abs) end end end |
#format_time_standard(time, *params) ⇒ Object
Format time
in compact form. For example, “12/31/09 12:59”.
245 246 247 |
# File 'lib/r18n-core/locale.rb', line 245 def format_time_standard(time, *params) format_date_standard(time) + format_time(time) end |
#inspect ⇒ Object
Human readable locale code and title.
137 138 139 |
# File 'lib/r18n-core/locale.rb', line 137 def inspect "Locale #{code} (#{title})" end |
#localize(obj, format = nil, *params) ⇒ Object
Convert object
to String. It support Fixnum, Bignum, Float, Time, Date and DateTime.
For time classes you can set format
in standard strftime
form, :full
(“01 Jule, 2009”), :human
(“yesterday”), :standard
(“07/01/09”) or :month
for standalone month name. Default format is :standard
.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/r18n-core/locale.rb', line 148 def localize(obj, format = nil, *params) case obj when Integer format_integer(obj) when Float format_float(obj) when Time, DateTime, Date return strftime(obj, format) if format.is_a? String return month_standalone[obj.month - 1] if :month == format return obj.to_s if :human == format and not params.first.is_a? I18n type = obj.is_a?(Date) ? 'date' : 'time' format = :standard unless format unless [:human, :full, :standard].include? format raise ArgumentError, "Unknown time formatter #{format}" end send "format_#{type}_#{format}", obj, *params else obj.to_s end end |
#ltr? ⇒ Boolean
Is locale has left-to-right write direction.
124 |
# File 'lib/r18n-core/locale.rb', line 124 def ltr?; true; end |
#month_abbrs ⇒ Object
121 |
# File 'lib/r18n-core/locale.rb', line 121 def month_abbrs; month_names; end |
#month_standalone ⇒ Object
120 |
# File 'lib/r18n-core/locale.rb', line 120 def month_standalone; month_names; end |
#pluralize(n) ⇒ Object
Return pluralization type for n
items. This is simple form. For special cases you can replace it in locale’s class.
292 293 294 295 296 297 298 299 300 301 |
# File 'lib/r18n-core/locale.rb', line 292 def pluralize(n) case n when 0 0 when 1 1 else 'n' end end |
#strftime(time, format) ⇒ Object
Same that Time.strftime
, but translate months and week days names. In time
you can use Time, DateTime or Date object. In format
you can use standard strftime
format.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/r18n-core/locale.rb', line 194 def strftime(time, format) translated = '' format.scan(/%[EO]?.|./o) do |c| case c.sub(/^%[EO]?(.)$/o, '%\\1') when '%A' translated << wday_names[time.wday] when '%a' translated << wday_abbrs[time.wday] when '%B' translated << month_names[time.month - 1] when '%b' translated << month_abbrs[time.month - 1] when '%p' translated << (time.hour < 12 ? time_am : time_pm) else translated << c end end time.strftime(translated) end |
#supported? ⇒ Boolean
Is locale has information file. In this class always return true.
132 133 134 |
# File 'lib/r18n-core/locale.rb', line 132 def supported? true end |