@riebeckite/plugin-l10n 0.0.1
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/LICENSE +201 -0
- package/README.md +91 -0
- package/README_ja.md +72 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +449 -0
- package/dist/src/l10n.d.ts +51 -0
- package/package.json +61 -0
- package/style.css +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright Rerurate_514
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# @riebeckite/plugin-l10n
|
|
2
|
+
|
|
3
|
+
Content localization for Riebeckite Markdown. It detects a locale and a separate translation identity for every note, prefixes non-default URLs, contributes a standard language switcher, and adds `hreflang` links. It does not translate Riebeckite UI strings.
|
|
4
|
+
|
|
5
|
+
[日本語](./README_ja.md)
|
|
6
|
+
|
|
7
|
+
## Basic setup
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { defineConfig } from "@riebeckite/core";
|
|
11
|
+
import { l10n } from "@riebeckite/plugin-l10n";
|
|
12
|
+
|
|
13
|
+
export default defineConfig({
|
|
14
|
+
plugins: [l10n({ defaultLang: "ja", languages: ["ja", "en", "zh-CN"] })],
|
|
15
|
+
});
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Locale detection
|
|
19
|
+
|
|
20
|
+
Use `README.en.md` as the recommended filename form. The plugin also accepts `README-en.md`, `README_en.md`, and configured BCP 47-style tags such as `README.en-US.md` and `README.zh-CN.md`.
|
|
21
|
+
|
|
22
|
+
Directory detection is also supported:
|
|
23
|
+
|
|
24
|
+
```text
|
|
25
|
+
content/en/README.md
|
|
26
|
+
content/ja/README.md
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Only directories matching configured `languages` are treated as locales. Frontmatter works anywhere:
|
|
30
|
+
|
|
31
|
+
```md
|
|
32
|
+
---
|
|
33
|
+
lang: en
|
|
34
|
+
translation: getting-started
|
|
35
|
+
---
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Conventions may be mixed. Built-in precedence is **frontmatter > filename > directory > default language**. Conflicts emit `L10N_LANGUAGE_CONFLICT`; use `strict: true` to fail the build instead. A duplicate `translation + lang` emits `L10N_DUPLICATE_TRANSLATION` (and also fails in strict mode).
|
|
39
|
+
|
|
40
|
+
`translation` is independent of locale and filename. Use it to group files with unrelated paths or names:
|
|
41
|
+
|
|
42
|
+
```text
|
|
43
|
+
日本語/はじめに.md lang: ja, translation: getting-started
|
|
44
|
+
English/getting-started.md lang: en, translation: getting-started
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## URLs and missing translations
|
|
48
|
+
|
|
49
|
+
The default language keeps Core's resolved URL (`/about`); other languages are prefixed (`/en/about`). The plugin augments the existing content-location resolver, so it works with other URL plugins rather than creating a router. Different source paths or a permalink plugin can provide different resolved URLs for translations.
|
|
50
|
+
|
|
51
|
+
No fallback pages are generated. `getLocalization(manifest, slug)` returns `availableLanguages` and language-to-href `translations` only for notes that exist; `getLocalizedContent(manifest, slug, lang)` returns `null` for a missing translation.
|
|
52
|
+
|
|
53
|
+
## Standard LanguageSwitcher
|
|
54
|
+
|
|
55
|
+
`l10n(...)` publishes the built-in, styled LanguageSwitcher in the standard `article.after-meta` layout slot. A standard Site consumer renders that slot, so no l10n-specific article integration is needed. It is omitted when a page has fewer than two real translations.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// Keep URLs and metadata but do not publish UI.
|
|
59
|
+
l10n({ defaultLang: "ja", languages: ["ja", "en"], ui: false });
|
|
60
|
+
|
|
61
|
+
// Use another standard slot, or replace the server-rendered component.
|
|
62
|
+
l10n({
|
|
63
|
+
defaultLang: "ja",
|
|
64
|
+
languages: ["ja", "en"],
|
|
65
|
+
ui: {
|
|
66
|
+
slot: "article.footer",
|
|
67
|
+
render: ({ localization }) => `<p>${localization.lang}</p>`,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Themes can override the default component through its `.l10n-switcher` CSS classes. `ui.render` is framework-neutral HTML so custom Sites can supply their own server-rendered component without coupling the plugin to a specific app.
|
|
73
|
+
|
|
74
|
+
## Custom detector
|
|
75
|
+
|
|
76
|
+
For another convention, provide `detect`. Its language is considered after frontmatter and before filename/directory; its `translationId` is explicit.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
l10n({
|
|
80
|
+
defaultLang: "ja",
|
|
81
|
+
languages: ["ja", "en", "fr"],
|
|
82
|
+
detect: ({ path }) =>
|
|
83
|
+
path.startsWith("French/") ? { lang: "fr", translationId: "hello" } : undefined,
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Links and SEO
|
|
88
|
+
|
|
89
|
+
Before the existing Content Graph is built, WikiLink graph targets are switched to the source note's language when that translation exists; otherwise their original target remains. No second graph is created. Normal Markdown links retain their authored destination.
|
|
90
|
+
|
|
91
|
+
Each translated entry receives one `<link rel="alternate" hreflang="…">` per existing translation through Core's `headTags` extension point. The site shell remains responsible for rendering those tags and for selecting `<html lang>` for a request.
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @riebeckite/plugin-l10n
|
|
2
|
+
|
|
3
|
+
Riebeckite の Markdown コンテンツをローカライズするプラグインです。UI 文言の翻訳は担当しませんが、標準の言語切替 UI を提供します。
|
|
4
|
+
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
## 基本設定
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { l10n } from "@riebeckite/plugin-l10n";
|
|
11
|
+
|
|
12
|
+
l10n({ defaultLang: "ja", languages: ["ja", "en", "zh-CN"] });
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## ロケール検出と翻訳グループ
|
|
16
|
+
|
|
17
|
+
ファイル名は `README.en.md` を推奨します。`README-en.md`、`README_en.md`、`README.en-US.md`、`README.zh-CN.md` も利用できます。設定済みの言語名に一致する先頭ディレクトリ(`en/README.md`)も検出します。
|
|
18
|
+
|
|
19
|
+
frontmatter も利用できます。
|
|
20
|
+
|
|
21
|
+
```md
|
|
22
|
+
---
|
|
23
|
+
lang: en
|
|
24
|
+
translation: getting-started
|
|
25
|
+
---
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
組み合わせた場合の組み込み規則の優先順位は **frontmatter > ファイル名 > ディレクトリ > 既定言語** です。矛盾は `L10N_LANGUAGE_CONFLICT`、同一 `translation + lang` の重複は `L10N_DUPLICATE_TRANSLATION` として診断します。`strict: true` ではビルドを失敗させます。
|
|
29
|
+
|
|
30
|
+
`translation` はロケール検出とは別です。異なるパス・ファイル名でも明示的な ID でグループ化できます。翻訳がない言語のページを複製・生成することはありません。
|
|
31
|
+
|
|
32
|
+
## URL・テーマ・SEO
|
|
33
|
+
|
|
34
|
+
既定言語は既存の URL(`/about`)、その他は言語プレフィックス付き(`/en/about`)です。既存の Content Location を拡張するため、別の URL 解決プラグインとも併用できます。翻訳ごとに URL を変える場合は、異なるソースパスまたは permalink プラグインを使います。
|
|
35
|
+
|
|
36
|
+
テーマは `getLocalization(manifest, slug)` から現在の言語、存在する言語、各翻訳の href を取得できます。`getLocalizedContent()` は翻訳がなければ `null` を返します。
|
|
37
|
+
|
|
38
|
+
## 標準 LanguageSwitcher
|
|
39
|
+
|
|
40
|
+
`l10n(...)` は標準記事レイアウトの `article.after-meta` スロットへ、組み込みの LanguageSwitcher を登録します。標準 Site consumer がこのスロットを描画するため、l10n 専用の記事レイアウト実装は不要です。実在する翻訳が 2 つ未満のページには表示されません。
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// URL とメタデータは維持し、UI だけを無効化します。
|
|
44
|
+
l10n({ defaultLang: "ja", languages: ["ja", "en"], ui: false });
|
|
45
|
+
|
|
46
|
+
// 配置の変更、またはサーバー描画コンポーネントの置換。
|
|
47
|
+
l10n({
|
|
48
|
+
defaultLang: "ja",
|
|
49
|
+
languages: ["ja", "en"],
|
|
50
|
+
ui: {
|
|
51
|
+
slot: "article.footer",
|
|
52
|
+
render: ({ localization }) => `<p>${localization.lang}</p>`,
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Theme は `.l10n-switcher` の CSS class を上書きできます。`ui.render` はフレームワーク非依存の HTML renderer なので、カスタム Site は任意のサーバー描画コンポーネントへ置き換えられます。
|
|
58
|
+
|
|
59
|
+
各ページには実在する翻訳だけを対象に `hreflang` の alternate link を追加します。WikiLink の Content Graph は同言語の翻訳を優先し、なければ元のリンク先を使います。通常の Markdown リンクは記述された URL を維持します。
|
|
60
|
+
|
|
61
|
+
## 独自検出
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
l10n({
|
|
65
|
+
defaultLang: "ja",
|
|
66
|
+
languages: ["ja", "en", "fr"],
|
|
67
|
+
detect: ({ path }) =>
|
|
68
|
+
path.startsWith("French/") ? { lang: "fr", translationId: "hello" } : undefined,
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
独自検出の言語は frontmatter より弱く、ファイル名・ディレクトリより強く扱われます。`translationId` は明示的な翻訳グループ ID です。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getLocalization, getLocalizedContent, type L10nContent, type L10nDetectorContext, type L10nDetectorResult, type L10nOptions, type L10nUiOptions, type LanguageSwitcherRenderContext, type LocalizedContent, l10n, l10nPlugin, renderLanguageSwitcher, } from "./src/l10n.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
// src/l10n.ts
|
|
2
|
+
import {
|
|
3
|
+
appendContentBodySlot,
|
|
4
|
+
createStyleAsset,
|
|
5
|
+
definePlugin,
|
|
6
|
+
escapeHtml,
|
|
7
|
+
escapeHtmlAttribute
|
|
8
|
+
} from "@riebeckite/core";
|
|
9
|
+
import { parse } from "yaml";
|
|
10
|
+
var LANGUAGE_METADATA_KEY = "l10n.lang";
|
|
11
|
+
var TRANSLATION_METADATA_KEY = "l10n.translationId";
|
|
12
|
+
function l10n(options) {
|
|
13
|
+
const resolved = resolveOptions(options);
|
|
14
|
+
let state = emptyState();
|
|
15
|
+
return definePlugin({
|
|
16
|
+
name: "l10n",
|
|
17
|
+
options,
|
|
18
|
+
assets: [createStyleAsset("l10n")],
|
|
19
|
+
validateOptions: () => validateOptions(options).map((message) => ({ path: "l10n", message })),
|
|
20
|
+
extendContentLocations: ({ entries, locations }) => {
|
|
21
|
+
state = detectContents(entries, resolved);
|
|
22
|
+
throwForStrictDiagnostics(state.diagnostics, resolved.strict);
|
|
23
|
+
for (const content of state.contents.values()) {
|
|
24
|
+
const location = locations.get(content.slug);
|
|
25
|
+
if (!location) continue;
|
|
26
|
+
locations.set(
|
|
27
|
+
content.slug,
|
|
28
|
+
withLocalization(location, content, resolved)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
extendContentGraph: ({ entries }) => {
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
const source = state.contents.get(entry.slug);
|
|
35
|
+
if (!source) continue;
|
|
36
|
+
entry.links = entry.links.map((link) => {
|
|
37
|
+
if (!link.slug) return link;
|
|
38
|
+
const target = state.contents.get(link.slug);
|
|
39
|
+
if (!target) return link;
|
|
40
|
+
const localized = findTranslation(
|
|
41
|
+
state,
|
|
42
|
+
target.translationId,
|
|
43
|
+
source.lang
|
|
44
|
+
);
|
|
45
|
+
return localized ? { ...link, kind: "note", slug: localized.slug } : { ...link, kind: "note" };
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
onManifestCreated: ({ manifest }) => {
|
|
50
|
+
addLocalizationHeadTags(manifest, state);
|
|
51
|
+
addLanguageSwitchers(manifest, resolved.ui);
|
|
52
|
+
},
|
|
53
|
+
addDiagnostics: () => state.diagnostics
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
var l10nPlugin = l10n;
|
|
57
|
+
function getLocalization(manifest, slug) {
|
|
58
|
+
const entry = manifest.bySlug.get(slug);
|
|
59
|
+
if (!entry) return null;
|
|
60
|
+
const lang = entry.publicLocation.metadata?.[LANGUAGE_METADATA_KEY];
|
|
61
|
+
const translationId = entry.publicLocation.metadata?.[TRANSLATION_METADATA_KEY];
|
|
62
|
+
if (!lang || !translationId) return null;
|
|
63
|
+
const candidates = manifest.entries.filter(
|
|
64
|
+
(candidate) => candidate.publicLocation.metadata?.[TRANSLATION_METADATA_KEY] === translationId
|
|
65
|
+
);
|
|
66
|
+
const translations = Object.fromEntries(
|
|
67
|
+
uniqueLanguageEntries(candidates).map((candidate) => [
|
|
68
|
+
candidate.publicLocation.metadata?.[LANGUAGE_METADATA_KEY] ?? "",
|
|
69
|
+
candidate.permalink
|
|
70
|
+
])
|
|
71
|
+
);
|
|
72
|
+
return {
|
|
73
|
+
slug,
|
|
74
|
+
permalink: entry.permalink,
|
|
75
|
+
lang,
|
|
76
|
+
translationId,
|
|
77
|
+
availableLanguages: Object.keys(translations).sort(),
|
|
78
|
+
translations
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function getLocalizedContent(manifest, slug, lang) {
|
|
82
|
+
const localization = getLocalization(manifest, slug);
|
|
83
|
+
if (!localization) return null;
|
|
84
|
+
const candidates = manifest.entries.filter(
|
|
85
|
+
(entry) => entry.publicLocation.metadata?.[TRANSLATION_METADATA_KEY] === localization.translationId && entry.publicLocation.metadata?.[LANGUAGE_METADATA_KEY] === lang
|
|
86
|
+
);
|
|
87
|
+
return candidates.length === 1 ? candidates[0] ?? null : null;
|
|
88
|
+
}
|
|
89
|
+
function resolveOptions(options) {
|
|
90
|
+
const validation = validateOptions(options);
|
|
91
|
+
if (validation.length > 0) throw new Error(validation.join("\n"));
|
|
92
|
+
const languages = options.languages.map(
|
|
93
|
+
(language) => canonicalLanguage(language)
|
|
94
|
+
);
|
|
95
|
+
return {
|
|
96
|
+
defaultLang: canonicalLanguage(options.defaultLang),
|
|
97
|
+
languages,
|
|
98
|
+
languageByLowercase: new Map(
|
|
99
|
+
languages.map((language) => [language.toLowerCase(), language])
|
|
100
|
+
),
|
|
101
|
+
strict: options.strict ?? false,
|
|
102
|
+
detect: options.detect,
|
|
103
|
+
ui: resolveUiOptions(options.ui)
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function validateOptions(options) {
|
|
107
|
+
if (!options || typeof options !== "object")
|
|
108
|
+
return ["l10n options are required."];
|
|
109
|
+
if (!Array.isArray(options.languages) || options.languages.length === 0) {
|
|
110
|
+
return ["l10n.languages must contain at least one language."];
|
|
111
|
+
}
|
|
112
|
+
const errors = [];
|
|
113
|
+
const languages = [];
|
|
114
|
+
for (const language of options.languages) {
|
|
115
|
+
try {
|
|
116
|
+
languages.push(canonicalLanguage(language));
|
|
117
|
+
} catch (error) {
|
|
118
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const defaultLang = canonicalLanguage(options.defaultLang);
|
|
123
|
+
if (!languages.includes(defaultLang)) {
|
|
124
|
+
errors.push("l10n.defaultLang must be included in l10n.languages.");
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
128
|
+
}
|
|
129
|
+
if (new Set(languages).size !== languages.length) {
|
|
130
|
+
errors.push("l10n.languages must not contain duplicate language tags.");
|
|
131
|
+
}
|
|
132
|
+
if (options.strict !== void 0 && typeof options.strict !== "boolean") {
|
|
133
|
+
errors.push("l10n.strict must be a boolean.");
|
|
134
|
+
}
|
|
135
|
+
if (options.detect !== void 0 && typeof options.detect !== "function") {
|
|
136
|
+
errors.push("l10n.detect must be a function.");
|
|
137
|
+
}
|
|
138
|
+
if (options.ui !== void 0 && options.ui !== false) {
|
|
139
|
+
if (typeof options.ui !== "object") {
|
|
140
|
+
errors.push("l10n.ui must be false or an object.");
|
|
141
|
+
} else if (options.ui.slot !== void 0 && (typeof options.ui.slot !== "string" || !options.ui.slot.trim())) {
|
|
142
|
+
errors.push("l10n.ui.slot must be a non-empty string.");
|
|
143
|
+
} else if (options.ui.render !== void 0 && typeof options.ui.render !== "function") {
|
|
144
|
+
errors.push("l10n.ui.render must be a function.");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return errors;
|
|
148
|
+
}
|
|
149
|
+
function resolveUiOptions(ui) {
|
|
150
|
+
if (ui === false) return false;
|
|
151
|
+
return {
|
|
152
|
+
slot: ui?.slot ?? "article.after-meta",
|
|
153
|
+
render: ui?.render ?? renderLanguageSwitcher
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function canonicalLanguage(value) {
|
|
157
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
158
|
+
throw new Error("A l10n language tag must be a non-empty string.");
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
return Intl.getCanonicalLocales(value.trim())[0] ?? value.trim();
|
|
162
|
+
} catch {
|
|
163
|
+
throw new Error(`Invalid l10n language tag: ${value}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function detectContents(entries, options) {
|
|
167
|
+
const contents = /* @__PURE__ */ new Map();
|
|
168
|
+
const diagnostics = [];
|
|
169
|
+
const owners = /* @__PURE__ */ new Map();
|
|
170
|
+
const conflictedGroups = /* @__PURE__ */ new Set();
|
|
171
|
+
for (const entry of entries) {
|
|
172
|
+
const frontmatter = parseFrontmatter(entry);
|
|
173
|
+
const filenameLanguage = detectFilenameLanguage(
|
|
174
|
+
entry.path,
|
|
175
|
+
options.languageByLowercase
|
|
176
|
+
);
|
|
177
|
+
const directoryLanguage = detectDirectoryLanguage(
|
|
178
|
+
entry.path,
|
|
179
|
+
options.languageByLowercase
|
|
180
|
+
);
|
|
181
|
+
const detectorContext = {
|
|
182
|
+
path: entry.path,
|
|
183
|
+
slug: entry.slug,
|
|
184
|
+
frontmatter,
|
|
185
|
+
filenameLanguage,
|
|
186
|
+
directoryLanguage
|
|
187
|
+
};
|
|
188
|
+
const custom = options.detect?.(detectorContext);
|
|
189
|
+
const frontmatterLanguage = configuredLanguage(
|
|
190
|
+
frontmatter.lang,
|
|
191
|
+
options.languageByLowercase
|
|
192
|
+
);
|
|
193
|
+
const signals = compactSignals({
|
|
194
|
+
custom: configuredLanguage(custom?.lang, options.languageByLowercase),
|
|
195
|
+
frontmatter: frontmatterLanguage,
|
|
196
|
+
filename: filenameLanguage,
|
|
197
|
+
directory: directoryLanguage
|
|
198
|
+
});
|
|
199
|
+
addUnknownLanguageDiagnostic(
|
|
200
|
+
diagnostics,
|
|
201
|
+
entry,
|
|
202
|
+
frontmatter.lang,
|
|
203
|
+
"frontmatter",
|
|
204
|
+
options
|
|
205
|
+
);
|
|
206
|
+
addUnknownLanguageDiagnostic(
|
|
207
|
+
diagnostics,
|
|
208
|
+
entry,
|
|
209
|
+
custom?.lang,
|
|
210
|
+
"custom detector",
|
|
211
|
+
options
|
|
212
|
+
);
|
|
213
|
+
if (new Set(Object.values(signals)).size > 1) {
|
|
214
|
+
diagnostics.push(conflictDiagnostic(entry, signals, options.strict));
|
|
215
|
+
}
|
|
216
|
+
const lang = signals.frontmatter ?? signals.custom ?? signals.filename ?? signals.directory ?? options.defaultLang;
|
|
217
|
+
const translationId = readTranslationId(custom?.translationId ?? frontmatter.translation) ?? deriveTranslationId(entry.path, filenameLanguage, directoryLanguage);
|
|
218
|
+
const content = {
|
|
219
|
+
path: entry.path,
|
|
220
|
+
slug: entry.slug,
|
|
221
|
+
frontmatter,
|
|
222
|
+
lang,
|
|
223
|
+
translationId,
|
|
224
|
+
signals
|
|
225
|
+
};
|
|
226
|
+
contents.set(content.slug, content);
|
|
227
|
+
const key = `${translationId}\0${lang}`;
|
|
228
|
+
const previous = owners.get(key);
|
|
229
|
+
if (previous) {
|
|
230
|
+
conflictedGroups.add(key);
|
|
231
|
+
diagnostics.push(duplicateDiagnostic(previous, content, options.strict));
|
|
232
|
+
} else {
|
|
233
|
+
owners.set(key, content);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return { contents, diagnostics, conflictedGroups };
|
|
237
|
+
}
|
|
238
|
+
function parseFrontmatter(entry) {
|
|
239
|
+
const match = entry.markdown.match(
|
|
240
|
+
/^(?:\uFEFF)?---\s*\r?\n([\s\S]*?)\r?\n---\s*(?:\r?\n|$)/
|
|
241
|
+
);
|
|
242
|
+
if (!match) return {};
|
|
243
|
+
const value = parse(match[1] ?? "");
|
|
244
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
245
|
+
throw new Error(`Invalid frontmatter object: ${entry.path}`);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
function detectFilenameLanguage(path, languages) {
|
|
250
|
+
const filename = path.split("/").at(-1)?.replace(/\.md$/i, "") ?? "";
|
|
251
|
+
for (const [lowercase, language] of languages) {
|
|
252
|
+
if (filename.toLowerCase().endsWith(`.${lowercase}`) || filename.toLowerCase().endsWith(`-${lowercase}`) || filename.toLowerCase().endsWith(`_${lowercase}`))
|
|
253
|
+
return language;
|
|
254
|
+
}
|
|
255
|
+
return void 0;
|
|
256
|
+
}
|
|
257
|
+
function detectDirectoryLanguage(path, languages) {
|
|
258
|
+
const first = path.split("/")[0]?.toLowerCase();
|
|
259
|
+
return first ? languages.get(first) : void 0;
|
|
260
|
+
}
|
|
261
|
+
function configuredLanguage(value, languages) {
|
|
262
|
+
if (typeof value !== "string" || !value.trim()) return void 0;
|
|
263
|
+
try {
|
|
264
|
+
return languages.get(canonicalLanguage(value).toLowerCase());
|
|
265
|
+
} catch {
|
|
266
|
+
return void 0;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function deriveTranslationId(path, filenameLanguage, directoryLanguage) {
|
|
270
|
+
const parts = path.replace(/\.md$/i, "").split("/");
|
|
271
|
+
if (directoryLanguage) parts.shift();
|
|
272
|
+
const last = parts.pop() ?? "";
|
|
273
|
+
const language = filenameLanguage?.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
274
|
+
parts.push(
|
|
275
|
+
language ? last.replace(new RegExp(`[._-]${language}$`, "i"), "") : last
|
|
276
|
+
);
|
|
277
|
+
return parts.join("/");
|
|
278
|
+
}
|
|
279
|
+
function readTranslationId(value) {
|
|
280
|
+
if (value === void 0 || value === null) return null;
|
|
281
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
282
|
+
throw new Error("l10n translation frontmatter must be a non-empty string.");
|
|
283
|
+
}
|
|
284
|
+
return value.trim();
|
|
285
|
+
}
|
|
286
|
+
function withLocalization(location, content, options) {
|
|
287
|
+
const unlocalizedPermalink = removeLocalePathSegments(
|
|
288
|
+
location.permalink,
|
|
289
|
+
content
|
|
290
|
+
);
|
|
291
|
+
const permalink = content.lang === options.defaultLang ? unlocalizedPermalink : prefixLanguage(unlocalizedPermalink, content.lang);
|
|
292
|
+
return {
|
|
293
|
+
...location,
|
|
294
|
+
permalink,
|
|
295
|
+
metadata: {
|
|
296
|
+
...location.metadata,
|
|
297
|
+
[LANGUAGE_METADATA_KEY]: content.lang,
|
|
298
|
+
[TRANSLATION_METADATA_KEY]: content.translationId
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function removeLocalePathSegments(permalink, content) {
|
|
303
|
+
let result = permalink;
|
|
304
|
+
const filenameLanguage = content.signals.filename;
|
|
305
|
+
if (filenameLanguage) {
|
|
306
|
+
const suffix = new RegExp(`([._-])${escapeRegExp(filenameLanguage)}$`, "i");
|
|
307
|
+
result = result.replace(suffix, "");
|
|
308
|
+
}
|
|
309
|
+
const directoryLanguage = content.signals.directory;
|
|
310
|
+
if (directoryLanguage) {
|
|
311
|
+
result = result.replace(
|
|
312
|
+
new RegExp(`^/${escapeRegExp(directoryLanguage)}(?=/|$)`, "i"),
|
|
313
|
+
""
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
return result || "/";
|
|
317
|
+
}
|
|
318
|
+
function prefixLanguage(permalink, language) {
|
|
319
|
+
const suffix = permalink === "/" ? "" : permalink;
|
|
320
|
+
return `/${language}${suffix}`;
|
|
321
|
+
}
|
|
322
|
+
function escapeRegExp(value) {
|
|
323
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
324
|
+
}
|
|
325
|
+
function findTranslation(state, translationId, lang) {
|
|
326
|
+
const key = `${translationId}\0${lang}`;
|
|
327
|
+
if (state.conflictedGroups.has(key)) return null;
|
|
328
|
+
return [...state.contents.values()].find(
|
|
329
|
+
(content) => content.translationId === translationId && content.lang === lang
|
|
330
|
+
) ?? null;
|
|
331
|
+
}
|
|
332
|
+
function addLocalizationHeadTags(manifest, state) {
|
|
333
|
+
for (const entry of manifest.entries) {
|
|
334
|
+
const content = state.contents.get(entry.slug);
|
|
335
|
+
if (!content) continue;
|
|
336
|
+
const translations = uniqueLanguageEntries(
|
|
337
|
+
manifest.entries.filter(
|
|
338
|
+
(candidate) => candidate.publicLocation.metadata?.[TRANSLATION_METADATA_KEY] === content.translationId
|
|
339
|
+
)
|
|
340
|
+
);
|
|
341
|
+
entry.headTags = [
|
|
342
|
+
...entry.headTags ?? [],
|
|
343
|
+
...translations.map((translation) => ({
|
|
344
|
+
tag: "link",
|
|
345
|
+
attrs: {
|
|
346
|
+
rel: "alternate",
|
|
347
|
+
hreflang: translation.publicLocation.metadata?.[LANGUAGE_METADATA_KEY] ?? "",
|
|
348
|
+
href: translation.permalink
|
|
349
|
+
}
|
|
350
|
+
}))
|
|
351
|
+
];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function addLanguageSwitchers(manifest, ui) {
|
|
355
|
+
if (ui === false) return;
|
|
356
|
+
for (const entry of manifest.entries) {
|
|
357
|
+
const localization = getLocalization(manifest, entry.slug);
|
|
358
|
+
if (!localization) continue;
|
|
359
|
+
const html = ui.render({ localization });
|
|
360
|
+
if (html) appendContentBodySlot(entry, ui.slot, html);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function renderLanguageSwitcher({
|
|
364
|
+
localization
|
|
365
|
+
}) {
|
|
366
|
+
if (localization.availableLanguages.length < 2) return null;
|
|
367
|
+
const links = localization.availableLanguages.map((lang) => {
|
|
368
|
+
const href = localization.translations[lang];
|
|
369
|
+
if (!href) return "";
|
|
370
|
+
const current = lang === localization.lang ? ' aria-current="page"' : "";
|
|
371
|
+
const escaped = escapeHtml(lang);
|
|
372
|
+
return `<li><a class="l10n-switcher__link" href="${escapeHtmlAttribute(href)}" hreflang="${escapeHtmlAttribute(lang)}" lang="${escapeHtmlAttribute(lang)}"${current}>${escaped}</a></li>`;
|
|
373
|
+
}).join("");
|
|
374
|
+
if (!links) return null;
|
|
375
|
+
return `<nav class="l10n-switcher" aria-label="Language"><span class="l10n-switcher__label">Language</span><ul class="l10n-switcher__list">${links}</ul></nav>`;
|
|
376
|
+
}
|
|
377
|
+
function uniqueLanguageEntries(entries) {
|
|
378
|
+
return entries.filter((entry) => {
|
|
379
|
+
const lang = entry.publicLocation.metadata?.[LANGUAGE_METADATA_KEY];
|
|
380
|
+
return lang !== void 0 && entries.filter(
|
|
381
|
+
(other) => other.publicLocation.metadata?.[LANGUAGE_METADATA_KEY] === lang
|
|
382
|
+
).length === 1;
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
function compactSignals(signals) {
|
|
386
|
+
return Object.fromEntries(
|
|
387
|
+
Object.entries(signals).filter(
|
|
388
|
+
(entry) => entry[1] !== void 0
|
|
389
|
+
)
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
function conflictDiagnostic(entry, signals, strict) {
|
|
393
|
+
return {
|
|
394
|
+
code: "L10N_LANGUAGE_CONFLICT",
|
|
395
|
+
severity: strict ? "error" : "warning",
|
|
396
|
+
filePath: entry.path,
|
|
397
|
+
slug: entry.slug,
|
|
398
|
+
message: `Conflicting localization metadata (${Object.entries(signals).map(([source, language]) => `${source}: ${language}`).join(
|
|
399
|
+
", "
|
|
400
|
+
)}); resolved to ${signals.frontmatter ?? signals.custom ?? signals.filename ?? signals.directory}.`,
|
|
401
|
+
meta: {
|
|
402
|
+
detected: signals,
|
|
403
|
+
resolved: signals.frontmatter ?? signals.custom ?? signals.filename ?? signals.directory
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function duplicateDiagnostic(first, second, strict) {
|
|
408
|
+
return {
|
|
409
|
+
code: "L10N_DUPLICATE_TRANSLATION",
|
|
410
|
+
severity: strict ? "error" : "warning",
|
|
411
|
+
filePath: second.path,
|
|
412
|
+
slug: second.slug,
|
|
413
|
+
message: `Both ${first.path} and ${second.path} claim translation "${second.translationId}" for ${second.lang}.`,
|
|
414
|
+
meta: {
|
|
415
|
+
translationId: second.translationId,
|
|
416
|
+
lang: second.lang,
|
|
417
|
+
first: first.path,
|
|
418
|
+
second: second.path
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function addUnknownLanguageDiagnostic(diagnostics, entry, value, source, options) {
|
|
423
|
+
if (value === void 0 || value === null || value === "" || configuredLanguage(value, options.languageByLowercase))
|
|
424
|
+
return;
|
|
425
|
+
diagnostics.push({
|
|
426
|
+
code: "L10N_UNKNOWN_LANGUAGE",
|
|
427
|
+
severity: options.strict ? "error" : "warning",
|
|
428
|
+
filePath: entry.path,
|
|
429
|
+
slug: entry.slug,
|
|
430
|
+
message: `${source} language "${String(value)}" is not configured.`
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
function throwForStrictDiagnostics(diagnostics, strict) {
|
|
434
|
+
if (!strict || !diagnostics.some((diagnostic) => diagnostic.severity === "error"))
|
|
435
|
+
return;
|
|
436
|
+
throw new Error(
|
|
437
|
+
diagnostics.filter((diagnostic) => diagnostic.severity === "error").map((diagnostic) => `${diagnostic.code}: ${diagnostic.message}`).join("\n")
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
function emptyState() {
|
|
441
|
+
return { contents: /* @__PURE__ */ new Map(), diagnostics: [], conflictedGroups: /* @__PURE__ */ new Set() };
|
|
442
|
+
}
|
|
443
|
+
export {
|
|
444
|
+
getLocalization,
|
|
445
|
+
getLocalizedContent,
|
|
446
|
+
l10n,
|
|
447
|
+
l10nPlugin,
|
|
448
|
+
renderLanguageSwitcher
|
|
449
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type ContentBodySlot, type ContentManifest, type ContentManifestEntry, type PostFrontmatter } from "@riebeckite/core";
|
|
2
|
+
export type L10nContent = {
|
|
3
|
+
readonly path: string;
|
|
4
|
+
readonly slug: string;
|
|
5
|
+
readonly frontmatter: PostFrontmatter;
|
|
6
|
+
};
|
|
7
|
+
export type L10nDetectorContext = L10nContent & {
|
|
8
|
+
readonly filenameLanguage: string | undefined;
|
|
9
|
+
readonly directoryLanguage: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
/** A detector may provide a language and/or an explicit translation identity. */
|
|
12
|
+
export type L10nDetectorResult = {
|
|
13
|
+
readonly lang?: string;
|
|
14
|
+
readonly translationId?: string;
|
|
15
|
+
};
|
|
16
|
+
export type L10nOptions = {
|
|
17
|
+
defaultLang: string;
|
|
18
|
+
languages: readonly string[];
|
|
19
|
+
strict?: boolean;
|
|
20
|
+
detect?: (content: L10nDetectorContext) => L10nDetectorResult | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Publishes the built-in switcher into a standard article layout slot.
|
|
23
|
+
* Pass `false` to keep localization metadata and URLs without UI.
|
|
24
|
+
*/
|
|
25
|
+
ui?: L10nUiOptions;
|
|
26
|
+
};
|
|
27
|
+
export type L10nUiOptions = false | {
|
|
28
|
+
/** Defaults to the standard `article.after-meta` slot. */
|
|
29
|
+
slot?: ContentBodySlot;
|
|
30
|
+
/** Replaces the built-in server-rendered LanguageSwitcher. */
|
|
31
|
+
render?: (context: LanguageSwitcherRenderContext) => string | null;
|
|
32
|
+
};
|
|
33
|
+
export type LocalizedContent = {
|
|
34
|
+
readonly slug: string;
|
|
35
|
+
readonly permalink: string;
|
|
36
|
+
readonly lang: string;
|
|
37
|
+
readonly translationId: string;
|
|
38
|
+
readonly availableLanguages: readonly string[];
|
|
39
|
+
readonly translations: Readonly<Record<string, string>>;
|
|
40
|
+
};
|
|
41
|
+
export type LanguageSwitcherRenderContext = {
|
|
42
|
+
readonly localization: LocalizedContent;
|
|
43
|
+
};
|
|
44
|
+
export declare function l10n(options: L10nOptions): import("@riebeckite/core").RiebeckitePlugin<L10nOptions>;
|
|
45
|
+
export declare const l10nPlugin: typeof l10n;
|
|
46
|
+
/** Returns one entry's language and available real translations. */
|
|
47
|
+
export declare function getLocalization(manifest: ContentManifest, slug: string): LocalizedContent | null;
|
|
48
|
+
/** Resolves an existing translation; missing translations return `null`. */
|
|
49
|
+
export declare function getLocalizedContent(manifest: ContentManifest, slug: string, lang: string): ContentManifestEntry | null;
|
|
50
|
+
/** Framework-neutral default renderer for the standard LanguageSwitcher. */
|
|
51
|
+
export declare function renderLanguageSwitcher({ localization, }: LanguageSwitcherRenderContext): string | null;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@riebeckite/plugin-l10n",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"source": "./index.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./style.css": "./style.css"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"yaml": "^2.9.0",
|
|
19
|
+
"@riebeckite/core": "0.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"tsx": "^4.22.4"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"README.md",
|
|
30
|
+
"README_ja.md",
|
|
31
|
+
"dist",
|
|
32
|
+
"style.css"
|
|
33
|
+
],
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
37
|
+
"directory": "packages/plugins/l10n"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
42
|
+
},
|
|
43
|
+
"description": "Content localization, localized URLs, and translation metadata for Riebeckite.",
|
|
44
|
+
"keywords": [
|
|
45
|
+
"riebeckite",
|
|
46
|
+
"plugin",
|
|
47
|
+
"localization",
|
|
48
|
+
"i18n",
|
|
49
|
+
"l10n"
|
|
50
|
+
],
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
53
|
+
},
|
|
54
|
+
"sideEffects": [
|
|
55
|
+
"./style.css"
|
|
56
|
+
],
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "node ../../../scripts/build_package.mjs",
|
|
59
|
+
"test": "node --import tsx --test \"test/*.test.ts\""
|
|
60
|
+
}
|
|
61
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
.l10n-switcher {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-wrap: wrap;
|
|
4
|
+
align-items: baseline;
|
|
5
|
+
gap: 0.5rem 1rem;
|
|
6
|
+
margin: 0.75rem 0 1.25rem;
|
|
7
|
+
font-size: 0.8125rem;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.l10n-switcher__label {
|
|
11
|
+
font-size: 0.6875rem;
|
|
12
|
+
font-weight: 700;
|
|
13
|
+
letter-spacing: 0.12em;
|
|
14
|
+
color: var(
|
|
15
|
+
--rb-color-muted,
|
|
16
|
+
color-mix(in srgb, currentColor 48%, transparent)
|
|
17
|
+
);
|
|
18
|
+
text-transform: uppercase;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.l10n-switcher__list {
|
|
22
|
+
display: flex;
|
|
23
|
+
flex-wrap: wrap;
|
|
24
|
+
gap: 0.75rem;
|
|
25
|
+
margin: 0;
|
|
26
|
+
padding: 0;
|
|
27
|
+
list-style: none;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.l10n-switcher__link {
|
|
31
|
+
color: inherit;
|
|
32
|
+
text-decoration: none;
|
|
33
|
+
text-underline-offset: 0.2em;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.l10n-switcher__link:hover,
|
|
37
|
+
.l10n-switcher__link:focus-visible {
|
|
38
|
+
color: var(--rb-color-accent, currentColor);
|
|
39
|
+
text-decoration: underline;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.l10n-switcher__link[aria-current="page"] {
|
|
43
|
+
font-weight: 700;
|
|
44
|
+
text-decoration: underline;
|
|
45
|
+
}
|