@gtkx/i18n 2.0.0-beta.10 → 2.0.0-beta.12
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 +8 -168
- package/dist/backend.d.ts +3 -3
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +7 -14
- package/dist/backend.js.map +1 -1
- package/dist/i18n.js +4 -8
- package/dist/i18n.js.map +1 -1
- package/dist/locale.d.ts.map +1 -1
- package/dist/locale.js +20 -5
- package/dist/locale.js.map +1 -1
- package/package.json +16 -23
- package/src/backend.ts +13 -27
- package/src/i18n.ts +7 -31
- package/src/locale.ts +28 -11
- package/src/virtual-config.d.ts +2 -1
package/README.md
CHANGED
|
@@ -1,177 +1,17 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
3
|
+
Gettext localization for GTKX through react-i18next.
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
The React framework for Linux.<br />
|
|
9
|
-
Write native GNOME applications with React and TypeScript.
|
|
10
|
-
Real Adwaita and GTK4 widgets with 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
|
-
|
|
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> ·
|
|
24
|
-
<a href="https://gtkx.dev/guide/getting-started">Documentation</a> ·
|
|
25
|
-
<a href="https://github.com/gtkx-org/gtkx/tree/main/examples">Examples</a> ·
|
|
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
|
-
This app starts with an Adwaita application shell, renders native GTK4 widgets inside it, and uses ordinary React hooks and events:
|
|
42
|
-
|
|
43
|
-
```tsx
|
|
44
|
-
import * as Gtk from "@gtkx/gi/gtk";
|
|
45
|
-
import { AdwApplication, AdwApplicationWindow, AdwHeaderBar, AdwToolbarView } from "@gtkx/jsx/adw";
|
|
46
|
-
import { GtkBox, GtkButton, GtkLabel } from "@gtkx/jsx/gtk";
|
|
47
|
-
import { createRoot, quit } from "@gtkx/react";
|
|
48
|
-
import { useState } from "react";
|
|
49
|
-
|
|
50
|
-
const Counter = () => {
|
|
51
|
-
const [count, setCount] = useState(0);
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
<AdwApplicationWindow
|
|
55
|
-
title="Hello GTKX"
|
|
56
|
-
defaultWidth={400}
|
|
57
|
-
defaultHeight={300}
|
|
58
|
-
onCloseRequest={quit}
|
|
59
|
-
>
|
|
60
|
-
<AdwToolbarView topBar={<AdwHeaderBar />}>
|
|
61
|
-
<GtkBox
|
|
62
|
-
orientation={Gtk.Orientation.VERTICAL}
|
|
63
|
-
spacing={20}
|
|
64
|
-
marginTop={40}
|
|
65
|
-
marginBottom={40}
|
|
66
|
-
marginStart={40}
|
|
67
|
-
marginEnd={40}
|
|
68
|
-
valign={Gtk.Align.CENTER}
|
|
69
|
-
halign={Gtk.Align.CENTER}
|
|
70
|
-
>
|
|
71
|
-
<GtkLabel cssClasses={["title-1"]}>Welcome to GTKX!</GtkLabel>
|
|
72
|
-
<GtkLabel cssClasses={["title-2"]}>{`Count: ${String(count)}`}</GtkLabel>
|
|
73
|
-
<GtkButton
|
|
74
|
-
label="Increment"
|
|
75
|
-
onClicked={() => setCount((c) => c + 1)}
|
|
76
|
-
cssClasses={["suggested-action", "pill"]}
|
|
77
|
-
/>
|
|
78
|
-
</GtkBox>
|
|
79
|
-
</AdwToolbarView>
|
|
80
|
-
</AdwApplicationWindow>
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const App = () => (
|
|
85
|
-
<AdwApplication>
|
|
86
|
-
<Counter />
|
|
87
|
-
</AdwApplication>
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
createRoot().render(<App />);
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
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.
|
|
94
|
-
|
|
95
|
-
## Why GTKX
|
|
96
|
-
|
|
97
|
-
### Adwaita-first application development
|
|
98
|
-
|
|
99
|
-
GTKX uses Adwaita as the foundation for applications, windows, navigation, dialogs, and adaptive layouts, with the complete GTK4 toolkit underneath. It adds a declarative React layer and an integrated TypeScript toolchain to the GNOME platform:
|
|
100
|
-
|
|
101
|
-
- a React reconciler that exposes every GObject as a JSX element,
|
|
102
|
-
- a CLI for scaffolding, development, and production builds,
|
|
103
|
-
- a dev server with Fast Refresh that patches your running UI in place,
|
|
104
|
-
- 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,
|
|
105
|
-
- a Testing Library-style API for querying and driving your widgets in tests,
|
|
106
|
-
- and a Model Context Protocol (MCP) server that exposes your live app to AI agents.
|
|
107
|
-
|
|
108
|
-
### Native GNOME widgets, without a compatibility layer
|
|
109
|
-
|
|
110
|
-
React Native and similar frameworks hide the native toolkit so one API can run everywhere. GTKX renders real Adwaita and GTK4 widgets and exposes any other GObject-Introspection library on your system. It is GNOME-native and Linux-only by design.
|
|
111
|
-
|
|
112
|
-
### Why Node.js, and why generated bindings
|
|
113
|
-
|
|
114
|
-
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.
|
|
115
|
-
|
|
116
|
-
GTKX generates the TypeScript types and native FFI calls from the same GObject-Introspection data, so the types cannot drift from the calls they back. `Adw-1` is the sole default GIR root; its GIR include pulls in `Gtk-4.0`, so codegen covers both complete API surfaces. Neither belongs in a GTKX 2 `libraries` list.
|
|
117
|
-
|
|
118
|
-
At runtime, the native Rust core calls straight into the system Adwaita, GTK4, and GLib libraries through libffi, without loading libgirepository at all.
|
|
119
|
-
|
|
120
|
-
## Quick start
|
|
121
|
-
|
|
122
|
-
GTKX is Linux-only and needs Node.js 26.7 or later. See [Requirements](#requirements).
|
|
123
|
-
|
|
124
|
-
Scaffold a new app with the `create-gtkx` initializer:
|
|
7
|
+
In an existing GTKX app:
|
|
125
8
|
|
|
126
9
|
```sh
|
|
127
|
-
npm
|
|
10
|
+
npm install @gtkx/i18n@2
|
|
128
11
|
```
|
|
129
12
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
Then run your new app:
|
|
133
|
-
|
|
134
|
-
```sh
|
|
135
|
-
cd my-app
|
|
136
|
-
npm run dev
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
To go further, follow the [tutorial](https://gtkx.dev/tutorial/).
|
|
140
|
-
|
|
141
|
-
## Documentation
|
|
142
|
-
|
|
143
|
-
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.
|
|
144
|
-
|
|
145
|
-
**[Read the docs →](https://gtkx.dev/guide/why-gtkx)**
|
|
146
|
-
|
|
147
|
-
## Requirements
|
|
148
|
-
|
|
149
|
-
GTKX is Linux-only. You need:
|
|
150
|
-
|
|
151
|
-
- Linux with the Adwaita (1.8 or later), GTK4 (4.20 or later), and GLib development libraries
|
|
152
|
-
- Node.js 26.7 or later
|
|
153
|
-
|
|
154
|
-
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.
|
|
155
|
-
|
|
156
|
-
## Examples
|
|
157
|
-
|
|
158
|
-
Explore the [example apps](https://github.com/gtkx-org/gtkx/tree/main/examples):
|
|
159
|
-
|
|
160
|
-
- [`hello-world`](https://github.com/gtkx-org/gtkx/tree/main/examples/hello-world): the Adwaita counter above.
|
|
161
|
-
- [`gtk-demo`](https://github.com/gtkx-org/gtkx/tree/main/examples/gtk-demo): the official GTK4 widget showcase in an Adwaita application shell, covering lists, dialogs, gestures, CSS, and OpenGL.
|
|
162
|
-
- [`browser`](https://github.com/gtkx-org/gtkx/tree/main/examples/browser): an Adwaita browser built around `WebKitWebView`.
|
|
163
|
-
- [`animations`](https://github.com/gtkx-org/gtkx/tree/main/examples/animations): an Adwaita showcase for `@gtkx/animated`, with React Spring animations driven by the GTK frame clock.
|
|
164
|
-
- [`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.
|
|
165
|
-
- [`tutorial`](https://github.com/gtkx-org/gtkx/tree/main/examples/tutorial): the Tasks app the documentation builds.
|
|
166
|
-
|
|
167
|
-
## Status
|
|
168
|
-
|
|
169
|
-
GTKX is stable and ready for production use.
|
|
170
|
-
|
|
171
|
-
## Contributing
|
|
172
|
-
|
|
173
|
-
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)
|
|
174
14
|
|
|
175
|
-
|
|
15
|
+
GTKX requires Linux, Node.js, and the [native prerequisites](https://gtkx.dev/v2/guide/getting-started).
|
|
176
16
|
|
|
177
|
-
|
|
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
|
|
5
|
-
npgettext(context: string, msgid: string, msgidPlural: string, count: number
|
|
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;
|
package/dist/backend.d.ts.map
CHANGED
|
@@ -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,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,
|
|
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,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACpE,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAClG,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CAChE,CAAC;AAGF,QAAA,MAAM,oBAAoB,6BAA6B,CAAC;AAyBxD,QAAA,MAAM,cAAc,EAAE,aAQrB,CAAC;AAWF,QAAA,MAAM,gBAAgB,UAAW,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
|
-
|
|
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
|
-
|
|
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 (
|
|
36
|
-
|
|
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
|
-
|
|
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;
|
package/dist/backend.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;QAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,aAAa,EAAE,KAAK,EAAE,WAAW,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IACpF,CAAC;IACD,QAAQ,CAAC,OAAO,EAAE,KAAK;QACnB,OAAO,
|
|
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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MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,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(applicationId, contextualSingular, contextualPlural, normalizeCount(count));\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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import i18next from "i18next";
|
|
2
2
|
import { initReactI18next } from "react-i18next";
|
|
3
|
-
import { GETTEXT_RESOURCE_KEY, gettextBackend, isGettextCatalog
|
|
3
|
+
import { GETTEXT_RESOURCE_KEY, gettextBackend, isGettextCatalog } from "./backend.js";
|
|
4
4
|
import { locale } from "./locale.js";
|
|
5
5
|
const gettextFormat = {
|
|
6
6
|
type: "i18nFormat",
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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 (
|
|
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 +1 @@
|
|
|
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,
|
|
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,EAAE,oBAAoB,EAAE,cAAc,EAAuB,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3G,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,OAAO;QAC3C,MAAM,QAAQ,GAAY,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,oBAAoB,EAAE;YACrF,YAAY,EAAE,KAAK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AW,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 { GETTEXT_RESOURCE_KEY, gettextBackend, type GettextCatalog, isGettextCatalog } 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 = (catalog: GettextCatalog, msgid: string, options: TOptions, count: number): 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 = (catalog: GettextCatalog, source: string, context: string | undefined): 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 = (catalog: GettextCatalog, msgid: string, options: TOptions): 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.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"locale.d.ts","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"locale.d.ts","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"AAcA,QAAA,MAAM,MAAM,EAAE,MAA2B,CAAC;AAyC1C,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/locale.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t } from "@gtkx/runtime";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
3
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { applicationId } from "virtual:gtkx-config";
|
|
4
|
+
import { applicationId, localeDir } from "virtual:gtkx-config";
|
|
4
5
|
const LIBC = "libc.so.6";
|
|
5
6
|
const LC_ALL = 6;
|
|
6
7
|
const GETTEXT_CODESET = ["UTF", "8"].join("-");
|
|
@@ -16,12 +17,26 @@ function requireStringResult(result) {
|
|
|
16
17
|
return result;
|
|
17
18
|
}
|
|
18
19
|
function initializeLocale() {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
requireStringResult(bindTextDomainBinding(applicationId,
|
|
20
|
+
const directory = localeDir ?? bundledLocaleDir();
|
|
21
|
+
requireStringResult(setLocaleBinding(LC_ALL, ""));
|
|
22
|
+
requireStringResult(bindTextDomainBinding(applicationId, directory));
|
|
22
23
|
requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));
|
|
23
24
|
requireStringResult(textDomainBinding(applicationId));
|
|
24
|
-
return
|
|
25
|
+
return new Intl.NumberFormat().resolvedOptions().locale;
|
|
26
|
+
}
|
|
27
|
+
function bundledLocaleDir() {
|
|
28
|
+
const configUrl = new URL("gtkx-i18n.json", import.meta.url);
|
|
29
|
+
if (!existsSync(configUrl)) {
|
|
30
|
+
return fileURLToPath(new URL("locale", configUrl));
|
|
31
|
+
}
|
|
32
|
+
const config = JSON.parse(readFileSync(configUrl, "utf8"));
|
|
33
|
+
if (typeof config !== "object" ||
|
|
34
|
+
config === null ||
|
|
35
|
+
!("localeDir" in config) ||
|
|
36
|
+
typeof config.localeDir !== "string") {
|
|
37
|
+
throw new TypeError("Invalid GTKX locale configuration");
|
|
38
|
+
}
|
|
39
|
+
return fileURLToPath(new URL(config.localeDir, configUrl));
|
|
25
40
|
}
|
|
26
41
|
export { locale };
|
|
27
42
|
//# sourceMappingURL=locale.js.map
|
package/dist/locale.js.map
CHANGED
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"locale.js","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAE/D,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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AAC,SAAS,CAAC,EAAE,CAAC;QACzB,OAAO,aAAa,CAAC,IAAI,GAAG,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;IAEpE,IACI,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,CAAC,CAAC,WAAW,IAAI,MAAM,CAAC;QACxB,OAAO,MAAM,CAAC,SAAS,KAAK,QAAQ,EACtC,CAAC;QACC,MAAM,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO,aAAa,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;AAC/D,CAAC;AAED,OAAO,EAAE,MAAM,EAAE,CAAC","sourcesContent":["import { t } from \"@gtkx/runtime\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\nimport { applicationId, localeDir } 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(LIBC, \"bind_textdomain_codeset\", [t.string(), t.string()], t.string());\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 directory = localeDir ?? bundledLocaleDir();\n requireStringResult(setLocaleBinding(LC_ALL, \"\"));\n requireStringResult(bindTextDomainBinding(applicationId, directory));\n requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));\n requireStringResult(textDomainBinding(applicationId));\n\n return new Intl.NumberFormat().resolvedOptions().locale;\n}\n\nfunction bundledLocaleDir(): string {\n const configUrl = new URL(\"gtkx-i18n.json\", import.meta.url);\n\n if (!existsSync(configUrl)) {\n return fileURLToPath(new URL(\"locale\", configUrl));\n }\n\n const config: unknown = JSON.parse(readFileSync(configUrl, \"utf8\"));\n\n if (\n typeof config !== \"object\" ||\n config === null ||\n !(\"localeDir\" in config) ||\n typeof config.localeDir !== \"string\"\n ) {\n throw new TypeError(\"Invalid GTKX locale configuration\");\n }\n\n return fileURLToPath(new URL(config.localeDir, configUrl));\n}\n\nexport { locale };\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gtkx/i18n",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.0.0-beta.12",
|
|
4
|
+
"description": "Gettext localization for GTKX through react-i18next.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"gtkx",
|
|
7
7
|
"gtk",
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
"gettext",
|
|
14
14
|
"localization",
|
|
15
15
|
"linux",
|
|
16
|
-
"desktop"
|
|
16
|
+
"desktop",
|
|
17
|
+
"i18next",
|
|
18
|
+
"react-i18next"
|
|
17
19
|
],
|
|
18
20
|
"homepage": "https://gtkx.dev",
|
|
19
21
|
"bugs": {
|
|
@@ -56,35 +58,26 @@
|
|
|
56
58
|
"dist",
|
|
57
59
|
"src"
|
|
58
60
|
],
|
|
59
|
-
"nx": {
|
|
60
|
-
"targets": {
|
|
61
|
-
"lint": {
|
|
62
|
-
"options": {
|
|
63
|
-
"cwd": ".",
|
|
64
|
-
"command": "eslint {projectRoot}"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
61
|
"engines": {
|
|
70
62
|
"node": ">=26.7.0"
|
|
71
63
|
},
|
|
72
64
|
"dependencies": {
|
|
73
|
-
"@gtkx/runtime": "2.0.0-beta.
|
|
74
|
-
"i18next": "^26.
|
|
75
|
-
"react-i18next": "^17.0.
|
|
65
|
+
"@gtkx/runtime": "2.0.0-beta.12",
|
|
66
|
+
"i18next": "^26.5.2",
|
|
67
|
+
"react-i18next": "^17.0.21"
|
|
76
68
|
},
|
|
77
69
|
"peerDependencies": {
|
|
78
|
-
"react": "^19.
|
|
70
|
+
"@types/react": "^19.3",
|
|
71
|
+
"react": "^19.3"
|
|
79
72
|
},
|
|
80
73
|
"devDependencies": {
|
|
81
|
-
"@gtkx/config": "2.0.0-beta.
|
|
82
|
-
"@gtkx/testing": "2.0.0-beta.
|
|
83
|
-
"@gtkx/vitest": "2.0.0-beta.
|
|
84
|
-
"@types/react": "^19.
|
|
85
|
-
"vitest": "
|
|
74
|
+
"@gtkx/config": "2.0.0-beta.12",
|
|
75
|
+
"@gtkx/testing": "2.0.0-beta.12",
|
|
76
|
+
"@gtkx/vitest": "2.0.0-beta.12",
|
|
77
|
+
"@types/react": "^19.3.0",
|
|
78
|
+
"vitest": "5.0.5"
|
|
86
79
|
},
|
|
87
80
|
"scripts": {
|
|
88
|
-
"release": "
|
|
81
|
+
"release": "node ../../scripts/release/publish-package.ts"
|
|
89
82
|
}
|
|
90
83
|
}
|
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
|
|
8
|
-
npgettext(context: string, msgid: string, msgidPlural: string, count: number
|
|
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,24 +20,18 @@ const gettextCatalog: GettextCatalog = {
|
|
|
20
20
|
return GLib.dngettext(applicationId, msgid, msgidPlural, normalizeCount(count));
|
|
21
21
|
},
|
|
22
22
|
pgettext(context, msgid) {
|
|
23
|
-
|
|
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);
|
|
27
30
|
const contextualPlural = contextualMsgid(context, msgidPlural);
|
|
28
31
|
|
|
29
|
-
const translated = GLib.dngettext(
|
|
30
|
-
applicationId,
|
|
31
|
-
contextualSingular,
|
|
32
|
-
contextualPlural,
|
|
33
|
-
normalizeCount(count),
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
if (translated === contextualSingular) {
|
|
37
|
-
return msgid;
|
|
38
|
-
}
|
|
32
|
+
const translated = GLib.dngettext(applicationId, contextualSingular, contextualPlural, normalizeCount(count));
|
|
39
33
|
|
|
40
|
-
return translated === contextualPlural ?
|
|
34
|
+
return translated === contextualSingular || translated === contextualPlural ? undefined : translated;
|
|
41
35
|
},
|
|
42
36
|
};
|
|
43
37
|
|
|
@@ -51,20 +45,12 @@ const gettextBackend: BackendModule = {
|
|
|
51
45
|
},
|
|
52
46
|
};
|
|
53
47
|
|
|
54
|
-
const normalizeCount = (count: number
|
|
55
|
-
if (
|
|
56
|
-
|
|
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");
|
|
48
|
+
const normalizeCount = (count: number): bigint => {
|
|
49
|
+
if (!Number.isSafeInteger(count) || count < 0) {
|
|
50
|
+
throw new RangeError("gettext counts must be non-negative safe integers");
|
|
65
51
|
}
|
|
66
52
|
|
|
67
|
-
return count;
|
|
53
|
+
return BigInt(count);
|
|
68
54
|
};
|
|
69
55
|
|
|
70
56
|
const contextualMsgid = (context: string, msgid: string): string => `${context}${CONTEXT_SEPARATOR}${msgid}`;
|
package/src/i18n.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import type { I18nFormatModule, TOptions } from "i18next";
|
|
2
2
|
import i18next from "i18next";
|
|
3
3
|
import { initReactI18next } from "react-i18next";
|
|
4
|
-
import {
|
|
5
|
-
GETTEXT_RESOURCE_KEY,
|
|
6
|
-
gettextBackend,
|
|
7
|
-
type GettextCatalog,
|
|
8
|
-
isGettextCatalog,
|
|
9
|
-
} from "./backend.js";
|
|
4
|
+
import { GETTEXT_RESOURCE_KEY, gettextBackend, type GettextCatalog, isGettextCatalog } from "./backend.js";
|
|
10
5
|
import { locale } from "./locale.js";
|
|
11
6
|
|
|
12
7
|
type GettextFormat = I18nFormatModule & {
|
|
@@ -57,11 +52,7 @@ const pluralLookup = ({ catalog, context, count, msgid, msgidPlural }: PluralLoo
|
|
|
57
52
|
return catalog.ngettext(msgid, msgidPlural, count);
|
|
58
53
|
}
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return contextual === msgid || contextual === msgidPlural
|
|
63
|
-
? catalog.ngettext(msgid, msgidPlural, count)
|
|
64
|
-
: contextual;
|
|
55
|
+
return catalog.npgettext(context, msgid, msgidPlural, count) ?? catalog.ngettext(msgid, msgidPlural, count);
|
|
65
56
|
};
|
|
66
57
|
|
|
67
58
|
const stringOption = (options: TOptions, key: string): string | undefined => {
|
|
@@ -93,12 +84,7 @@ const pluralSources = (msgid: string, options: TOptions): { plural: string; sing
|
|
|
93
84
|
};
|
|
94
85
|
};
|
|
95
86
|
|
|
96
|
-
const pluralPointLookup = (
|
|
97
|
-
catalog: GettextCatalog,
|
|
98
|
-
msgid: string,
|
|
99
|
-
options: TOptions,
|
|
100
|
-
count: number,
|
|
101
|
-
): string => {
|
|
87
|
+
const pluralPointLookup = (catalog: GettextCatalog, msgid: string, options: TOptions, count: number): string => {
|
|
102
88
|
const { plural, singular } = pluralSources(msgid, options);
|
|
103
89
|
|
|
104
90
|
return pluralLookup({
|
|
@@ -110,25 +96,15 @@ const pluralPointLookup = (
|
|
|
110
96
|
});
|
|
111
97
|
};
|
|
112
98
|
|
|
113
|
-
const contextualLookup = (
|
|
114
|
-
catalog: GettextCatalog,
|
|
115
|
-
source: string,
|
|
116
|
-
context: string | undefined,
|
|
117
|
-
): string => {
|
|
99
|
+
const contextualLookup = (catalog: GettextCatalog, source: string, context: string | undefined): string => {
|
|
118
100
|
if (context === undefined) {
|
|
119
101
|
return catalog.gettext(source);
|
|
120
102
|
}
|
|
121
103
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return translated === source ? catalog.gettext(source) : translated;
|
|
104
|
+
return catalog.pgettext(context, source) ?? catalog.gettext(source);
|
|
125
105
|
};
|
|
126
106
|
|
|
127
|
-
const singularPointLookup = (
|
|
128
|
-
catalog: GettextCatalog,
|
|
129
|
-
msgid: string,
|
|
130
|
-
options: TOptions,
|
|
131
|
-
): string | undefined => {
|
|
107
|
+
const singularPointLookup = (catalog: GettextCatalog, msgid: string, options: TOptions): string | undefined => {
|
|
132
108
|
const source = stringOption(options, "defaultValue") ?? msgid;
|
|
133
109
|
const translated = contextualLookup(catalog, source, getContext(options.context));
|
|
134
110
|
|
|
@@ -136,7 +112,7 @@ const singularPointLookup = (
|
|
|
136
112
|
};
|
|
137
113
|
|
|
138
114
|
const pointLookup = (catalog: GettextCatalog, msgid: string, options: TOptions): string | undefined => {
|
|
139
|
-
if (
|
|
115
|
+
if (options.count !== undefined) {
|
|
140
116
|
return pluralPointLookup(catalog, msgid, options, options.count);
|
|
141
117
|
}
|
|
142
118
|
|
package/src/locale.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t } from "@gtkx/runtime";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
3
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { applicationId } from "virtual:gtkx-config";
|
|
4
|
+
import { applicationId, localeDir } from "virtual:gtkx-config";
|
|
4
5
|
|
|
5
6
|
const LIBC = "libc.so.6";
|
|
6
7
|
const LC_ALL = 6;
|
|
@@ -8,12 +9,7 @@ const GETTEXT_CODESET = ["UTF", "8"].join("-");
|
|
|
8
9
|
const setLocaleBinding = t.bind(LIBC, "setlocale", [t.int32, t.string()], t.string());
|
|
9
10
|
const bindTextDomainBinding = t.bind(LIBC, "bindtextdomain", [t.string(), t.string()], t.string());
|
|
10
11
|
|
|
11
|
-
const bindTextDomainCodesetBinding = t.bind(
|
|
12
|
-
LIBC,
|
|
13
|
-
"bind_textdomain_codeset",
|
|
14
|
-
[t.string(), t.string()],
|
|
15
|
-
t.string(),
|
|
16
|
-
);
|
|
12
|
+
const bindTextDomainCodesetBinding = t.bind(LIBC, "bind_textdomain_codeset", [t.string(), t.string()], t.string());
|
|
17
13
|
|
|
18
14
|
const textDomainBinding = t.bind(LIBC, "textdomain", [t.string()], t.string());
|
|
19
15
|
const locale: string = initializeLocale();
|
|
@@ -27,13 +23,34 @@ function requireStringResult(result: unknown): string {
|
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
function initializeLocale(): string {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
requireStringResult(bindTextDomainBinding(applicationId,
|
|
26
|
+
const directory = localeDir ?? bundledLocaleDir();
|
|
27
|
+
requireStringResult(setLocaleBinding(LC_ALL, ""));
|
|
28
|
+
requireStringResult(bindTextDomainBinding(applicationId, directory));
|
|
33
29
|
requireStringResult(bindTextDomainCodesetBinding(applicationId, GETTEXT_CODESET));
|
|
34
30
|
requireStringResult(textDomainBinding(applicationId));
|
|
35
31
|
|
|
36
|
-
return
|
|
32
|
+
return new Intl.NumberFormat().resolvedOptions().locale;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function bundledLocaleDir(): string {
|
|
36
|
+
const configUrl = new URL("gtkx-i18n.json", import.meta.url);
|
|
37
|
+
|
|
38
|
+
if (!existsSync(configUrl)) {
|
|
39
|
+
return fileURLToPath(new URL("locale", configUrl));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const config: unknown = JSON.parse(readFileSync(configUrl, "utf8"));
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
typeof config !== "object" ||
|
|
46
|
+
config === null ||
|
|
47
|
+
!("localeDir" in config) ||
|
|
48
|
+
typeof config.localeDir !== "string"
|
|
49
|
+
) {
|
|
50
|
+
throw new TypeError("Invalid GTKX locale configuration");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return fileURLToPath(new URL(config.localeDir, configUrl));
|
|
37
54
|
}
|
|
38
55
|
|
|
39
56
|
export { locale };
|
package/src/virtual-config.d.ts
CHANGED