aboutsummaryrefslogtreecommitdiff
path: root/core/text/i18n/doc.odin
blob: ef619451e6be22f5b85ec01917b6e21ee4c1d48f (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
//+build ignore
package i18n

/*
	The i18n package is flexible and easy to use.

	It has one call to get a translation: `get`, which the user can alias into something like `T`.

	`get`, referred to as `T` here, has a few different signatures.
	All of them will return the key if the entry can't be found in the active translation catalog.

	- `T(key)`              returns the translation of `key`.
	- `T(key, n)`           returns a pluralized translation of `key` according to value `n`.

	- `T(section, key)`     returns the translation of `key` in `section`.
	- `T(section, key, n)`  returns a pluralized translation of `key` in `section` according to value `n`.

	By default lookup take place in the global `i18n.ACTIVE` catalog for ease of use.
	If you want to override which translation to use, for example in a language preview dialog, you can use the following:

	- `T(key, n, catalog)`           returns the pluralized version of `key` from explictly supplied catalog.
	- `T(section, key, n, catalog)`  returns the pluralized version of `key` in `section` from explictly supplied catalog.

	If a catalog has translation contexts or sections, then ommitting it in the above calls looks up in section "".

	The default pluralization rule is n != 1, which is to say that passing n == 1 (or not passing n) returns the singular form.
	Passing n != 1 returns plural form 1.

	Should a language not conform to this rule, you can pass a pluralizer procedure to the catalog parser.
	This is a procedure that maps an integer to an integer, taking a value and returning which plural slot should be used.

	You can also assign it to a loaded catalog after parsing, of course.

	Some code examples follow.
*/

/*
```cpp
import "core:fmt"
import "core:text/i18n"

T :: i18n.get

mo :: proc() {
	using fmt

	err: i18n.Error

	/*
		Parse MO file and set it as the active translation so we can omit `get`'s "catalog" parameter.
	*/
	i18n.ACTIVE, err = i18n.parse_mo(#load("translations/nl_NL.mo"))
	defer i18n.destroy()

	if err != .None { return }

	/*
		These are in the .MO catalog.
	*/
	println("-----")
	println(T(""))
	println("-----")
	println(T("There are 69,105 leaves here."))
	println("-----")
	println(T("Hellope, World!"))
	println("-----")
	// We pass 1 into `T` to get the singular format string, then 1 again into printf.
	printf(T("There is %d leaf.\n", 1), 1)
	// We pass 42 into `T` to get the plural format string, then 42 again into printf.
	printf(T("There is %d leaf.\n", 42), 42)

	/*
		This isn't in the translation catalog, so the key is passed back untranslated.
	*/
	println("-----")
	println(T("Come visit us on Discord!"))
}

qt :: proc() {
	using fmt

	err: i18n.Error

	/*
		Parse QT file and set it as the active translation so we can omit `get`'s "catalog" parameter.
	*/
	i18n.ACTIVE, err = i18n.parse_qt(#load("translations/nl_NL-qt-ts.ts"))
	defer i18n.destroy()

	if err != .None {
		return
	}

	/*
		These are in the .TS catalog. As you can see they have sections.
	*/
	println("--- Page section ---")
	println("Page:Text for translation =", T("Page", "Text for translation"))
	println("-----")
	println("Page:Also text to translate =", T("Page", "Also text to translate"))
	println("-----")
	println("--- installscript section ---")
	println("installscript:99 bottles of beer on the wall =", T("installscript", "99 bottles of beer on the wall"))
	println("-----")
	println("--- apple_count section ---")
	println("apple_count:%d apple(s) =")
	println("\t 1  =", T("apple_count", "%d apple(s)", 1))
	println("\t 42 =", T("apple_count", "%d apple(s)", 42))
}
```
*/