rename `NativeString` to `CString`
[nit.git] / lib / gettext / gettext.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Internationalization of Strings using `gettext` library
16 module gettext is
17 new_annotation i18n
18 end
19
20 in "C" `{
21 #include <libintl.h>
22 #include <locale.h>
23 `}
24
25 redef class Sys
26 redef fun run do
27 "".to_cstring.set_locale
28 super
29 end
30 end
31
32 redef class CString
33 # Sets the locale of the program running
34 #
35 # This can be set at different times in the program,
36 # if used with an empty string, the locale will
37 # be set to the user's environment locale.
38 #
39 # For more info, SEE setlocale manual
40 fun set_locale `{
41 setlocale(LC_ALL, self);
42 `}
43 end
44
45 redef class String
46 # Gets the translation of `self` via gettext from `dir` using `domain`
47 #
48 # `domain`: Gettext domain, i.e. file/module name
49 # `dir`: Where the locale/LC_MESSAGES folder for this domain is located
50 fun get_translation(domain, dir: String): String do
51 bindtextdomain(domain, dir)
52 return dgettext(domain)
53 end
54
55 # Gettext `gettext`, SEE gettext manual for further info
56 fun gettext: String
57 import String.to_cstring, CString.to_s `{
58 return CString_to_s(gettext(String_to_cstring(self)));
59 `}
60
61 # Gettext `dgettext`, SEE gettext manual for further info
62 fun dgettext(domain: String): String
63 import String.to_cstring, CString.to_s `{
64 return CString_to_s(dgettext(String_to_cstring(domain), String_to_cstring(self)));
65 `}
66 end
67
68 # Gettext `bindtextdomain`, SEE gettext manual for further info
69 fun bindtextdomain(domain, dir: String) import String.to_cstring `{
70 bindtextdomain(String_to_cstring(domain), String_to_cstring(dir));
71 `}
72
73 # Gettext `textdomain`, SEE gettext manual for further info
74 fun textdomain(domain: String) import String.to_cstring `{
75 textdomain(String_to_cstring(domain));
76 `}