Skip to content

Commit d9e7903

Browse files
authored
Merge pull request #2 from wuespace/improvements
Improve i18n capabilities
2 parents 54355d0 + 5f9542c commit d9e7903

File tree

7 files changed

+146
-10
lines changed

7 files changed

+146
-10
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<babeledit_project be_version="5.5.1" version="1.3">
3+
<!--
4+
5+
BabelEdit project file
6+
https://www.codeandweb.com/babeledit
7+
8+
This file contains meta data for all translations, but not the translation texts itself.
9+
They are stored in framework-specific message files (.json / .vue / .yaml / .properties)
10+
11+
-->
12+
<preset_collections/>
13+
<framework>generic-json</framework>
14+
<filename>babeledit-project.babel</filename>
15+
<source_root_dir>../</source_root_dir>
16+
<folder_node>
17+
<name/>
18+
<children>
19+
<package_node>
20+
<name>main</name>
21+
<children>
22+
<concept_node>
23+
<name>ABC {0} DEF</name>
24+
<description/>
25+
<comment/>
26+
<translations>
27+
<translation>
28+
<language>de-DE</language>
29+
<approved>false</approved>
30+
</translation>
31+
<translation>
32+
<language>en-US</language>
33+
<approved>false</approved>
34+
</translation>
35+
</translations>
36+
</concept_node>
37+
<concept_node>
38+
<name>Hello world!</name>
39+
<description/>
40+
<comment/>
41+
<translations>
42+
<translation>
43+
<language>de-DE</language>
44+
<approved>false</approved>
45+
</translation>
46+
<translation>
47+
<language>en-US</language>
48+
<approved>false</approved>
49+
</translation>
50+
</translations>
51+
</concept_node>
52+
<concept_node>
53+
<name>Welcome to the {{}}}}{{ {0} {1} {{0}} {{1}} homepage {0}!</name>
54+
<description/>
55+
<comment/>
56+
<translations>
57+
<translation>
58+
<language>de-DE</language>
59+
<approved>true</approved>
60+
</translation>
61+
<translation>
62+
<language>en-US</language>
63+
<approved>true</approved>
64+
</translation>
65+
</translations>
66+
</concept_node>
67+
</children>
68+
</package_node>
69+
</children>
70+
</folder_node>
71+
<embedded_source_texts>false</embedded_source_texts>
72+
<isTemplateProject>false</isTemplateProject>
73+
<languages>
74+
<language>
75+
<code>de-DE</code>
76+
</language>
77+
<language>
78+
<code>en-US</code>
79+
</language>
80+
</languages>
81+
<translation_packages>
82+
<translation_package>
83+
<name>main</name>
84+
<translation_urls>
85+
<translation_url>
86+
<path>de.json</path>
87+
<language>de-DE</language>
88+
</translation_url>
89+
<translation_url>
90+
<path>en.json</path>
91+
<language>en-US</language>
92+
</translation_url>
93+
</translation_urls>
94+
</translation_package>
95+
</translation_packages>
96+
<editor_configuration>
97+
<save_empty_translations>true</save_empty_translations>
98+
<translation_order>alphabetically</translation_order>
99+
<copy_templates>
100+
<copy_template>'%1'</copy_template>
101+
<copy_template>t`%1`</copy_template>
102+
<copy_template>lt`%1`</copy_template>
103+
</copy_templates>
104+
<custom_languages/>
105+
<id_extractor_ignores/>
106+
<machine_translation_formality>default</machine_translation_formality>
107+
<machine_translation_context/>
108+
</editor_configuration>
109+
<primary_language>en-US</primary_language>
110+
<configuration>
111+
<indent>tab</indent>
112+
<format>plaintext-json</format>
113+
<support_arrays>true</support_arrays>
114+
</configuration>
115+
</babeledit_project>

example/locales/de.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"Hello world!": "Hallo Welt!",
32
"ABC {0} DEF": "Deutsches ABC {0} DEF",
4-
"Welcome to the {{}}}}{{ {0} {1} {{0}} {{1}} homepage {0}!": ""
3+
"Hello world!": "Hallo Welt!",
4+
"Welcome to the {{}}}}{{ {0} {1} {{0}} {{1}} homepage {0}!": "Willkommen zur {{}}}}{{ {0} {1} {{0}} {{1}} Homepage {0}!"
55
}

example/locales/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"Hello world!": "Hello world!",
32
"ABC {0} DEF": "ABC {0} DEF",
3+
"Hello world!": "Hello world!",
44
"Welcome to the {{}}}}{{ {0} {1} {{0}} {{1}} homepage {0}!": "Welcome to the {{}}}}{{ {0} {1} {{0}} {{1}} homepage {0}!"
55
}

example/main.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ app.get("/", (c) => {
2222
<div>
2323
{t`Welcome to the {{}}}}{{ {0} {1} {{0}} {{1}} homepage ${useRequestContext().req.url}!`}
2424
{t`Hello world!`}
25+
{t`Test: ${<code>{t`Hello!`}</code>}`}
2526
<LocalePrinter />
2627
</div>
2728
)),

lib/hono/LocalyzedStringValue.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { FC } from "@hono/hono/jsx";
12
import type { LazyLocalyzedString } from "./LazyLocalyzedString.ts";
23

34
/**
@@ -29,4 +30,8 @@ import type { LazyLocalyzedString } from "./LazyLocalyzedString.ts";
2930
* };
3031
* ```
3132
*/
32-
export type LocalyzedStringValue = string | number | LazyLocalyzedString;
33+
export type LocalyzedStringValue =
34+
| string
35+
| number
36+
| LazyLocalyzedString
37+
| ReturnType<FC>;

lib/hono/asFC.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ import type { FC } from "@hono/hono/jsx";
3131
*
3232
* @see {@link t}
3333
*/
34-
export function asFC(X: FC): ReturnType<FC> {
34+
export function asFC(X: FC): string | Promise<string> {
3535
return <X />;
3636
}

lib/hono/t.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { raw } from "@hono/hono/html";
2+
import type { HtmlEscapedString } from "@hono/hono/utils/html";
13
import { unescapeKey } from "../common/unescapeKey.ts";
24
import { ensureLazyLocalyzedString } from "./ensureLazyLocalyzedString.ts";
35
import { getLocalizationMap } from "./getLocalizationMap.ts";
@@ -46,12 +48,20 @@ import type { LocalyzedStringValue } from "./LocalyzedStringValue.ts";
4648
export function t(
4749
strings: TemplateStringsArray,
4850
...values: LocalyzedStringValue[]
49-
): string;
50-
export function t(lls: LazyLocalyzedString): string;
51+
): string | HtmlEscapedString;
52+
export function t(lls: LazyLocalyzedString): string | HtmlEscapedString;
53+
export function t(raw: string): string | HtmlEscapedString;
5154
export function t(
52-
string: TemplateStringsArray | LazyLocalyzedString,
55+
string: TemplateStringsArray | LazyLocalyzedString | string,
5356
...values: LocalyzedStringValue[]
54-
): string {
57+
): string | HtmlEscapedString {
58+
if (typeof string === "string") {
59+
// simple string, return as is
60+
string = {
61+
localizationKey: string,
62+
values: [],
63+
};
64+
}
5565
const lls = ensureLazyLocalyzedString(string, values);
5666
const localizationValues = getLocalizationMap();
5767

@@ -60,6 +70,11 @@ export function t(
6070
localizationValues[lls.localizationKey] = lls.localizationKey;
6171
}
6272

73+
if (localizationValues[lls.localizationKey].length === 0) {
74+
// Untranslated string, use key as value
75+
localizationValues[lls.localizationKey] = lls.localizationKey;
76+
}
77+
6378
let result = localizationValues[lls.localizationKey];
6479

6580
for (let i = 0; i < lls.values.length; i++) {
@@ -81,5 +96,5 @@ export function t(
8196
}
8297
}
8398

84-
return unescapeKey(result);
99+
return raw(unescapeKey(result));
85100
}

0 commit comments

Comments
 (0)