]>
Commit | Line | Data |
---|---|---|
a5eae6b7 MR |
1 | <?php |
2 | /** | |
3 | * Functions involved in translating with gettext | |
4 | * @package awl | |
5 | * @subpackage Translation | |
6 | * @author Andrew McMillan <andrew@catalyst.net.nz> | |
7 | * @copyright Catalyst IT Ltd | |
8 | * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 | |
9 | */ | |
10 | if ( !function_exists("i18n") ) { | |
11 | /** | |
12 | * Mark a string as being internationalized. This is a semaphore method; it | |
13 | * does nothing but it allows us to easily identify strings that require | |
14 | * translation. Generally this is used to mark strings that will be stored | |
15 | * in the database (like descriptions of permissions). | |
16 | * | |
17 | * AWL uses GNU gettext for internationalization (i18n) and localization (l10n) of | |
18 | * text presented to the user. Gettext needs to know about all places involving strings, | |
19 | * that must be translated. Mark any place, where localization at runtime shall take place | |
20 | * by using the function translate(). | |
21 | * | |
22 | * In the help I have used 'xlate' rather than 'translate' and 'x18n' rather than 'i18n' | |
23 | * so that the tools skip this particular file for translation :-) | |
24 | * | |
25 | * E.g. instead of: | |
26 | * print 'TEST to be displayed in different languages'; | |
27 | * use: | |
28 | * print xlate('TEST to be displayed in different languages'); | |
29 | * and you are all set for pure literals. The translation teams will receive that literal | |
30 | * string as a job to translate and will translate it (when the message is clear enough). | |
31 | * At runtime the message is then localized when printed. | |
32 | * The input string can contain a hint to assist translators: | |
33 | * print xlate('TT <!-- abbreviation for Translation Test -->'); | |
34 | * The hint portion of the string will not be printed. | |
35 | * | |
36 | * But consider this case: | |
37 | * $message_to_be_localized = 'TEST to be displayed in different languages'; | |
38 | * print xlate($message_to_be_localized); | |
39 | * | |
40 | * The translate() function is called in the right place for runtime handling, but there | |
41 | * is no message at gettext preprocessing time to be given to the translation teams, | |
42 | * just a variable name. Translation of the variable name would break the code! So all | |
43 | * places potentially feeding this variable have to be marked to be given to translation | |
44 | * teams, but not translated at runtime! | |
45 | * | |
46 | * This method resolves all such cases. Simply mark the candidates: | |
47 | * $message_to_be_localized = x18n('TEST to be displayed in different languages'); | |
48 | * print xlate($message_to_be_localized); | |
49 | * | |
50 | * @param string the value | |
51 | * @return string the same value | |
52 | */ | |
53 | function i18n($value) { | |
54 | return $value; /* Just pass the value through */ | |
55 | } | |
56 | } | |
57 | ||
58 | ||
59 | if ( !function_exists("translate") ) { | |
60 | /** | |
61 | * Convert a string in English to whatever this user's locale is | |
62 | */ | |
63 | function translate( $en ) { | |
64 | $xl = gettext($en); | |
65 | dbg_error_log("I18N","Translated =%s= into =%s=", $en, $xl ); | |
66 | return $xl; | |
67 | } | |
68 | } | |
69 | ||
70 | ||
71 | if ( !function_exists("init_gettext") ) { | |
72 | /** | |
73 | * Initialise our use of Gettext | |
74 | */ | |
75 | function init_gettext( $domain, $location ) { | |
76 | bindtextdomain( $domain, $location ); | |
77 | $codeset = bind_textdomain_codeset( $domain, "UTF-8" ); | |
78 | textdomain( $domain ); | |
79 | dbg_error_log("I18N","Bound domain =%s= to location =%s= using character set =%s=", $domain, $location, $codeset ); | |
80 | } | |
81 | } | |
82 | ||
83 | ||
84 | if ( !function_exists("awl_set_locale") ) { | |
85 | /** | |
86 | * Set the translation to the user's locale. At this stage all we do is | |
87 | * call the gettext function. | |
88 | */ | |
89 | function awl_set_locale( $locale ) { | |
90 | global $c; | |
91 | ||
92 | if ( !is_array($locale) && ! preg_match('/^[a-z]{2}(_[A-Z]{2})?\./', $locale ) ) { | |
93 | $locale = array( $locale, $locale.".UTF-8"); | |
94 | } | |
95 | if ( $newlocale = setlocale( LC_ALL, $locale) ) { | |
96 | dbg_error_log("I18N","Set locale to =%s=", $newlocale ); | |
97 | $c->current_locale = $newlocale; | |
98 | } | |
99 | else { | |
100 | dbg_log_array("I18N","Unsupported locale: ", $locale, false ); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | ?> |