Module: Knj::Locales
- Defined in:
- lib/knj/locales.rb,
lib/knj/locale_strings.rb
Overview
This file can be symlinked and thereby easily translated by POEdit. Further more the static methods can be used for stuff.
Class Method Summary collapse
-
.ago_strings ⇒ Object
Returns an array of translate ‘ago’-strings.
-
.days_arr ⇒ Object
Returns an array of translated day-names (‘_’-method must exist!).
-
.days_short_arr ⇒ Object
Returns an array of short translated day-names.
-
.lang ⇒ Object
Returns the primary locale, secondary locale and the two put together.
-
.locale ⇒ Object
Returns the current locale for the current environment (_session or Thread.current).
-
.localeconv ⇒ Object
Returns various localized information about the environment.
-
.months_arr ⇒ Object
Returns an array of translated month-names.
-
.number_in(num_str) ⇒ Object
Returns a float from the formatted string according to the current locale.
-
.number_out(num_str, dec = 2) ⇒ Object
Returns the given number as a formatted string according to the current locale.
Class Method Details
.ago_strings ⇒ Object
Returns an array of translate ‘ago’-strings. View the source to see which.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/knj/locale_strings.rb', line 25 def self.ago_strings return { :year_ago_str => _("%s year ago"), :years_ago_str => _("%s years ago"), :month_ago_str => _("%s month ago"), :months_ago_str => _("%s months ago"), :day_ago_str => _("%s day ago"), :days_ago_str => _("%s days ago"), :hour_ago_str => _("%s hour ago"), :hours_ago_str => _("%s hours ago"), :min_ago_str => _("%s minute ago"), :mins_ago_str => _("%s minutes ago"), :sec_ago_str => _("%s second ago"), :secs_ago_str => _("%s seconds ago"), :right_now_str => _("right now") } end |
.days_arr ⇒ Object
Returns an array of translated day-names (‘_’-method must exist!).
Examples
Knj::Locales.days_arr #=> [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]
6 7 8 |
# File 'lib/knj/locale_strings.rb', line 6 def self.days_arr return [_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")] end |
.days_short_arr ⇒ Object
Returns an array of short translated day-names.
Examples
Knj::Locales.days_short_arr #=> [“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”]
13 14 15 |
# File 'lib/knj/locale_strings.rb', line 13 def self.days_short_arr return [_("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun")] end |
.lang ⇒ Object
Returns the primary locale, secondary locale and the two put together.
Examples
Knj::Locales.lang #=> => “en”, “second” => “GB”, “full” => “en_GB”
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/knj/locales.rb', line 6 def self.lang match = self.locale.to_s.match(/^([a-z]{2})_([A-Z]{2})/) raise "Could not understand language: #{self.locale}." if !match return { "first" => match[1], "second" => match[2], "full" => "#{match[1]}_#{match[2]}" } end |
.locale ⇒ Object
Returns the current locale for the current environment (_session or Thread.current).
Examples
Knj::Locales.locale #=> ‘en_GB’ Knj::Locales.locale #=> ‘da_DK’
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/knj/locales.rb', line 61 def self.locale begin return _session[:locale] if _session[:locale].to_s.strip.length > 0 rescue NameError #ignore. end if Thread.current[:locale] return Thread.current[:locale] elsif ENV["LANGUAGE"] return ENV["LANGUAGE"] elsif ENV["LANG"] return ENV["LANG"] end raise "Could not figure out locale." end |
.localeconv ⇒ Object
Returns various localized information about the environment.
Examples
Knj::Locales.localeconv #=> => “.”, “thousands_sep” => “,”, “csv_delimiter” => “,”
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/knj/locales.rb', line 20 def self.localeconv f = Knj::Locales.lang["first"] case f when "da", "de", "es", "pl", "sv" dec = "," thousand = "." csv_delimiter = ";" else dec = "." thousand = "," csv_delimiter = "," end return { "decimal_point" => dec, "thousands_sep" => thousand, "csv_delimiter" => csv_delimiter } end |
.months_arr ⇒ Object
Returns an array of translated month-names.
Examples
Knj::Locales.months_arr #=> [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”]
20 21 22 |
# File 'lib/knj/locale_strings.rb', line 20 def self.months_arr return [_("January"), _("February"), _("March"), _("April"), _("May"), _("June"), _("July"), _("August"), _("September"), _("October"), _("November"), _("December")] end |
.number_in(num_str) ⇒ Object
Returns a float from the formatted string according to the current locale.
Examples
Knj::Locales.number_in(“123,456.68”) #=> 123456.68
44 45 46 47 |
# File 'lib/knj/locales.rb', line 44 def self.number_in(num_str) lc = Knj::Locales.localeconv return num_str.to_s.gsub(lc["thousands_sep"], "").gsub(lc["decimal_point"], ".").to_f end |
.number_out(num_str, dec = 2) ⇒ Object
Returns the given number as a formatted string according to the current locale.
Examples
Knj::Locales.number_out(123456.68) #=> “123,456.68”
52 53 54 55 |
# File 'lib/knj/locales.rb', line 52 def self.number_out(num_str, dec = 2) lc = Knj::Locales.localeconv return Php4r.number_format(num_str, dec, lc["decimal_point"], lc["thousands_sep"]) end |