summaryrefslogtreecommitdiff
path: root/doc/Gettext.md
blob: 05127304c586e329a3350c526d7c6b5701d1380d (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Gettext

> See also: [`examples/gettext.js`][examples-gettext] for usage examples

This module provides a convenience layer for the "gettext" family of functions,
relying on GLib for the actual implementation.

Example usage:

```js
const Gettext = imports.gettext;

Gettext.textdomain('myapp');
Gettext.bindtextdomain('myapp', '/usr/share/locale');

let translated = Gettext.gettext('Hello world!');
```

#### Import

When using ESModules:

```js
import Gettext from 'gettext';
```

When using legacy imports:

```js
const Gettext = imports.gettext;
```

[examples-gettext]: https://gitlab.gnome.org/GNOME/gjs/blob/HEAD/examples/gettext.js

### Gettext.LocaleCategory

An enumeration of locale categories supported by GJS.

* `CTYPE = 0` — Character classification
* `NUMERIC = 1` — Formatting of nonmonetary numeric values
* `TIME = 2` — Formatting of date and time values
* `COLLATE = 3` — String collation
* `MONETARY = 4` — Formatting of monetary values
* `MESSAGES = 5` — Localizable natural-language messages
* `ALL = 6` — All of the locale

### Gettext.setlocale(category, locale)

> Note: It is rarely, if ever, necessary to call this function in GJS

Parameters:
* category (`Gettext.LocaleCategory`) — A locale category
* locale (`String`|`null`) — A locale string, or `null` to query the locale

Returns:
* (`String`|`null`) — A locale string, or `null` if `locale` is not `null`

Set or query the program's current locale.

Example usage:

```js
Gettext.setlocale(Gettext.LocaleCategory.MESSAGES, 'en_US.UTF-8');
```

### Gettext.textdomain(domainName)

Parameters:
* domainName (`String`) — A translation domain

Set the default domain to `domainName`, which is used in all future gettext
calls. Note that this does not affect functions that take an explicit
`domainName` argument, such as `Gettext.dgettext()`.

Typically this will be the project name or another unique identifier. For
example, GNOME Calculator might use something like `"gnome-calculator"` while a
GNOME Shell Extension might use its extension UUID.

### Gettext.bindtextdomain(domainName, dirName)

Parameters:
* domainName (`String`) — A translation domain
* dirName (`String`) — A directory path

Specify `dirName` as the directory that contains translations for `domainName`.

In most cases, `dirName` will be the system locale directory, such as
`/usr/share/locale`. GNOME Shell's `ExtensionUtils.initTranslations()` method,
on the other hand, will check an extension's directory for a `locale`
subdirectory before falling back to the system locale directory.

### Gettext.gettext(msgid)

> Note: This is equivalent to calling `Gettext.dgettext(null, msgid)`

Parameters:
* msgid (`String`) — A string to translate

Returns:
* (`String`) — A translated message

This function is a wrapper of `dgettext()` which does not translate the message
if the default domain as set with `Gettext.textdomain()` has no translations for
the current locale.

### Gettext.dgettext(domainName, msgid)

> Note: This is an alias for [`GLib.dgettext()`][gdgettext]

Parameters:
* domainName (`String`|`null`) — A translation domain
* msgid (`String`) — A string to translate

Returns:
* (`String`) — A translated message

This function is a wrapper of `dgettext()` which does not translate the message
if the default domain as set with `Gettext.textdomain()` has no translations for
the current locale.

[gdgettext]: https://gjs-docs.gnome.org/glib20/glib.dgettext

### Gettext.dcgettext(domainName, msgid, category)

> Note: This is an alias for [`GLib.dcgettext()`][gdcgettext]

Parameters:
* domainName (`String`|`null`) — A translation domain
* msgid (`String`) — A string to translate
* category (`Gettext.LocaleCategory`) — A locale category

Returns:
* (`String`) — A translated message

This is a variant of `Gettext.dgettext()` that allows specifying a locale
category.

[gdcgettext]: https://gjs-docs.gnome.org/glib20/glib.dcgettext

### Gettext.ngettext(msgid1, msgid2, n)

> Note: This is equivalent to calling
> `Gettext.dngettext(null, msgid1, msgid2, n)`

Parameters:
* msgid1 (`String`) — The singular form of the string to be translated
* msgid2 (`String`) — The plural form of the string to be translated
* n (`Number`) — The number determining the translation form to use

Returns:
* (`String`) — A translated message

Translate a string that may or may not be plural, like "I have 1 apple" and
"I have 2 apples".

In GJS, this should be used in conjunction with [`Format.vprintf()`][vprintf],
which supports the same substitutions as `printf()`:

```js
const numberOfApples = Math.round(Math.random() + 1);
const translated = Format.vprintf(Gettext.ngettext('I have %d apple',
    'I have %d apples', numberOfApples), [numberOfApples]);
```

[vprintf]: https://gjs-docs.gnome.org/gjs/format.md#format-vprintf

### Gettext.dngettext(domainName, msgid1, msgid2, n)

> Note: This is an alias for [`GLib.dngettext()`][gdngettext]

Parameters:
* domainName (`String`|`null`) — A translation domain
* msgid1 (`String`) — A string to translate
* msgid2 (`String`) — A pluralized string to translate
* n (`Number`) — The number determining the translation form to use

Returns:
* (`String`) — A translated message

This function is a wrapper of `dngettext()` which does not translate the message
if the default domain as set with `textdomain()` has no translations for the
current locale.

[gdngettext]: https://gjs-docs.gnome.org/glib20/glib.dngettext

### Gettext.pgettext(context, msgid)

> Note: This is equivalent to calling `Gettext.dpgettext(null, context, msgid)`

Parameters:
* context (`String`|`null`) — A context to disambiguate `msgid`
* msgid (`String`) — A string to translate

Returns:
* (`String`) — A translated message

This is a variant of `Gettext.dgettext()` which supports a disambiguating
message context.

This is used to disambiguate a translation where the same word may be used
differently, depending on the situation. For example, in English "read" is the
same for both past and present tense, but may not be in other languages.

### Gettext.dpgettext(domainName, context, msgid)

> Note: This is an alias for [`GLib.dpgettext2()`][gdpgettext2]

Parameters:
* domainName (`String`|`null`) — A translation domain
* context (`String`|`null`) — A context to disambiguate `msgid`
* msgid (`String`) — A string to translate

Returns:
* (`String`) — A translated message

This is a variant of `Gettext.dgettext()` which supports a disambiguating
message context.

[gdpgettext2]: https://gjs-docs.gnome.org/glib20/glib.dpgettext2

### Gettext.domain(domainName)

> Note: This method is specific to GJS

Parameters:
* domainName (`String`) — A domain name

Returns:
* (`Object`) — An object with common gettext methods

Create an object with bindings for `Gettext.gettext()`, `Gettext.ngettext()`,
and `Gettext.pgettext()`, bound to a `domainName`.