PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/i18n/locale.e

http://github.com/tybor/Liberty
Specman e | 399 lines | 325 code | 45 blank | 29 comment | 11 complexity | 564d9de359ec252d3e302b83d74341d8 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4expanded class LOCALE
  5
  6insert
  7   TIME_HANDLER
  8      redefine
  9         out_in_tagged_out_memory, default_create
 10      end
 11
 12create {ANY}
 13   default_create, make, make_language, make_country, make_encoding
 14
 15feature {ANY}
 16   language: FIXED_STRING
 17   country: FIXED_STRING
 18   encoding: FIXED_STRING
 19
 20   out_in_tagged_out_memory
 21      do
 22         tagged_out_memory.append(language)
 23         if country /= Void then
 24            tagged_out_memory.extend('_')
 25            tagged_out_memory.append(country)
 26            if encoding /= Void then
 27               tagged_out_memory.extend('.')
 28               tagged_out_memory.append(encoding)
 29            end
 30         end
 31      end
 32
 33feature {ANY} -- some useful locale-related items
 34
 35   -- NOTE: All those features share the same once STRING.
 36   -- Be sure to copy the result if it is meant to be kept.
 37
 38   localized_text (text_id: ABSTRACT_STRING): STRING
 39         -- Generic text translation
 40      local
 41         tid, t: POINTER
 42      do
 43         tid := text_id.to_external
 44         t := i18n_localized_text(tid)
 45         Result := to_string(t)
 46         if tid = t then
 47            Result.prepend(once "???")
 48         end
 49      end
 50
 51    -- see nl_langinfo(3)
 52
 53   localized_time (time: TIME): STRING
 54      do
 55         Result := to_string(i18n_localized_time(time.time_memory))
 56      end
 57
 58   localized_date (time: TIME): STRING
 59      do
 60         Result := to_string(i18n_localized_date(time.time_memory))
 61      end
 62
 63   localized_time_and_date (time: TIME): STRING
 64      do
 65         Result := to_string(i18n_localized_time_and_date(time.time_memory))
 66      end
 67
 68   localized_day (day: INTEGER): STRING
 69      require
 70         day.in_range(0, 7) -- 0 and 7 are Sunday
 71      do
 72         Result := to_string(i18n_localized_day(day))
 73      end
 74
 75   localized_abbreviated_day (day: INTEGER): STRING
 76      require
 77         day.in_range(0, 7) -- 0 and 7 are Sunday
 78      do
 79         Result := to_string(i18n_localized_abbreviated_day(day))
 80      end
 81
 82   localized_month (month: INTEGER): STRING
 83      require
 84         month.in_range(1, 12)
 85      do
 86         Result := to_string(i18n_localized_month(month))
 87      end
 88
 89   localized_abbreviated_month (month: INTEGER): STRING
 90      require
 91         month.in_range(1, 12)
 92      do
 93         Result := to_string(i18n_localized_abbreviated_month(month))
 94      end
 95
 96   localized_radix_character: STRING
 97         -- (decimal dot, decimal comma, etc.)
 98      do
 99         Result := to_string(i18n_localized_radix_character)
100      end
101
102   localized_thousands_separator: STRING
103      do
104         Result := to_string(i18n_localized_thousands_separator)
105      end
106
107   localized_yes: STRING
108      do
109         Result := to_string(i18n_localized_yes)
110      end
111
112   localized_no: STRING
113      do
114         Result := to_string(i18n_localized_no)
115      end
116
117   localized_ante_meridiem: STRING
118      do
119         Result := to_string(i18n_localized_ante_meridiem)
120      end
121
122   localized_post_meridiem: STRING
123      do
124         Result := to_string(i18n_localized_post_meridiem)
125      end
126
127feature {}
128   to_string (p: POINTER): STRING
129      do
130         if p.is_not_null then
131            Result := once ""
132            Result.from_external_copy(p)
133         end
134      ensure
135         p.is_null implies Result = Void
136         p.is_not_null implies Result /= Void
137      end
138
139feature {ANY}
140   valid_spec (spec: ABSTRACT_STRING): BOOLEAN
141         -- Checking the validity of the argument of `make'.
142      local
143         i, j: INTEGER
144      do
145         i := spec.first_index_of('_')
146         j := spec.first_index_of('.')
147         if spec.valid_index(i) then
148            Result := spec.substring(spec.lower, i - 1).is_equal(spec.substring(spec.lower, i - 1).as_lower)
149            if Result then
150               if spec.valid_index(j) then
151                  Result := spec.substring(i + 1, j - 1).is_equal(spec.substring(i + 1, j - 1).as_upper)
152               else
153                  Result := spec.substring(i + 1, spec.upper).is_equal(spec.substring(i + 1, spec.upper).as_upper)
154               end
155            end
156         else
157            Result := spec.is_equal(spec.as_lower)
158         end
159      end
160
161feature {ANY}
162   make_language (a_language: ABSTRACT_STRING)
163      require
164         a_language.is_equal(a_language.as_lower)
165      do
166         language := a_language.intern
167         country := Void
168         encoding := Void
169      ensure
170         language.is_equal(a_language)
171         country = Void
172         encoding = Void
173      end
174
175   make_country (a_language, a_country: ABSTRACT_STRING)
176      require
177         a_language.is_equal(a_language.as_lower)
178         a_country.is_equal(a_country.as_upper)
179      do
180         language := a_language.intern
181         country := a_country.intern
182         encoding := Void
183      ensure
184         language.is_equal(a_language)
185         country.is_equal(a_country)
186         encoding = Void
187      end
188
189   make_encoding (a_language, a_country, a_encoding: ABSTRACT_STRING)
190      require
191         a_language.is_equal(a_language.as_lower)
192         a_country.is_equal(a_country.as_upper)
193         a_encoding /= Void
194      do
195         language := a_language.intern
196         country := a_country.intern
197         encoding := a_encoding.intern
198      ensure
199         language.is_equal(a_language)
200         country.is_equal(a_country)
201         encoding.is_equal(a_encoding)
202      end
203
204   make (spec: ABSTRACT_STRING)
205      require
206         valid_spec(spec)
207      local
208         i, j: INTEGER; l, c: ABSTRACT_STRING
209      do
210         i := spec.first_index_of('_')
211         j := spec.first_index_of('.')
212         if spec.valid_index(i) then
213            l := spec.substring(spec.lower, i - 1)
214            if spec.valid_index(j) then
215               c := spec.substring(i + 1, j - 1)
216               make_encoding(l, c, spec.substring(j + 1, spec.upper))
217            else
218               make_country(l, spec.substring(i + 1, spec.upper))
219            end
220         else
221            make_language(spec)
222         end
223      end
224
225   default_create
226      local
227         langp: POINTER; lang: STRING
228      do
229         langp := i18n_get_environment_language
230         if langp.is_not_null then
231            create lang.from_external(langp)
232         end
233         if lang /= Void and then valid_spec(lang) then
234            make(lang)
235         else
236            make(once "en")
237         end
238      end
239
240   i18n_get_environment_language: POINTER
241      external "plug_in"
242      alias "{
243         location: "externals"
244         module_name: "plugin"
245         feature_name: "i18n_get_environment_language()"
246      }"
247      end
248
249   i18n_localized_time (time: INTEGER_64): POINTER
250      external "plug_in"
251      alias "{
252         location: "externals"
253         module_name: "plugin"
254         feature_name: "i18n_localized_time"
255      }"
256      end
257
258   i18n_localized_date (time: INTEGER_64): POINTER
259      external "plug_in"
260      alias "{
261         location: "externals"
262         module_name: "plugin"
263         feature_name: "i18n_localized_date"
264      }"
265      end
266
267   i18n_localized_time_and_date (time: INTEGER_64): POINTER
268      external "plug_in"
269      alias "{
270         location: "externals"
271         module_name: "plugin"
272         feature_name: "i18n_localized_time_and_date"
273      }"
274      end
275
276   i18n_localized_day (day: INTEGER): POINTER
277      external "plug_in"
278      alias "{
279         location: "externals"
280         module_name: "plugin"
281         feature_name: "i18n_localized_day"
282      }"
283      end
284
285   i18n_localized_abbreviated_day (day: INTEGER): POINTER
286      external "plug_in"
287      alias "{
288         location: "externals"
289         module_name: "plugin"
290         feature_name: "i18n_localized_abbreviated_day"
291      }"
292      end
293
294   i18n_localized_month (month: INTEGER): POINTER
295      external "plug_in"
296      alias "{
297         location: "externals"
298         module_name: "plugin"
299         feature_name: "i18n_localized_month"
300      }"
301      end
302
303   i18n_localized_abbreviated_month (month: INTEGER): POINTER
304      external "plug_in"
305      alias "{
306         location: "externals"
307         module_name: "plugin"
308         feature_name: "i18n_localized_abbreviated_month"
309      }"
310      end
311
312   i18n_localized_radix_character: POINTER
313      external "plug_in"
314      alias "{
315         location: "externals"
316         module_name: "plugin"
317         feature_name: "i18n_localized_radix_character()"
318      }"
319      end
320
321   i18n_localized_thousands_separator: POINTER
322      external "plug_in"
323      alias "{
324         location: "externals"
325         module_name: "plugin"
326         feature_name: "i18n_localized_thousands_separator()"
327      }"
328      end
329
330   i18n_localized_yes: POINTER
331      external "plug_in"
332      alias "{
333         location: "externals"
334         module_name: "plugin"
335         feature_name: "i18n_localized_yes()"
336      }"
337      end
338
339   i18n_localized_no: POINTER
340      external "plug_in"
341      alias "{
342         location: "externals"
343         module_name: "plugin"
344         feature_name: "i18n_localized_no()"
345      }"
346      end
347
348   i18n_localized_ante_meridiem: POINTER
349      external "plug_in"
350      alias "{
351         location: "externals"
352         module_name: "plugin"
353         feature_name: "i18n_localized_ante_meridiem()"
354      }"
355      end
356
357   i18n_localized_post_meridiem: POINTER
358      external "plug_in"
359      alias "{
360         location: "externals"
361         module_name: "plugin"
362         feature_name: "i18n_localized_post_meridiem()"
363      }"
364      end
365
366   i18n_localized_text (text_id: POINTER): POINTER
367      external "plug_in"
368      alias "{
369         location: "externals"
370         module_name: "plugin"
371         feature_name: "i18n_localized_text"
372      }"
373      end
374
375invariant
376   language /= Void
377   encoding /= Void implies country /= Void
378
379end -- class LOCALE
380--
381-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
382--
383-- Permission is hereby granted, free of charge, to any person obtaining a copy
384-- of this software and associated documentation files (the "Software"), to deal
385-- in the Software without restriction, including without limitation the rights
386-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
387-- copies of the Software, and to permit persons to whom the Software is
388-- furnished to do so, subject to the following conditions:
389--
390-- The above copyright notice and this permission notice shall be included in
391-- all copies or substantial portions of the Software.
392--
393-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
394-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
395-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
396-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
397-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
398-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
399-- THE SOFTWARE.