File: locale.rb

package info (click to toggle)
libgettext-ruby 1.7.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,892 kB
  • ctags: 1,198
  • sloc: ruby: 6,738; ansic: 67; makefile: 38; sql: 14; sh: 6
file content (217 lines) | stat: -rw-r--r-- 5,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
=begin
  locale.rb - Locale module

  Copyright (C) 2002-2006  Masao Mutoh

  You may redistribute it and/or modify it under the same
  license terms as Ruby.

  $Id: locale.rb,v 1.11 2006/03/15 16:34:05 mutoh Exp $
=end

require 'gettext/locale_object'

begin
  require 'locale_system'
rescue LoadError
  # This is used for installation process(from rake only)
  # And deprecated these constants.
  module Locale
    # Deprecated.
    CTYPE = 0
    # Deprecated.
    NUMERIC = 1
    # Deprecated.
    TIME = 2
    # Deprecated.
    COLLATE = 3
    # Deprecated.
    MONETARY = 4
    # Deprecated.
    MESSAGES = 5
    # Deprecated.
    ALL = 6
  end
end

if /cygwin|mingw|win32/ =~ RUBY_PLATFORM
  require 'gettext/locale_win32'
else
  require 'gettext/locale_posix'
end

# Locale module manages the locale informations of the application.
module Locale
  unless defined? CTYPE
    CTYPE = 0    #:nodoc:
    NUMERIC = 1  #:nodoc:
    TIME = 2     #:nodoc:
    COLLATE = 3  #:nodoc:
    MONETARY = 4 #:nodoc:
    MESSAGES = 5 #:nodoc:
    ALL = 6      #:nodoc:
  end

  @@default = nil
  @@current = nil

  module_function
  # Sets the default locale (Locale::Object).
  #
  # * locale: the default locale
  # * Returns: self.
  def set_default(locale)
    @@default = locale
    if @@default
      @@default.charset ||= @@locale_system_module.get_charset(locale)
    end
    self
  end
  # Same as Locale.set_default.
  #
  # * locale: the default locale (Locale::Object).
  # * Returns: locale.
  def default=(locale)
    set_default(locale)
    @@default
  end

  # Gets the system locale.
  # * Returns: the system locale (Locale::Object).
  def system
    @@locale_system_module.system
  end

  # Gets the default locale.
  #
  # If the default locale not set, this returns system locale.
  # * Returns: the default locale (Locale::Object).
  def default
    @@default ? @@default : system
  end

  # Gets the current locale (Locale::Object).
  #
  # If the current locale is not set, this returns default locale. 
  # * Returns: the current locale (Locale::Object).
  def current
    @@current = default unless @@current
    @@current
  end

  # Sets a locale as the current locale.
  #
  # This returns the current Locale::Object.
  # * lang: Locale::Object or locale name(String), or language name.
  # * country: the country code(String)
  # * charset: the charset(override the charset even if the locale name has charset).
  # * Returns: self
  #
  #    Locale.set_current("ja_JP.eucJP")
  #    Locale.set_current("ja", "JP")
  #    Locale.set_current("ja", "JP", "eucJP")
  #    Locale.set_current("ja", nil, "eucJP")
  #    Locale.set_current(Locale::Object.new("ja", "JP", "eucJP"))
  def set_current(lang, country = nil, charset = nil)
    if lang == nil
      @@current = nil
    else
      if lang.kind_of? Locale::Object
	@@current = lang
      else
	@@current = Locale::Object.new(lang, country, charset)
      end
      @@current.charset ||= @@locale_system_module.get_charset(@@current)
    end
    self
  end

  # Sets a current locale. This is a single argument version of Locale.set_current.
  #
  # * lang: the Locale::Object
  # * Returns: the current locale (Locale::Object).
  #
  #    Locale.current = "ja_JP.eucJP"
  #    Locale.current = Locale::Object.new("ja", "JP", "eucJP")
  def current=(lang)
    set_current(lang)
    @@current
  end

  # call-seq:
  #   set(lang)
  #   set(lang, country = nil, charset = nil)
  #
  # * lang: language as String, or Locale::Object
  # * country: country as String or nil
  # * charset: charset as String or nil
  # * Returns: a Locale::Object.
  #
  # Sets a default locale. This function is an alias of Locale.set_default.
  #
  # *Notice*: Locale.set(lctype, locale) is deprecated.
  def set(lang, country = nil, charset = nil)
    if lang.kind_of? Numeric
      warn "Locale.set(lctype, locale) is deprecated. Use Locale.set(locale) instead." 
      # Locale.set(Locale::CTYPE, "ja_JP.eucJP") "country" means locale.
      lang = country
      country = nil
      charset = nil
    end

    set_current(nil)
    if lang.kind_of? String
      set_default(Locale::Object.new(lang, country, charset))
    elsif lang.kind_of? Locale::Object
      set_default(lang)
    else
      set_default(nil)
    end
    @@default
  end

  # call-seq:
  #   current
  #   get
  #   get(lctype = nil)  (Deprecated)
  #
  # * Returns: the current locale (Locale::Object).
  #
  # *Notice*: lctype is deprecated. Use this with no parameter instead.
  def get(lctype = nil)
    warn "Locale.get(lctype) is deprecated. Use Locale.get (noparam) instead." if lctype
    @@current = default unless @@current
    @@current
  end

  # *Deprecated*. Use Locale.set or Locale.set_current instead.
  def setlocale(lctype, loc)
    warn "Deprecated. Use Locale#set instead."
    set(lctype, loc)
  end

  # Same as charset. Gets the charset of the current locale.
  # * Returns: the charset of the current locale (String)
  def codeset
    current.charset
  end

  # Gets the charset of the current locale.
  # * Returns: the charset of the current locale (String)
  def charset
    codeset
  end 
  # Same as codeset. Returns the charset of the current locale. 
  # * Returns: the charset of the current locale (String)
  def current_charset
    codeset
  end

  # Clear default/current locale.
  # * Returns: self
  def clear
    set(nil)
    set_current(nil)
    self
  end
end