module Knj::Locales

This file can be symlinked and thereby easily translated by POEdit. Further more the static methods can be used for stuff.

This module can be used to handel various language-stuff.

Constants

LANG_CONVERTIONS

Public Class Methods

ago_strings() click to toggle source

Returns an array of translate 'ago'-strings. View the source to see which.

# 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() click to toggle source

Returns an array of translated day-names ('_'-method must exist!).

Examples

Knj::Locales.days_arr #=> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
# File lib/knj/locale_strings.rb, line 6
def self.days_arr
  return [_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")]
end
days_short_arr() click to toggle source

Returns an array of short translated day-names.

Examples

Knj::Locales.days_short_arr #=> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
# File lib/knj/locale_strings.rb, line 13
def self.days_short_arr
  return [_("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun")]
end
lang() click to toggle source

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

Examples

Knj::Locales.lang #=> {"first" => "en", "second" => "GB", "full" => "en_GB"}
# 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})/)
  if !match
    match = locale_str.match(/^[a-z]{2}$/)
    raise "Could not understand language: '#{locale_str}'." unless match
  end
      
  return {
    "first" => match[1],
    "second" => match[2],
    "full" => "#{match[1]}_#{match[2]}"
  }
end
locale() click to toggle source

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

Examples

Knj::Locales.locale #=> 'en_GB'
Knj::Locales.locale #=> 'da_DK'
# File lib/knj/locales.rb, line 79
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 ::Kernel.const_defined?(:I18n) && !I18n.locale.to_s.strip.empty?
    return I18n.locale
  elsif ENV["LANGUAGE"]
    return ENV["LANGUAGE"]
  elsif ENV["LANG"]
    return ENV["LANG"]
  end
  
  return "en_GB"
end
localeconv() click to toggle source

Returns various localized information about the environment.

Examples

Knj::Locales.localeconv #=> {"decimal_point" => ".", "thousands_sep" => ",", "csv_delimiter" => ","}
# File lib/knj/locales.rb, line 36
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() click to toggle source

Returns an array of translated month-names.

Examples

Knj::Locales.months_arr #=> ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
# 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) click to toggle source

Returns a float from the formatted string according to the current locale.

Examples

Knj::Locales.number_in("123,456.68") #=> 123456.68
# File lib/knj/locales.rb, line 60
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) click to toggle source

Returns the given number as a formatted string according to the current locale.

Examples

Knj::Locales.number_out(123456.68) #=> "123,456.68"
# File lib/knj/locales.rb, line 68
def self.number_out(num_str, dec = 2)
  lc = Knj::Locales.localeconv
  
  require "php4r" if !Kernel.const_defined?(:Php4r)
  return Php4r.number_format(num_str, dec, lc["decimal_point"], lc["thousands_sep"])
end