Skip to content

Commit 0c45b2f

Browse files
committed
feat: prepare for l10n #4
Add a build step to generate `messages.json` from `locales.toml`. Also, change command ids from kebab-case to snake_case. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/i18n/Locale-Specific_Message_reference
1 parent 3731469 commit 0c45b2f

10 files changed

Lines changed: 137 additions & 17 deletions

File tree

‎ext/_locales/en/messages.json‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎locales.toml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file contains translations for various messages Ghostbird shows.
2+
# The format is as follows:
3+
#
4+
# [<message_id>]
5+
# <language> = "<translated message>"
6+
#
7+
# See https://pontoon.mozilla.org/projects/thunderbirdnet/ for canonical terms like "email".
8+
9+
[manifest_name]
10+
# Full name of the extension.
11+
en = "Ghostbird - GhostText for Thunderbird"
12+
ja = "Ghostbird - Thunderbird用GhostText"
13+
14+
[manifest_description]
15+
# Description of the extension.
16+
en = "Use your favorite text editor to compose emails"
17+
ja = "お好みのテキストエディタでメールを編集"
18+
19+
[manifest_commands_start_ghostbird_description]
20+
# Description of the shortcut key to start Ghostbird.
21+
en = "Start syncing with the text editor"
22+
ja = "テキストエディタとの同期を開始"
23+
24+
[manifest_commands_stop_ghostbird_description]
25+
# Description of the shortcut key to stop Ghostbird.
26+
en = "End syncing with the text editor"
27+
ja = "テキストエディタとの同期を停止"

‎manifest_template.json‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
{
22
"manifest_version": 3,
3-
"name": "Ghostbird - GhostText for Thunderbird",
3+
"name": "__MSG_manifest_name__",
44
"short_name": "Ghostbird",
5-
"description": "Use your favorite text editor to compose emails in Thunderbird",
5+
"description": "__MSG_manifest_description__",
66
"version": "$version$",
77
"homepage_url": "$homepage_url$",
8+
"version_name": "$version_name$",
89
"default_locale": "en",
910
"author": "snipsnipsnip",
11+
"browser_specific_settings": {
12+
"gecko": {
13+
"id": "@ghostbird.exteditor.github.io",
14+
"strict_min_version": "128.0"
15+
}
16+
},
1017
"permissions": [
1118
"alarms",
1219
"storage",
@@ -23,21 +30,15 @@
2330
"icons": {
2431
"96": "blue.svg"
2532
},
26-
"browser_specific_settings": {
27-
"gecko": {
28-
"id": "@ghostbird.exteditor.github.io",
29-
"strict_min_version": "128.0"
30-
}
31-
},
3233
"commands": {
33-
"start-ghostbird": {
34-
"description": "Start Ghostbird and connect to the GhostText server",
34+
"start_ghostbird": {
35+
"description": "__MSG_manifest_commands_start_ghostbird_description__",
3536
"suggested_key": {
3637
"default": "Ctrl+E"
3738
}
3839
},
39-
"stop-ghostbird": {
40-
"description": "Disconnect from the GhostText server and Stop Ghostbird. (Set the same shortcut as 'Start Ghostbird' to toggle)",
40+
"stop_ghostbird": {
41+
"description": "__MSG_manifest_commands_stop_ghostbird_description__",
4142
"suggested_key": {
4243
"default": "Ctrl+E"
4344
}

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"devDependencies": {
5959
"@biomejs/biome": "2.2.0",
6060
"@std/text": "jsr:^1.0.16",
61+
"@std/toml": "jsr:^1.0.10",
6162
"@types/node": "^24.3.0",
6263
"@types/thunderbird-webext-browser": "^127.0.0",
6364
"barrelsby": "^2.8.1",

‎src/app-background/api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { IComposeWindow } from "src/ghosttext-adaptor/api"
33
export type { IGhostServerPort } from "src/ghosttext-adaptor/api"
44

55
/** IDs of shortcut keys. Must be in sync with ones in manifest.json */
6-
export type CommandId = "start-ghostbird" | "stop-ghostbird"
6+
export type CommandId = "start_ghostbird" | "stop_ghostbird"
77

88
/** Queries current shortcut key config */
99
export interface ICommandConfig {

‎src/app-background/background_event_router.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { IComposeWindow } from "src/ghosttext-adaptor/api"
12
import type { CommandId, ICommandConfig, IComposeWindowDetector, ITab } from "./api"
23
import type { ComposeActionNotifier } from "./compose_action_notifier"
34

@@ -24,10 +25,14 @@ export class BackgroundEventRouter {
2425
return this.composeActionNotifier.toggle(composeTab)
2526
}
2627

28+
return this.runCommand(command, composeTab)
29+
}
30+
31+
private runCommand(command: string, composeTab: IComposeWindow): Promise<void> {
2732
switch (command as CommandId) {
28-
case "start-ghostbird":
33+
case "start_ghostbird":
2934
return this.composeActionNotifier.start(composeTab)
30-
case "stop-ghostbird":
35+
case "stop_ghostbird":
3136
return this.composeActionNotifier.stop(composeTab)
3237
}
3338
// We don't handle default here so that tsc checks for exhaustiveness

‎src/app-background/compose_action_notifier.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ComposeActionNotifier {
2828
}
2929
}
3030

31-
stop(tab: IComposeWindow): void {
31+
async stop(tab: IComposeWindow): Promise<void> {
3232
this.close(tab, this.runners.get(tab.tabId))
3333
}
3434

‎tools/generate_locale_messages.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { readFile } from "node:fs/promises"
2+
import { parse } from "@std/toml"
3+
import type { EmittedAsset, Plugin } from "rolldown"
4+
5+
type Options = { path: string }
6+
7+
/** Generates _locales/<lang>/messages.json from the template file */
8+
export const generateLocaleMessages = ({ path }: Options): Plugin => ({
9+
name: "generate_locale_messages",
10+
async generateBundle(): Promise<void> {
11+
let tomlId = await this.resolve(path)
12+
if (!tomlId) {
13+
this.warn(`${path} doesn't exist; skipping generation`)
14+
return
15+
}
16+
17+
let locales = await loadLocaleToml(tomlId.id)
18+
let messagesByLocales = localeToMessages(locales)
19+
20+
for (let [locale, messages] of messagesByLocales) {
21+
let dir = locale.replaceAll("-", "_")
22+
this.emitFile({
23+
type: "asset",
24+
fileName: `_locales/${dir}/messages.json`,
25+
originalFileName: tomlId.id,
26+
source: JSON.stringify(messages),
27+
} satisfies EmittedAsset)
28+
}
29+
},
30+
})
31+
32+
type LanguageId = string
33+
type MessageId = string
34+
type MessageLabel = string
35+
type MessagesJsonContent = Record<MessageId, { message: MessageLabel }>
36+
type LocaleTomlContent = Record<MessageId, Record<LanguageId, MessageLabel | { message: MessageLabel }>>
37+
38+
/**
39+
* Converts a set of translations into WebExtension's locale messages.json format.
40+
*/
41+
function localeToMessages(toml: LocaleTomlContent): Map<LanguageId, MessagesJsonContent> {
42+
let langs = new Map<LanguageId, MessagesJsonContent>()
43+
for (const [messageId, translations] of Object.entries(toml)) {
44+
for (const [lang, msg] of Object.entries(translations)) {
45+
let content = langs.get(lang) ?? {}
46+
content[messageId] = typeof msg === "string" ? { message: msg } : msg
47+
langs.set(lang, content)
48+
}
49+
}
50+
return langs
51+
}
52+
53+
/** Loads a TOML file that contains translations for various messages Ghostbird shows. */
54+
async function loadLocaleToml(tomlPath: string): Promise<LocaleTomlContent> {
55+
let tomlText = await readFile(tomlPath, { encoding: "utf8" })
56+
return parse(tomlText) as LocaleTomlContent
57+
}

‎tools/tsdown_config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* - Generating index.ts (./barrelsby)
55
* - Type checking with tsc (./typecheck_with_tsc)
66
* - Generating manifest.json (./generate_manifest)
7+
* - Generating messages.json in _locales (./generate_locale_messages)
78
* - Bundling and minifying
89
* - Copying non-js files in ext/
910
*
@@ -22,6 +23,7 @@ import { dirname, join, resolve } from "node:path"
2223
import { env } from "node:process"
2324
import type { Options, UserConfig } from "tsdown"
2425
import { barrelsby } from "./barrelsby"
26+
import { generateLocaleMessages } from "./generate_locale_messages"
2527
import { generateManifest } from "./generate_manifest"
2628
import { typecheckWithTsc } from "./typecheck_with_tsc"
2729

@@ -78,6 +80,8 @@ const esmConfig = {
7880
typecheckWithTsc(),
7981
// Generate manifest.json
8082
generateManifest({ env: isRelease ? (env as Record<string, string>) : {} }),
83+
// Generate _locales/*/messages.json
84+
generateLocaleMessages({ path: "./locales.toml" }),
8185
],
8286
} satisfies Options
8387

‎yarn.lock‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ __metadata:
510510
dependencies:
511511
"@biomejs/biome": "npm:2.2.0"
512512
"@std/text": "jsr:^1.0.16"
513+
"@std/toml": "jsr:^1.0.10"
513514
"@types/node": "npm:^24.3.0"
514515
"@types/thunderbird-webext-browser": "npm:^127.0.0"
515516
barrelsby: "npm:^2.8.1"
@@ -641,6 +642,13 @@ __metadata:
641642
languageName: node
642643
linkType: hard
643644

645+
"@jsr/std__collections@npm:^1.1.3":
646+
version: 1.1.3
647+
resolution: "@jsr/std__collections@npm:1.1.3::__archiveUrl=https%3A%2F%2Fnpm.jsr.io%2F~%2F11%2F%40jsr%2Fstd__collections%2F1.1.3.tgz"
648+
checksum: 10c0/38053fc2082b6a644efee8594c7c6bf46a7265aacce075ba31759ece817623c6b325f2663c72cf3232a159a3f6d5efa6adcefaa04159422f719bc8089c0e8160
649+
languageName: node
650+
linkType: hard
651+
644652
"@jsr/std__regexp@npm:^1.0.1":
645653
version: 1.0.1
646654
resolution: "@jsr/std__regexp@npm:1.0.1::__archiveUrl=https%3A%2F%2Fnpm.jsr.io%2F~%2F11%2F%40jsr%2Fstd__regexp%2F1.0.1.tgz"
@@ -1082,6 +1090,24 @@ __metadata:
10821090
languageName: node
10831091
linkType: hard
10841092

1093+
"@std/toml@jsr:^1.0.10":
1094+
version: 1.0.10
1095+
resolution: "@std/toml@jsr:1.0.10::__archiveUrl=https%3A%2F%2Fnpm.jsr.io%2F~%2F11%2F%40jsr%2Fstd__toml%2F1.0.10.tgz"
1096+
dependencies:
1097+
"@jsr/std__collections": "npm:^1.1.3"
1098+
checksum: 10c0/81f81e1ea12cbe40e0235ab6754d07f042834d9d681db9ec48d13e4c2fe50051fe457c5814ee0ce27bed3d95388f479d7b585a2d6216d505401c114cfccff216
1099+
languageName: node
1100+
linkType: hard
1101+
1102+
"@std/toml@npm:@jsr/std__toml@^1.0.10":
1103+
version: 1.0.10
1104+
resolution: "@jsr/std__toml@npm:1.0.10::__archiveUrl=https%3A%2F%2Fnpm.jsr.io%2F~%2F11%2F%40jsr%2Fstd__toml%2F1.0.10.tgz"
1105+
dependencies:
1106+
"@jsr/std__collections": "npm:^1.1.3"
1107+
checksum: 10c0/81f81e1ea12cbe40e0235ab6754d07f042834d9d681db9ec48d13e4c2fe50051fe457c5814ee0ce27bed3d95388f479d7b585a2d6216d505401c114cfccff216
1108+
languageName: node
1109+
linkType: hard
1110+
10851111
"@tybys/wasm-util@npm:^0.10.1":
10861112
version: 0.10.1
10871113
resolution: "@tybys/wasm-util@npm:0.10.1"

0 commit comments

Comments
 (0)