-
Notifications
You must be signed in to change notification settings - Fork 0
/
L10N.mon
141 lines (137 loc) · 5.4 KB
/
L10N.mon
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
/**
* Title: L10N.mon
* Description: L10N EPL Helper
* Copyright (c) 2020 Software AG, Darmstadt, Germany and/or its licensors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
* http:/www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.apamax;
using com.apama.correlator.Component;
/**
* Event wrapper for doing translation / localization of EPL applications.
*
* It's recommended that you import it into each monitor or event you use it in with:
*
<code>
L10N l10n;
action _(string message) returns string { return l10n.gettext(message); }
action _N(string singular, string plural, integer n) returns string { return l10n.ngettext(singular, plural, n); }
</code>
*
* Use the convenience functions whenever you want a translated string, eg:
*
<code>
send Reply(l10n.printf(_("Unfortunately this didn't work for %1$s because of %1$s"), input, error)) to "output";
</code>
*
* The L10N instance must be initialized to define the text domain and the location of the translations before use.
*
* The locale to translate to is selected from the LANGUAGE environment variable.
*/
event L10N
{
/**
* Import the plugin.
* @private
*/
import "L10NPlugin" as plugin;
/**
* Initialize this L10N instance for a particular textdomain using the default location for translations.
*
* The default translation location is APAMA_WORK/translations/textdomain/
*
* @param textdomain A unique string to locate the translations for this project
*/
action init(string textdomain)
{
initFromPath(textdomain, Component.getInfo("envp")["APAMA_WORK"]+"/translations/"+textdomain);
}
/**
* Initialize this L10N instance for a particular textdomain, reading the location of translations
* from a correlator property variable.
*
* Property values can be specified from a .properties file or with -D on the correlator command line.
*
* @param textdomain A unique string to locate the translations for this project.
* @param translationsdirprop The name of a correlator configuration property which contains a path to the translations.
*/
action initFromProperty(string textdomain, string translationsdirprop)
{
initFromPath(textdomain, Component.getConfigProperties()[translationsdirprop]);
}
/**
* Initialize this L10N instance for a particular textdomain, reading the location of translations
* from an explicit path
*
* @param textdomain A unique string to locate the translations for this project.
* @param translationsdir The path to the translations.
*/
action initFromPath(string textdomain, string translationsdir)
{
plugin.init(textdomain, translationsdir);
self.textdomain := textdomain;
}
/**
* Return the localized string in the appropriate language, or msgid unchanged if a translation cannot be found.
*
* Normally this will be aliased to _ by a convenience function.
*
* @param msgid The string to translate in the input language (usually English).
* @returns The translated string, or the input string if a translation is not found.
*/
action gettext(string msgid) returns string
{
return plugin.gettext(textdomain, msgid);
}
/**
* Return the localized string in the localized language with singular/plural selected based on
* a runtime parameter. If no translation is found the appropriate input parameter is returned.
*
* Normally this will be aliased to _N by a convenience function.
*
* @param singular The string to translate in the input language for n=1.
* @param plural The string to translate in the input language for n!1.
* @param n The number to use to determine if we should use the singular or plural translation.
* @returns The translated string, or the input string if a translation is not found.
*/
action ngettext(string singular, string plural, integer n) returns string
{
return plugin.ngettext(textdomain, singular, plural, n);
}
/**
* Use a printf-style format string to render the sequence of arguments.
*
* Often used in combination with the gettext and ngettext functions to insert parameters which
* might be in different locations in different translations. For example:
*
<code>
log l10n.printf(_N("Succesfully processed one %1$s", "Succesfully processed %2$d %1$ss", count), [<any>type, count]) at INFO;
</code>
*
* Notes:
<ul>
<li>Supports positional parameter notation of printf</li>
<li>Excess parameters will be ignored to allow for varying number of arguments in strings</li>
<li>Insufficient parameters will leave later ones unreplaced</li>
<li>Type is taken from the actual type of the argument passed in rather than the format string, however format annotations are honoured</li>
</ul>
* @param format A printf-style format string.
* @param args A sequence<any> of all the arguments to format into the format string
* @returns The string with the result substituted in
*/
action printf(string format, sequence<any> args) returns string
{
return plugin.printf(format, args);
}
/**
* The textdomain to use for subsequent calls.
* @private
*/
string textdomain;
}