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