@gtkx/i18n 2.0.0-beta.1 → 2.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,174 +1,17 @@
1
- <p align="center">
2
- <img src="https://raw.githubusercontent.com/gtkx-org/gtkx/main/logo.svg" alt="GTKX" width="100" />
3
- </p>
1
+ # @gtkx/i18n
4
2
 
5
- <h1 align="center">GTKX</h1>
3
+ Gettext localization for GTKX through react-i18next.
6
4
 
7
- <p align="center">
8
- The React framework for Linux, built on GTK.<br />
9
- Write native GTK4 desktop applications with React and TypeScript.
10
- Real GTK widgets, the GNOME stack, and standard web tooling.
11
- </p>
5
+ Translate React content with `useTranslation` and `Trans`. The CLI extracts messages and compiles gettext catalogs; application bootstrap loads the catalog for the current locale.
12
6
 
13
- <p align="center">
14
- <a href="https://www.npmjs.com/package/create-gtkx"><img src="https://img.shields.io/npm/v/create-gtkx?color=cb3837&logo=npm&label=create-gtkx" alt="npm version" /></a>
15
- <a href="https://www.npmjs.com/package/create-gtkx"><img src="https://img.shields.io/npm/dm/create-gtkx?color=cb3837&logo=npm&label=downloads" alt="npm downloads" /></a>
16
- <img src="https://img.shields.io/badge/node-%E2%89%A526.7-339933?logo=node.js&logoColor=white" alt="Node >= 26.7" />
17
- <a href="https://github.com/gtkx-org/gtkx/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MPL--2.0-blue.svg" alt="License: MPL-2.0" /></a>
18
- <a href="https://github.com/gtkx-org/gtkx/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/gtkx-org/gtkx/ci.yml?branch=main&logo=github&label=CI" alt="CI status" /></a>
19
- <img src="https://img.shields.io/badge/TypeScript-strict-3178c6?logo=typescript&logoColor=white" alt="TypeScript" />
20
- </p>
21
-
22
- <p align="center">
23
- <a href="https://gtkx.dev">Homepage</a> &middot;
24
- <a href="https://gtkx.dev/guide/getting-started">Documentation</a> &middot;
25
- <a href="https://github.com/gtkx-org/gtkx/tree/main/examples">Examples</a> &middot;
26
- <a href="https://github.com/gtkx-org/gtkx/blob/main/CONTRIBUTING.md">Contributing</a>
27
- </p>
28
-
29
- ---
30
-
31
- <p align="center">
32
- <img src="https://raw.githubusercontent.com/gtkx-org/gtkx/main/examples/tutorial/assets/screenshot.png" alt="The Tasks app: an Adwaita window with a sidebar of smart views and colored lists on the left, and a boxed task list on the right." />
33
- </p>
34
-
35
- <p align="center">
36
- <em>The Tasks app you build in the <a href="https://gtkx.dev/tutorial/">tutorial</a>.</em>
37
- </p>
38
-
39
- ## Demo
40
-
41
- The intrinsic elements in this snippet render GTK4 widgets, and ordinary React hooks and events drive them:
42
-
43
- ```tsx
44
- import * as Gtk from "@gtkx/gi/gtk";
45
- import { GtkApplication, GtkApplicationWindow, GtkBox, GtkButton, GtkLabel } from "@gtkx/jsx/gtk";
46
- import { createRoot, quit } from "@gtkx/react";
47
- import { useState } from "react";
48
-
49
- const Counter = () => {
50
- const [count, setCount] = useState(0);
51
-
52
- return (
53
- <GtkApplicationWindow
54
- title="Hello GTKX"
55
- defaultWidth={400}
56
- defaultHeight={300}
57
- onCloseRequest={quit}
58
- >
59
- <GtkBox
60
- orientation={Gtk.Orientation.VERTICAL}
61
- spacing={20}
62
- marginTop={40}
63
- marginBottom={40}
64
- marginStart={40}
65
- marginEnd={40}
66
- valign={Gtk.Align.CENTER}
67
- halign={Gtk.Align.CENTER}
68
- >
69
- <GtkLabel cssClasses={["title-1"]}>Welcome to GTKX!</GtkLabel>
70
- <GtkLabel cssClasses={["title-2"]}>{`Count: ${count}`}</GtkLabel>
71
- <GtkButton
72
- label="Increment"
73
- onClicked={() => setCount((c) => c + 1)}
74
- cssClasses={["suggested-action", "pill"]}
75
- />
76
- </GtkBox>
77
- </GtkApplicationWindow>
78
- );
79
- };
80
-
81
- const App = () => (
82
- <GtkApplication>
83
- <Counter />
84
- </GtkApplication>
85
- );
86
-
87
- createRoot().render(<App />);
88
- ```
89
-
90
- This is the [`hello-world`](https://github.com/gtkx-org/gtkx/tree/main/examples/hello-world) example, with `app.tsx` and `index.tsx` combined into a single snippet. `@gtkx/gi` and `@gtkx/jsx` are per-project bindings generated by the CLI, not packages you install from npm.
91
-
92
- ## Why GTKX
93
-
94
- ### A declarative layer for the GNOME stack
95
-
96
- GTK4 is mature, and GtkBuilder XML can lay out a static interface, but nothing re-renders that interface when your application state changes, and nothing hot-reloads it as you work. GTKX adds that missing layer, and the tooling around it, on top of the stack you already know:
97
-
98
- - a React reconciler that exposes every GObject as a JSX element,
99
- - a CLI for scaffolding, development, and production builds,
100
- - a dev server with Fast Refresh that patches your running UI in place,
101
- - CSS-in-JS styling, React Hook Form-powered Adwaita controls, gettext-backed react-i18next localization, React Spring animations, React Navigation stack, tab, drawer, and split view navigators, and high-level list, grid, and dialog components,
102
- - a Testing Library-style API for querying and driving your widgets in tests,
103
- - and a Model Context Protocol (MCP) server that exposes your live app to AI agents.
104
-
105
- ### The full GNOME API surface
106
-
107
- React Native and similar frameworks hide the native toolkit so one API can run everywhere. GTKX exposes it: GTK4, Adwaita, and any other GObject-Introspection library on your system. Linux-only by design.
108
-
109
- ### Why Node.js, and why generated bindings
110
-
111
- GTKX runs on Node.js, which puts native modules, the npm ecosystem, and the tooling built for Node.js APIs within reach. GJS is GNOME's own runtime, built on SpiderMonkey rather than V8; node-gtk runs on Node.js but is lightly maintained, on the older nan/V8 ABI rather than N-API, and still centered on GTK3. The [Why GTKX guide](https://gtkx.dev/guide/why-gtkx) covers what running on Node.js means day to day.
112
-
113
- GTKX generates the TypeScript types and the native FFI calls from the same GObject-Introspection data, so the types cannot drift from the calls they back. Codegen covers the whole GTK4 and Adwaita surface.
114
-
115
- At runtime, the native Rust core calls straight into the system GTK4, Adwaita, and GLib libraries through libffi, without loading libgirepository at all.
116
-
117
- ## Quick start
118
-
119
- GTKX is Linux-only and needs Node.js 26.7 or later. See [Requirements](#requirements).
120
-
121
- Scaffold a new app with the `create-gtkx` initializer:
7
+ In an existing GTKX 2.0 beta app:
122
8
 
123
9
  ```sh
124
- npm create gtkx
10
+ npm install @gtkx/i18n@beta
125
11
  ```
126
12
 
127
- The same command works with other package managers: `pnpm create gtkx` or `yarn create gtkx`.
128
-
129
- Then run your new app:
130
-
131
- ```sh
132
- cd my-app
133
- npm run dev
134
- ```
135
-
136
- To go further, follow the [tutorial](https://gtkx.dev/tutorial/).
137
-
138
- ## Documentation
139
-
140
- The documentation at **[gtkx.dev](https://gtkx.dev)** includes a step-by-step tutorial that builds a complete GNOME app, from scaffolding to packaging and shipping, plus guides and a full API reference.
141
-
142
- **[Read the docs &rarr;](https://gtkx.dev/guide/why-gtkx)**
143
-
144
- ## Requirements
145
-
146
- GTKX is Linux-only. You need:
147
-
148
- - Linux with the GTK4 (4.20 or later), Adwaita (1.8 or later), and GLib development libraries
149
- - Node.js 26.7 or later
150
-
151
- The `@gtkx/native` addon ships prebuilt for x64 and arm64 glibc Linux; other targets need to build it from the GTKX repository, which requires a Rust toolchain.
152
-
153
- ## Examples
154
-
155
- Explore the [example apps](https://github.com/gtkx-org/gtkx/tree/main/examples):
156
-
157
- - [`hello-world`](https://github.com/gtkx-org/gtkx/tree/main/examples/hello-world): the counter above.
158
- - [`gtk-demo`](https://github.com/gtkx-org/gtkx/tree/main/examples/gtk-demo): a React port of the official GTK4 widget showcase, covering lists, dialogs, gestures, CSS, and OpenGL.
159
- - [`browser`](https://github.com/gtkx-org/gtkx/tree/main/examples/browser): a WebKitWebView-based web browser.
160
- - [`animations`](https://github.com/gtkx-org/gtkx/tree/main/examples/animations): a tour of `@gtkx/animated`, React Spring animations driven by the GTK frame clock.
161
- - [`navigation`](https://github.com/gtkx-org/gtkx/tree/main/examples/navigation): a tour of `@gtkx/navigation`, React Navigation's stack, tab, and drawer navigators rendered with libadwaita.
162
- - [`tutorial`](https://github.com/gtkx-org/gtkx/tree/main/examples/tutorial): the Tasks app the documentation builds.
163
-
164
- ## Status
165
-
166
- GTKX is stable and ready for production use.
167
-
168
- ## Contributing
169
-
170
- Contributions are welcome. See [CONTRIBUTING.md](https://github.com/gtkx-org/gtkx/blob/main/CONTRIBUTING.md), the [Code of Conduct](https://github.com/gtkx-org/gtkx/blob/main/CODE_OF_CONDUCT.md), and the [security policy](https://github.com/gtkx-org/gtkx/blob/main/SECURITY.md). Building the repo needs Node.js 26.7 or later, pnpm, and a Rust toolchain.
13
+ [Guide](https://gtkx.dev/v2/guide/internationalization) · [API reference](https://gtkx.dev/v2/reference/@gtkx/i18n/) · [GTKX](https://gtkx.dev)
171
14
 
172
- ## License
15
+ GTKX runs on Linux with Node.js and system native libraries. See [Getting Started](https://gtkx.dev/v2/guide/getting-started) for supported versions and installation. This README describes the 2.0 beta.
173
16
 
174
- GTKX is licensed under [MPL-2.0](https://github.com/gtkx-org/gtkx/blob/main/LICENSE).
17
+ [MPL-2.0](https://github.com/gtkx-org/gtkx/blob/main/LICENSE).
package/dist/backend.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { BackendModule } from "i18next";
2
2
  type GettextCatalog = {
3
3
  gettext(msgid: string): string;
4
- ngettext(msgid: string, msgidPlural: string, count: number | bigint): string;
5
- npgettext(context: string, msgid: string, msgidPlural: string, count: number | bigint): string;
6
- pgettext(context: string, msgid: string): string;
4
+ ngettext(msgid: string, msgidPlural: string, count: number): string;
5
+ npgettext(context: string, msgid: string, msgidPlural: string, count: number): string | undefined;
6
+ pgettext(context: string, msgid: string): string | undefined;
7
7
  };
8
8
  declare const GETTEXT_RESOURCE_KEY = "__gtkx_gettext_catalog__";
9
9
  declare const gettextBackend: BackendModule;
@@ -1 +1 @@
1
- {"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAI7C,KAAK,cAAc,GAAG;IAClB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE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I,OAAO,OAAO,KAAG,KAAK,IAAI,cAA0C,CAAC;AAE/F,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,gBAAgB,EAAE,KAAK,cAAc,EAAE,CAAC"}
package/dist/backend.js CHANGED
@@ -10,16 +10,15 @@ const gettextCatalog = {
10
10
  return GLib.dngettext(applicationId, msgid, msgidPlural, normalizeCount(count));
11
11
  },
12
12
  pgettext(context, msgid) {
13
- return GLib.dpgettext2(applicationId, context, msgid);
13
+ const contextual = contextualMsgid(context, msgid);
14
+ const translated = GLib.dgettext(applicationId, contextual);
15
+ return translated === contextual ? undefined : translated;
14
16
  },
15
17
  npgettext(context, msgid, msgidPlural, count) {
16
18
  const contextualSingular = contextualMsgid(context, msgid);
17
19
  const contextualPlural = contextualMsgid(context, msgidPlural);
18
20
  const translated = GLib.dngettext(applicationId, contextualSingular, contextualPlural, normalizeCount(count));
19
- if (translated === contextualSingular) {
20
- return msgid;
21
- }
22
- return translated === contextualPlural ? msgidPlural : translated;
21
+ return translated === contextualSingular || translated === contextualPlural ? undefined : translated;
23
22
  },
24
23
  };
25
24
  const gettextBackend = {
@@ -32,16 +31,10 @@ const gettextBackend = {
32
31
  },
33
32
  };
34
33
  const normalizeCount = (count) => {
35
- if (typeof count === "number") {
36
- if (!Number.isSafeInteger(count) || count < 0) {
37
- throw new RangeError("gettext counts must be non-negative safe integers");
38
- }
39
- return BigInt(count);
34
+ if (!Number.isSafeInteger(count) || count < 0) {
35
+ throw new RangeError("gettext counts must be non-negative safe integers");
40
36
  }
41
- if (count < 0n) {
42
- throw new RangeError("gettext counts must be non-negative");
43
- }
44
- return count;
37
+ return BigInt(count);
45
38
  };
46
39
  const contextualMsgid = (context, msgid) => `${context}${CONTEXT_SEPARATOR}${msgid}`;
47
40
  const isGettextCatalog = (value) => value === gettextCatalog;
@@ -1 +1 @@
1
- {"version":3,"file":"backend.js","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,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AAI,EAAE,EAAE,CAAC,oBAAoB,CAAC,EAAE,cAAc,EAAE,CAAC,CAAC;IAC/D,CAAC;CACJ,CAAC;AAEF,MAAM,cAAc,GAAG,CAAC,KAAsB,EAAU,EAAE;IACtD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YAC5C,MAAM,IAAI,UAAU,CAAC,mDAAmD,CAAC,CAAC;QAC9E,CAAC;QAED,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;IAED,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;QACb,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC,OAAe,EAAE,KAAa,EAAU,EAAE,CAAC,GAAG,OAAO,GAAG,iBAAiB,GAAG,KAAK,EAAE,CAAC;AAC7G,MAAM,gBAAgB,GAAG,CAAC,KAAc,EAA2B,EAAE,CAAC,KAAK,KAAK,cAAc,CAAC;AAE/F,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,gBAAgB,EAAuB,CAAC","sourcesContent":["import type { BackendModule } from \"i18next\";\nimport * as GLib from \"@gtkx/gi/glib\";\nimport { applicationId } from \"virtual:gtkx-config\";\n\ntype GettextCatalog = {\n gettext(msgid: string): string;\n ngettext(msgid: string, msgidPlural: string, count: number | bigint): string;\n npgettext(context: string, msgid: string, msgidPlural: string, count: number | bigint): string;\n pgettext(context: string, msgid: string): string;\n};\n\nconst CONTEXT_SEPARATOR = \"\\u{4}\";\nconst GETTEXT_RESOURCE_KEY = \"__gtkx_gettext_catalog__\";\n\nconst gettextCatalog: GettextCatalog = {\n gettext(msgid) {\n return GLib.dgettext(applicationId, msgid);\n },\n ngettext(msgid, msgidPlural, count) {\n return GLib.dngettext(applicationId, msgid, msgidPlural, normalizeCount(count));\n },\n pgettext(context, msgid) {\n return GLib.dpgettext2(applicationId, context, msgid);\n },\n npgettext(context, msgid, msgidPlural, count) {\n const contextualSingular = contextualMsgid(context, msgid);\n const contextualPlural = contextualMsgid(context, msgidPlural);\n\n const translated = GLib.dngettext(\n applicationId,\n contextualSingular,\n contextualPlural,\n normalizeCount(count),\n );\n\n if (translated === contextualSingular) {\n return msgid;\n }\n\n return translated === contextualPlural ? msgidPlural : translated;\n },\n};\n\nconst gettextBackend: BackendModule = {\n type: \"backend\",\n init() {\n return;\n },\n read(_language, _namespace, callback) {\n callback(null, { [GETTEXT_RESOURCE_KEY]: gettextCatalog });\n },\n};\n\nconst normalizeCount = (count: number | bigint): bigint => {\n if (typeof count === \"number\") {\n if (!Number.isSafeInteger(count) || count < 0) {\n throw new RangeError(\"gettext counts must be non-negative safe integers\");\n }\n\n return BigInt(count);\n }\n\n if (count < 0n) {\n throw new RangeError(\"gettext counts must be non-negative\");\n }\n\n return count;\n};\n\nconst contextualMsgid = (context: string, msgid: string): string => `${context}${CONTEXT_SEPARATOR}${msgid}`;\nconst isGettextCatalog = (value: unknown): value is GettextCatalog => value === gettextCatalog;\n\nexport { GETTEXT_RESOURCE_KEY, gettextBackend, isGettextCatalog, type GettextCatalog };\n"]}
1
+ {"version":3,"file":"backend.js","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AASpD,MAAM,iBAAiB,GAAG,OAAO,CAAC;AAClC,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAExD,MAAM,cAAc,GAAmB;IACnC,OAAO,CAAC,KAAK;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IAC/C,CAAC;IACD,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK;QA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AAE,CAAC;QAC5C,MAAM,IAAI,UAAU,CAAC,mDAAmD,CAAC,CAAC;IAC9E,CAAC;IAED,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC,OAAe,EAAE,KAAa,EAAU,EAAE,CAAC,GAAG,OAAO,GAAG,iBAAiB,GAAG,KAAK,EAAE,CAAC;AAC7G,MAAM,gBAAgB,GAAG,CAAC,KAAc,EAA2B,EAAE,CAAC,KAAK,KAAK,cAAc,CAAC;AAE/F,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,gBAAgB,EAAuB,CAAC","sourcesContent":["import type { BackendModule } from \"i18next\";\nimport * as GLib from \"@gtkx/gi/glib\";\nimport { applicationId } from \"virtual:gtkx-config\";\n\ntype GettextCatalog = {\n gettext(msgid: string): string;\n ngettext(msgid: string, msgidPlural: string, count: number): string;\n npgettext(context: string, msgid: string, msgidPlural: string, count: number): string | undefined;\n pgettext(context: string, msgid: string): string | undefined;\n};\n\nconst CONTEXT_SEPARATOR = \"\\u{4}\";\nconst GETTEXT_RESOURCE_KEY = \"__gtkx_gettext_catalog__\";\n\nconst gettextCatalog: GettextCatalog = {\n gettext(msgid) {\n return GLib.dgettext(applicationId, msgid);\n },\n ngettext(msgid, msgidPlural, count) {\n return GLib.dngettext(applicationId, msgid, msgidPlural, normalizeCount(count));\n },\n pgettext(context, msgid) {\n const contextual = contextualMsgid(context, msgid);\n const translated = GLib.dgettext(applicationId, contextual);\n\n return translated === contextual ? undefined : translated;\n },\n npgettext(context, msgid, msgidPlural, count) {\n const contextualSingular = contextualMsgid(context, msgid);\n const contextualPlural = contextualMsgid(context, msgidPlural);\n\n const translated = GLib.dngettext(\n applicationId,\n contextualSingular,\n contextualPlural,\n normalizeCount(count),\n );\n\n return translated === contextualSingular || translated === contextualPlural ? undefined : translated;\n },\n};\n\nconst gettextBackend: BackendModule = {\n type: \"backend\",\n init() {\n return;\n },\n read(_language, _namespace, callback) {\n callback(null, { [GETTEXT_RESOURCE_KEY]: gettextCatalog });\n },\n};\n\nconst normalizeCount = (count: number): bigint => {\n if (!Number.isSafeInteger(count) || count < 0) {\n throw new RangeError(\"gettext counts must be non-negative safe integers\");\n }\n\n return BigInt(count);\n};\n\nconst contextualMsgid = (context: string, msgid: string): string => `${context}${CONTEXT_SEPARATOR}${msgid}`;\nconst isGettextCatalog = (value: unknown): value is GettextCatalog => value === gettextCatalog;\n\nexport { GETTEXT_RESOURCE_KEY, gettextBackend, isGettextCatalog, type GettextCatalog };\n"]}
package/dist/i18n.js CHANGED
@@ -29,10 +29,7 @@ const pluralLookup = ({ catalog, context, count, msgid, msgidPlural }) => {
29
29
  if (context === undefined) {
30
30
  return catalog.ngettext(msgid, msgidPlural, count);
31
31
  }
32
- const contextual = catalog.npgettext(context, msgid, msgidPlural, count);
33
- return contextual === msgid || contextual === msgidPlural
34
- ? catalog.ngettext(msgid, msgidPlural, count)
35
- : contextual;
32
+ return catalog.npgettext(context, msgid, msgidPlural, count) ?? catalog.ngettext(msgid, msgidPlural, count);
36
33
  };
37
34
  const stringOption = (options, key) => {
38
35
  const value = options[key];
@@ -71,8 +68,7 @@ const contextualLookup = (catalog, source, context) => {
71
68
  if (context === undefined) {
72
69
  return catalog.gettext(source);
73
70
  }
74
- const translated = catalog.pgettext(context, source);
75
- return translated === source ? catalog.gettext(source) : translated;
71
+ return catalog.pgettext(context, source) ?? catalog.gettext(source);
76
72
  };
77
73
  const singularPointLookup = (catalog, msgid, options) => {
78
74
  const source = stringOption(options, "defaultValue") ?? msgid;
@@ -80,7 +76,7 @@ const singularPointLookup = (catalog, msgid, options) => {
80
76
  return translated === source ? undefined : translated;
81
77
  };
82
78
  const pointLookup = (catalog, msgid, options) => {
83
- if (typeof options.count === "number") {
79
+ if (options.count !== undefined) {
84
80
  return pluralPointLookup(catalog, msgid, options, options.count);
85
81
  }
86
82
  return singularPointLookup(catalog, msgid, options);
package/dist/i18n.js.map CHANGED
@@ -1 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,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAElF,OAAO,UAAU,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC;AAC1D,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAC,OAAuB,EAAE,KAAa,EAAE,OAAiB,EAAsB,EAAE;IAClG,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;QACpC,OAAO,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACrE,CAAC;IAED,OAAO,mBAAmB,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;AACxD,CAAC,CAAC;AAEF,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAErE,KAAK,OAAO,CAAC,IAAI,CAAC;IACd,SAAS,EAAE,aAAa;IACxB,WAAW,EAAE,KAAK;IAClB,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE;IACrC,gBAAgB,EAAE,OAAO;IACzB,YAAY,EAAE,GAAG;IACjB,GAAG,EAAE,MAAM;IACX,EAAE,EAAE,CAAC,aAAa,CAAC;IACnB,WAAW,EAAE,KAAK;CACrB,CAAC,CAAC","sourcesContent":["import type { I18nFormatModule, TOptions } from \"i18next\";\nimport i18next from \"i18next\";\nimport { initReactI18next } from \"react-i18next\";\nimport {\n GETTEXT_RESOURCE_KEY,\n gettextBackend,\n type GettextCatalog,\n isGettextCatalog,\n} from \"./backend.js\";\nimport { locale } from \"./locale.js\";\n\ntype GettextFormat = I18nFormatModule & {\n addLookupKeys(): void;\n getResource(language: string, namespace: string, msgid: string, options: TOptions): string | undefined;\n handleAsObject: boolean;\n};\n\ntype PluralLookup = {\n catalog: GettextCatalog;\n context: string | undefined;\n count: number;\n msgid: string;\n msgidPlural: string;\n};\n\nconst gettextFormat: GettextFormat = {\n type: \"i18nFormat\",\n handleAsObject: false,\n addLookupKeys() {\n return;\n },\n getResource(language, namespace, msgid, options) {\n const resource: unknown = i18next.getResource(language, namespace, GETTEXT_RESOURCE_KEY, {\n keySeparator: false,\n });\n\n if (!isGettextCatalog(resource)) {\n return;\n }\n\n return pointLookup(resource, msgid, options);\n },\n};\n\nconst getContext = (value: unknown): string | undefined => {\n if (typeof value !== \"string\" && typeof value !== \"number\") {\n return undefined;\n }\n\n const context = String(value);\n\n return context.length === 0 ? undefined : context;\n};\n\nconst pluralLookup = ({ catalog, context, count, msgid, msgidPlural }: PluralLookup): string => {\n if (context === undefined) {\n return catalog.ngettext(msgid, msgidPlural, count);\n }\n\n const contextual = catalog.npgettext(context, msgid, msgidPlural, count);\n\n return contextual === msgid || contextual === msgidPlural\n ? catalog.ngettext(msgid, msgidPlural, count)\n : contextual;\n};\n\nconst stringOption = (options: TOptions, key: string): string | undefined => {\n const value = options[key];\n\n return typeof value === \"string\" ? value : undefined;\n};\n\nconst assertSupportedPluralOptions = (options: TOptions): void => {\n if (options.ordinal === true || stringOption(options, \"defaultValue_zero\") !== undefined) {\n throw new RangeError(\"GNU gettext does not support ordinal or zero-specific plural forms\");\n }\n};\n\nconst pluralSources = (msgid: string, options: TOptions): { plural: string; singular: string } => {\n assertSupportedPluralOptions(options);\n const defaultValue = stringOption(options, \"defaultValue\");\n const defaultValueOne = stringOption(options, \"defaultValue_one\");\n const defaultValueOther = stringOption(options, \"defaultValue_other\");\n const hasPluralDefaults = defaultValueOne !== undefined || defaultValueOther !== undefined;\n\n if (!hasPluralDefaults) {\n return { plural: defaultValue ?? msgid, singular: msgid };\n }\n\n return {\n plural: defaultValueOther ?? defaultValue ?? msgid,\n singular: defaultValueOne ?? defaultValue ?? msgid,\n };\n};\n\nconst pluralPointLookup = (\n catalog: GettextCatalog,\n msgid: string,\n options: TOptions,\n count: number,\n): string => {\n const { plural, singular } = pluralSources(msgid, options);\n\n return pluralLookup({\n catalog,\n context: getContext(options.context),\n count,\n msgid: singular,\n msgidPlural: plural,\n });\n};\n\nconst contextualLookup = (\n catalog: GettextCatalog,\n source: string,\n context: string | undefined,\n): string => {\n if (context === undefined) {\n return catalog.gettext(source);\n }\n\n const translated = catalog.pgettext(context, source);\n\n return translated === source ? catalog.gettext(source) : translated;\n};\n\nconst singularPointLookup = (\n catalog: GettextCatalog,\n msgid: string,\n options: TOptions,\n): string | undefined => {\n const source = stringOption(options, \"defaultValue\") ?? msgid;\n const translated = contextualLookup(catalog, source, getContext(options.context));\n\n return translated === source ? undefined : translated;\n};\n\nconst pointLookup = (catalog: GettextCatalog, msgid: string, options: TOptions): string | undefined => {\n if (typeof options.count === \"number\") {\n return pluralPointLookup(catalog, msgid, options, options.count);\n }\n\n return singularPointLookup(catalog, msgid, options);\n};\n\ni18next.use(gettextBackend).use(gettextFormat).use(initReactI18next);\n\nvoid i18next.init({\n defaultNS: \"translation\",\n fallbackLng: false,\n initAsync: false,\n interpolation: { escapeValue: false },\n contextSeparator: \"\\u{4}\",\n keySeparator: \".\",\n lng: locale,\n ns: [\"translation\"],\n nsSeparator: false,\n});\n"]}
1
+ {"version":3,"file":"i18n.js","sourceRoot":"","sources":["../src/i18n.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EACH,oBAAoB,EACpB,cAAc,EAEd,gBAAgB,GACnB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAgBrC,MAAM,aAAa,GAAkB;IACjC,IAAI,EAAE,YAAY;IAClB,cAAc,EAAE,KAAK;IACrB,aAAa;QACT,OAAO;IACX,CAAC;IACD,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,OAA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CAAC;IACd,SAAS,EAAE,aAAa;IACxB,WAAW,EAAE,KAAK;IAClB,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE;IACrC,gBAAgB,EAAE,OAAO;IACzB,YAAY,EAAE,GAAG;IACjB,GAAG,EAAE,MAAM;IACX,EAAE,EAAE,CAAC,aAAa,CAAC;IACnB,WAAW,EAAE,KAAK;CACrB,CAAC,CAAC","sourcesContent":["import type { I18nFormatModule, TOptions } from \"i18next\";\nimport i18next from \"i18next\";\nimport { initReactI18next } from \"react-i18next\";\nimport {\n GETTEXT_RESOURCE_KEY,\n gettextBackend,\n type GettextCatalog,\n isGettextCatalog,\n} from \"./backend.js\";\nimport { locale } from \"./locale.js\";\n\ntype GettextFormat = I18nFormatModule & {\n addLookupKeys(): void;\n getResource(language: string, namespace: string, msgid: string, options: TOptions): string | undefined;\n handleAsObject: boolean;\n};\n\ntype PluralLookup = {\n catalog: GettextCatalog;\n context: string | undefined;\n count: number;\n msgid: string;\n msgidPlural: string;\n};\n\nconst gettextFormat: GettextFormat = {\n type: \"i18nFormat\",\n handleAsObject: false,\n addLookupKeys() {\n return;\n },\n getResource(language, namespace, msgid, options) {\n const resource: unknown = i18next.getResource(language, namespace, GETTEXT_RESOURCE_KEY, {\n keySeparator: false,\n });\n\n if (!isGettextCatalog(resource)) {\n return;\n }\n\n return pointLookup(resource, msgid, options);\n },\n};\n\nconst getContext = (value: unknown): string | undefined => {\n if (typeof value !== \"string\" && typeof value !== \"number\") {\n return undefined;\n }\n\n const context = String(value);\n\n return context.length === 0 ? undefined : context;\n};\n\nconst pluralLookup = ({ catalog, context, count, msgid, msgidPlural }: PluralLookup): string => {\n if (context === undefined) {\n return catalog.ngettext(msgid, msgidPlural, count);\n }\n\n return catalog.npgettext(context, msgid, msgidPlural, count) ?? catalog.ngettext(msgid, msgidPlural, count);\n};\n\nconst stringOption = (options: TOptions, key: string): string | undefined => {\n const value = options[key];\n\n return typeof value === \"string\" ? value : undefined;\n};\n\nconst assertSupportedPluralOptions = (options: TOptions): void => {\n if (options.ordinal === true || stringOption(options, \"defaultValue_zero\") !== undefined) {\n throw new RangeError(\"GNU gettext does not support ordinal or zero-specific plural forms\");\n }\n};\n\nconst pluralSources = (msgid: string, options: TOptions): { plural: string; singular: string } => {\n assertSupportedPluralOptions(options);\n const defaultValue = stringOption(options, \"defaultValue\");\n const defaultValueOne = stringOption(options, \"defaultValue_one\");\n const defaultValueOther = stringOption(options, \"defaultValue_other\");\n const hasPluralDefaults = defaultValueOne !== undefined || defaultValueOther !== undefined;\n\n if (!hasPluralDefaults) {\n return { plural: defaultValue ?? msgid, singular: msgid };\n }\n\n return {\n plural: defaultValueOther ?? defaultValue ?? msgid,\n singular: defaultValueOne ?? defaultValue ?? msgid,\n };\n};\n\nconst pluralPointLookup = (\n catalog: GettextCatalog,\n msgid: string,\n options: TOptions,\n count: number,\n): string => {\n const { plural, singular } = pluralSources(msgid, options);\n\n return pluralLookup({\n catalog,\n context: getContext(options.context),\n count,\n msgid: singular,\n msgidPlural: plural,\n });\n};\n\nconst contextualLookup = (\n catalog: GettextCatalog,\n source: string,\n context: string | undefined,\n): string => {\n if (context === undefined) {\n return catalog.gettext(source);\n }\n\n return catalog.pgettext(context, source) ?? catalog.gettext(source);\n};\n\nconst singularPointLookup = (\n catalog: GettextCatalog,\n msgid: string,\n options: TOptions,\n): string | undefined => {\n const source = stringOption(options, \"defaultValue\") ?? msgid;\n const translated = contextualLookup(catalog, source, getContext(options.context));\n\n return translated === source ? undefined : translated;\n};\n\nconst pointLookup = (catalog: GettextCatalog, msgid: string, options: TOptions): string | undefined => {\n if (options.count !== undefined) {\n return pluralPointLookup(catalog, msgid, options, options.count);\n }\n\n return singularPointLookup(catalog, msgid, options);\n};\n\ni18next.use(gettextBackend).use(gettextFormat).use(initReactI18next);\n\nvoid i18next.init({\n defaultNS: \"translation\",\n fallbackLng: false,\n initAsync: false,\n interpolation: { escapeValue: false },\n contextSeparator: \"\\u{4}\",\n keySeparator: \".\",\n lng: locale,\n ns: [\"translation\"],\n nsSeparator: false,\n});\n"]}
package/dist/locale.js CHANGED
@@ -17,11 +17,11 @@ function requireStringResult(result) {
17
17
  }
18
18
  function initializeLocale() {
19
19
  const localeDir = process.env.GTKX_LOCALE_DIR ?? fileURLToPath(new URL("locale", import.meta.url));
20
- const initialized = requireStringResult(setLocaleBinding(LC_ALL, ""));
20
+ requireStringResult(setLocaleBinding(LC_ALL, ""));
21
21
  requireStringResult(bindTextDomainBinding(applicationId, localeDir));
22
22
  requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));
23
23
  requireStringResult(textDomainBinding(applicationId));
24
- return initialized;
24
+ return new Intl.NumberFormat().resolvedOptions().locale;
25
25
  }
26
26
  export { locale };
27
27
  //# sourceMappingURL=locale.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"locale.js","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,IAAI,GAAG,WAAW,CAAC;AACzB,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,gBAAgB,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AACtF,MAAM,qBAAqB,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AAEnG,MAAM,4BAA4B,GAAG,CAAC,CAAC,IAAI,CACvC,IAAI,EACJ,yBAAyB,EACzB,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,EACxB,CAAC,CAAC,MAAM,EAAE,CACb,CAAC;AAEF,MAAM,iBAAiB,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AAC/E,MAAM,MAAM,GAAW,gBAAgB,EAAE,CAAC;AAE1C,SAAS,mBAAmB,CAAC,MAAe;IACxC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,IAAI,SAAS,CAAC,yCAAyC,CAAC,CAAC;IACnE,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,SAAS,gBAAgB;IACrB,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,aAAa,CAAC,IAAI,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IACnG,MAAM,WAAW,GAAG,mBAAmB,CAAC,gBAAgB,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC;IACtE,mBAAmB,CAAC,qBAAqB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC,CAAC;IACrE,mBAAmB,CAAC,4BAA4B,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC;IAClF,mBAAmB,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC,CAAC;IAEtD,OAAO,WAAW,CAAC;AACvB,CAAC;AAED,OAAO,EAAE,MAAM,EAAE,CAAC","sourcesContent":["import { t } from \"@gtkx/runtime\";\nimport { fileURLToPath } from \"node:url\";\nimport { applicationId } from \"virtual:gtkx-config\";\n\nconst LIBC = \"libc.so.6\";\nconst LC_ALL = 6;\nconst GETTEXT_CODESET = [\"UTF\", \"8\"].join(\"-\");\nconst setLocaleBinding = t.bind(LIBC, \"setlocale\", [t.int32, t.string()], t.string());\nconst bindTextDomainBinding = t.bind(LIBC, \"bindtextdomain\", [t.string(), t.string()], t.string());\n\nconst bindTextDomainCodesetBinding = t.bind(\n LIBC,\n \"bind_textdomain_codeset\",\n [t.string(), t.string()],\n t.string(),\n);\n\nconst textDomainBinding = t.bind(LIBC, \"textdomain\", [t.string()], t.string());\nconst locale: string = initializeLocale();\n\nfunction requireStringResult(result: unknown): string {\n if (typeof result !== \"string\") {\n throw new TypeError(\"Unable to initialize the process locale\");\n }\n\n return result;\n}\n\nfunction initializeLocale(): string {\n const localeDir = process.env.GTKX_LOCALE_DIR ?? fileURLToPath(new URL(\"locale\", import.meta.url));\n const initialized = requireStringResult(setLocaleBinding(LC_ALL, \"\"));\n requireStringResult(bindTextDomainBinding(applicationId, localeDir));\n requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));\n requireStringResult(textDomainBinding(applicationId));\n\n return initialized;\n}\n\nexport { locale };\n"]}
1
+ {"version":3,"file":"locale.js","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,IAAI,GAAG,WAAW,CAAC;AACzB,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,gBAAgB,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,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EtD,OAAO,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC,eAAe,EAAE,CAAC,MAAM,CAAC;AAC5D,CAAC;AAED,OAAO,EAAE,MAAM,EAAE,CAAC","sourcesContent":["import { t } from \"@gtkx/runtime\";\nimport { fileURLToPath } from \"node:url\";\nimport { applicationId } from \"virtual:gtkx-config\";\n\nconst LIBC = \"libc.so.6\";\nconst LC_ALL = 6;\nconst GETTEXT_CODESET = [\"UTF\", \"8\"].join(\"-\");\nconst setLocaleBinding = t.bind(LIBC, \"setlocale\", [t.int32, t.string()], t.string());\nconst bindTextDomainBinding = t.bind(LIBC, \"bindtextdomain\", [t.string(), t.string()], t.string());\n\nconst bindTextDomainCodesetBinding = t.bind(\n LIBC,\n \"bind_textdomain_codeset\",\n [t.string(), t.string()],\n t.string(),\n);\n\nconst textDomainBinding = t.bind(LIBC, \"textdomain\", [t.string()], t.string());\nconst locale: string = initializeLocale();\n\nfunction requireStringResult(result: unknown): string {\n if (typeof result !== \"string\") {\n throw new TypeError(\"Unable to initialize the process locale\");\n }\n\n return result;\n}\n\nfunction initializeLocale(): string {\n const localeDir = process.env.GTKX_LOCALE_DIR ?? fileURLToPath(new URL(\"locale\", import.meta.url));\n requireStringResult(setLocaleBinding(LC_ALL, \"\"));\n requireStringResult(bindTextDomainBinding(applicationId, localeDir));\n requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));\n requireStringResult(textDomainBinding(applicationId));\n\n return new Intl.NumberFormat().resolvedOptions().locale;\n}\n\nexport { locale };\n"]}
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@gtkx/i18n",
3
- "version": "2.0.0-beta.1",
4
- "description": "A react-i18next backend for GTKX applications using GNU gettext catalogs.",
3
+ "version": "2.0.0-beta.11",
4
+ "description": "Gettext localization for GTKX through react-i18next.",
5
5
  "keywords": [
6
6
  "gtkx",
7
7
  "gtk",
8
8
  "gtk4",
9
+ "adwaita",
10
+ "gnome",
9
11
  "react",
10
12
  "i18n",
11
13
  "gettext",
@@ -68,19 +70,20 @@
68
70
  "node": ">=26.7.0"
69
71
  },
70
72
  "dependencies": {
71
- "@gtkx/runtime": "2.0.0-beta.1",
72
- "i18next": "^26.4.1",
73
- "react-i18next": "^17.0.13"
73
+ "@gtkx/runtime": "2.0.0-beta.11",
74
+ "i18next": "^26.4.2",
75
+ "react-i18next": "^17.0.15"
74
76
  },
75
77
  "peerDependencies": {
76
- "react": "^19.2"
78
+ "@types/react": "^19.3",
79
+ "react": "^19.3"
77
80
  },
78
81
  "devDependencies": {
79
- "@gtkx/config": "2.0.0-beta.1",
80
- "@gtkx/testing": "2.0.0-beta.1",
81
- "@gtkx/vitest": "2.0.0-beta.1",
82
- "@types/react": "^19.2.18",
83
- "vitest": "^4.1.11"
82
+ "@gtkx/config": "2.0.0-beta.11",
83
+ "@gtkx/testing": "2.0.0-beta.11",
84
+ "@gtkx/vitest": "2.0.0-beta.11",
85
+ "@types/react": "^19.3.0",
86
+ "vitest": "^5.0.1"
84
87
  },
85
88
  "scripts": {
86
89
  "release": "tsx ../../scripts/release-package.ts"
package/src/backend.ts CHANGED
@@ -4,9 +4,9 @@ import { applicationId } from "virtual:gtkx-config";
4
4
 
5
5
  type GettextCatalog = {
6
6
  gettext(msgid: string): string;
7
- ngettext(msgid: string, msgidPlural: string, count: number | bigint): string;
8
- npgettext(context: string, msgid: string, msgidPlural: string, count: number | bigint): string;
9
- pgettext(context: string, msgid: string): string;
7
+ ngettext(msgid: string, msgidPlural: string, count: number): string;
8
+ npgettext(context: string, msgid: string, msgidPlural: string, count: number): string | undefined;
9
+ pgettext(context: string, msgid: string): string | undefined;
10
10
  };
11
11
 
12
12
  const CONTEXT_SEPARATOR = "\u{4}";
@@ -20,7 +20,10 @@ const gettextCatalog: GettextCatalog = {
20
20
  return GLib.dngettext(applicationId, msgid, msgidPlural, normalizeCount(count));
21
21
  },
22
22
  pgettext(context, msgid) {
23
- return GLib.dpgettext2(applicationId, context, msgid);
23
+ const contextual = contextualMsgid(context, msgid);
24
+ const translated = GLib.dgettext(applicationId, contextual);
25
+
26
+ return translated === contextual ? undefined : translated;
24
27
  },
25
28
  npgettext(context, msgid, msgidPlural, count) {
26
29
  const contextualSingular = contextualMsgid(context, msgid);
@@ -33,11 +36,7 @@ const gettextCatalog: GettextCatalog = {
33
36
  normalizeCount(count),
34
37
  );
35
38
 
36
- if (translated === contextualSingular) {
37
- return msgid;
38
- }
39
-
40
- return translated === contextualPlural ? msgidPlural : translated;
39
+ return translated === contextualSingular || translated === contextualPlural ? undefined : translated;
41
40
  },
42
41
  };
43
42
 
@@ -51,20 +50,12 @@ const gettextBackend: BackendModule = {
51
50
  },
52
51
  };
53
52
 
54
- const normalizeCount = (count: number | bigint): bigint => {
55
- if (typeof count === "number") {
56
- if (!Number.isSafeInteger(count) || count < 0) {
57
- throw new RangeError("gettext counts must be non-negative safe integers");
58
- }
59
-
60
- return BigInt(count);
61
- }
62
-
63
- if (count < 0n) {
64
- throw new RangeError("gettext counts must be non-negative");
53
+ const normalizeCount = (count: number): bigint => {
54
+ if (!Number.isSafeInteger(count) || count < 0) {
55
+ throw new RangeError("gettext counts must be non-negative safe integers");
65
56
  }
66
57
 
67
- return count;
58
+ return BigInt(count);
68
59
  };
69
60
 
70
61
  const contextualMsgid = (context: string, msgid: string): string => `${context}${CONTEXT_SEPARATOR}${msgid}`;
package/src/i18n.ts CHANGED
@@ -57,11 +57,7 @@ const pluralLookup = ({ catalog, context, count, msgid, msgidPlural }: PluralLoo
57
57
  return catalog.ngettext(msgid, msgidPlural, count);
58
58
  }
59
59
 
60
- const contextual = catalog.npgettext(context, msgid, msgidPlural, count);
61
-
62
- return contextual === msgid || contextual === msgidPlural
63
- ? catalog.ngettext(msgid, msgidPlural, count)
64
- : contextual;
60
+ return catalog.npgettext(context, msgid, msgidPlural, count) ?? catalog.ngettext(msgid, msgidPlural, count);
65
61
  };
66
62
 
67
63
  const stringOption = (options: TOptions, key: string): string | undefined => {
@@ -119,9 +115,7 @@ const contextualLookup = (
119
115
  return catalog.gettext(source);
120
116
  }
121
117
 
122
- const translated = catalog.pgettext(context, source);
123
-
124
- return translated === source ? catalog.gettext(source) : translated;
118
+ return catalog.pgettext(context, source) ?? catalog.gettext(source);
125
119
  };
126
120
 
127
121
  const singularPointLookup = (
@@ -136,7 +130,7 @@ const singularPointLookup = (
136
130
  };
137
131
 
138
132
  const pointLookup = (catalog: GettextCatalog, msgid: string, options: TOptions): string | undefined => {
139
- if (typeof options.count === "number") {
133
+ if (options.count !== undefined) {
140
134
  return pluralPointLookup(catalog, msgid, options, options.count);
141
135
  }
142
136
 
package/src/locale.ts CHANGED
@@ -28,12 +28,12 @@ function requireStringResult(result: unknown): string {
28
28
 
29
29
  function initializeLocale(): string {
30
30
  const localeDir = process.env.GTKX_LOCALE_DIR ?? fileURLToPath(new URL("locale", import.meta.url));
31
- const initialized = requireStringResult(setLocaleBinding(LC_ALL, ""));
31
+ requireStringResult(setLocaleBinding(LC_ALL, ""));
32
32
  requireStringResult(bindTextDomainBinding(applicationId, localeDir));
33
33
  requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));
34
34
  requireStringResult(textDomainBinding(applicationId));
35
35
 
36
- return initialized;
36
+ return new Intl.NumberFormat().resolvedOptions().locale;
37
37
  }
38
38
 
39
39
  export { locale };