Skip to content

Commit 70392eb

Browse files
authored
🤖 Merge PR DefinitelyTyped#72666 feat(gettext.js): bump version, fix incorrect types, add tests by @hkleungai
1 parent 7b24342 commit 70392eb

File tree

3 files changed

+94
-20
lines changed

3 files changed

+94
-20
lines changed

‎types/gettext.js/gettext.js-tests.ts‎

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,92 @@ import Gettext = require("gettext.js");
22

33
const instance = Gettext();
44

5-
const json: Gettext.JsonData = {
6-
"": {
5+
{
6+
const messages = {
77
language: "fr",
88
"plural-forms": "nplurals=2; plural=n>1;",
9-
},
10-
Welcome: "Bienvenue",
11-
"There is %1 apple": [
12-
"Il y a %1 pomme",
13-
"Il y a %1 pommes",
14-
],
15-
};
16-
17-
instance.loadJSON(json, "messages");
18-
instance.setLocale("fr");
19-
if (instance.ngettext("There is %1 apple", "There are %1 apples", 0) !== "Il y a 0 pomme") {
20-
throw new Error("Failed test");
21-
}
22-
if (instance.strfmt("There are %1 apples", 0) !== "There are 0 apples") {
23-
throw new Error("Failed test");
9+
} satisfies Gettext.JsonDataMessages;
10+
11+
// $ExpectType Gettext
12+
instance
13+
.setMessages("domain", "fr", messages)
14+
.setMessages("domain", "fr", messages, "nplurals=2; plural=n>1;");
15+
}
16+
17+
{
18+
const json = {
19+
"": {
20+
language: "fr",
21+
"plural-forms": "nplurals=2; plural=n>1;",
22+
},
23+
Welcome: "Bienvenue",
24+
"There is %1 apple": [
25+
"Il y a %1 pomme",
26+
"Il y a %1 pommes",
27+
],
28+
} satisfies Gettext.JsonData;
29+
30+
// $ExpectType Gettext
31+
instance
32+
.loadJSON(json)
33+
.loadJSON(json, "messages");
34+
}
35+
36+
{
37+
// $ExpectType Gettext
38+
instance.setLocale("fr");
39+
}
40+
41+
{
42+
// $ExpectType string
43+
instance.getLocale();
44+
}
45+
46+
{
47+
// $ExpectType Gettext | string
48+
instance.textdomain();
49+
// $ExpectType Gettext | string
50+
instance.textdomain("domain");
51+
}
52+
53+
{
54+
// $ExpectType string
55+
instance.gettext("message id");
56+
// $ExpectType string
57+
instance.gettext("message id", "arg 1", "arg 2");
58+
}
59+
60+
{
61+
// $ExpectType string
62+
instance.ngettext("There is %1 apple", "There are %1 apples", 0);
63+
// $ExpectType string
64+
instance.ngettext("There is %1 apple", "There are %1 apples", 0, "arg 1", "arg 2");
65+
}
66+
67+
{
68+
// $ExpectType string
69+
instance.pgettext("There is %1 apple", "There are %1 apples");
70+
// $ExpectType string
71+
instance.pgettext("There is %1 apple", "There are %1 apples", "arg 1", "arg 2");
72+
}
73+
74+
declare const nullableStr: string | null | undefined;
75+
declare const nullableNum: number | null | undefined;
76+
{
77+
// $ExpectType string
78+
instance.dcnpgettext(nullableStr, nullableStr, "There are %1 apples", nullableStr, nullableNum);
79+
// $ExpectType string
80+
instance.dcnpgettext(nullableStr, nullableStr, "There are %1 apples", nullableStr, nullableNum, "arg 1", "arg 2");
81+
}
82+
83+
{
84+
// $ExpectType string
85+
instance.strfmt("There is %1 apple");
86+
// $ExpectType string
87+
instance.strfmt("There is %1 apple", "arg 1", "arg 2");
88+
}
89+
90+
{
91+
// $ExpectType string[]
92+
instance.expand_locale("fr");
2493
}

‎types/gettext.js/index.d.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ declare namespace i18n {
2424
}
2525

2626
interface Gettext {
27-
setMessages(domain: string, locale: string, messages: JsonDataMessages, plural_forms?: PluralForm): Gettext;
28-
loadJSON(jsonData: JsonData, domain?: string): Gettext;
27+
setMessages(
28+
domain: string,
29+
locale: string,
30+
messages: JsonDataMessages,
31+
plural_forms?: string,
32+
): Gettext;
33+
loadJSON(jsonData: JsonData | string, domain?: string): Gettext;
2934
setLocale(locale: string): Gettext;
3035
getLocale(): string;
3136
textdomain(domain?: string): Gettext | string;

‎types/gettext.js/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@types/gettext.js",
4-
"version": "1.0.9999",
4+
"version": "2.0.9999",
55
"projects": [
66
"https://github.com/guillaumepotier/gettext.js"
77
],

0 commit comments

Comments
 (0)