-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extension): multilang support (#89)
* feat: validate locales script * feat: popup translation * feat: options translation
- Loading branch information
Showing
23 changed files
with
492 additions
and
1,654 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// @ts-check | ||
|
||
import { readdir, readFile } from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { cwd } from "node:process"; | ||
|
||
const localesPath = path.join(cwd(), "src", "_locales"); | ||
|
||
validate(); | ||
|
||
async function validate() { | ||
/** | ||
* @type {(import("node:fs").Dirent)[]} | ||
*/ | ||
let localesDir; | ||
|
||
try { | ||
localesDir = await readdir(localesPath, { | ||
encoding: "utf8", | ||
recursive: true, | ||
withFileTypes: true, | ||
}); | ||
} catch (error) { | ||
// @ts-expect-error | ||
throw new Error(`Failed to read locales folder "${localesPath}".`, { | ||
cause: error, | ||
}); | ||
} | ||
|
||
/** | ||
* @type {Map<string, Set<string>>} | ||
*/ | ||
const messageKeys = new Map(); | ||
const localeFiles = localesDir.filter((dirEntry) => { | ||
return dirEntry.isFile(); | ||
}); | ||
|
||
for await (const dirEntry of localeFiles) { | ||
const locale = dirEntry.path; | ||
|
||
/** | ||
* @type {string} | ||
*/ | ||
let contents; | ||
|
||
try { | ||
const entryPath = path.join(dirEntry.path, dirEntry.name); | ||
contents = await readFile(entryPath, { encoding: "utf8" }); | ||
} catch (error) { | ||
// @ts-expect-error | ||
throw new Error(`Failed to read locale file "${dirEntry.path}".`, { | ||
cause: error, | ||
}); | ||
} | ||
|
||
/** | ||
* @type {Record<string, never>} | ||
*/ | ||
const messageData = JSON.parse(contents); | ||
|
||
const messages = Object.keys(messageData); | ||
|
||
messageKeys.set(locale, new Set(messages)); | ||
} | ||
|
||
const counts = new Set( | ||
[...messageKeys.values()].map((messages) => messages.size) | ||
); | ||
|
||
if (counts.size !== 1) { | ||
throw new Error("Not all languages are equally supported."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"$id": "locale-messages", | ||
"title": "LocaleMessages", | ||
"type": "object", | ||
"additionalProperties": { | ||
"anyOf": [ | ||
{ | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": ["message"], | ||
"properties": { | ||
"message": { "type": "string" }, | ||
"description": { "type": "string" }, | ||
"placeholders": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": ["content"], | ||
"properties": { | ||
"content": { | ||
"type": "string" | ||
}, | ||
"example": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": ["message"], | ||
"properties": { | ||
"message": { "type": "string" }, | ||
"description": { "type": "string" } | ||
} | ||
} | ||
] | ||
} | ||
} |
Oops, something went wrong.