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.

Constant Summary collapse

LANG_CONVERTIONS =
{
  "en" => "en_GB"
}

Class Method Summary collapse

Class Method Details

.ago_stringsObject

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_arrObject

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_arrObject

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

.langObject

Returns the primary locale, secondary locale and the two put together.

Examples

Knj::Locales.lang #=> => “en”, “second” => “GB”, “full” => “en_GB”



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/knj/locales.rb', line 13

def self.lang
  locale_str = self.locale.to_s.strip
  locale_str = "en_GB" if locale_str.empty?
  
  #Sometimes language can be 'en'. Convert that to 'en_GB' if that is so.
  locale_str = Knj::Locales::LANG_CONVERTIONS[locale_str] if Knj::Locales::LANG_CONVERTIONS.key?(locale_str)
  
  match = locale_str.match(/^([a-z]{2})_([A-Z]{2})/)
  raise "Could not understand language: '#{locale_str}'." if !match
      
  return {
    "first" => match[1],
    "second" => match[2],
    "full" => "#{match[1]}_#{match[2]}"
  }
end

.localeObject

Returns the current locale for the current environment (_session or Thread.current).

Examples

Knj::Locales.locale #=> ‘en_GB’ Knj::Locales.locale #=> ‘da_DK’



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/knj/locales.rb', line 76

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
  
  return "en_GB"
end

.localeconvObject

Returns various localized information about the environment.

Examples

Knj::Locales.localeconv #=> => “.”, “thousands_sep” => “,”, “csv_delimiter” => “,”



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/knj/locales.rb', line 33

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_arrObject

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



57
58
59
60
# File 'lib/knj/locales.rb', line 57

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”



65
66
67
68
69
70
# File 'lib/knj/locales.rb', line 65

def self.number_out(num_str, dec = 2)
  lc = Knj::Locales.localeconv
  
  require "php4r"
  return Php4r.number_format(num_str, dec, lc["decimal_point"], lc["thousands_sep"])
end