Skip to content

Commit e9aa482

Browse files
fix(packages/i18n): improve readability of code
1 parent 02eb48e commit e9aa482

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

packages/i18n/src/build.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { mkdir, readFile, writeFile } from 'node:fs/promises';
9-
import { parseYAML, parseJSON5, parseTOML } from 'confbox';
9+
import { parseJSON5, parseTOML, parseYAML } from 'confbox';
1010
import { dirname, extname } from 'node:path';
1111
import { applyChromeMessagePlaceholders, getSubstitutionCount } from './utils';
1212

@@ -118,6 +118,7 @@ export async function parseMessagesFile(
118118
): Promise<ParsedMessage[]> {
119119
const text = await readFile(file, 'utf8');
120120
const ext = extname(file).toLowerCase();
121+
121122
return parseMessagesText(text, EXT_FORMATS_MAP[ext] ?? 'JSON5');
122123
}
123124

@@ -173,13 +174,16 @@ function _parseMessagesObject(
173174
`Messages file should not contain \`${object}\` (found at "${path.join('.')}")`,
174175
);
175176
}
177+
176178
if (Array.isArray(object))
177179
return object.flatMap((item, i) =>
178180
_parseMessagesObject(path.concat(String(i)), item, depth + 1),
179181
);
182+
180183
if (isPluralMessage(object)) {
181184
const message = Object.values(object).join('|');
182185
const substitutions = getSubstitutionCount(message);
186+
183187
return [
184188
{
185189
type: 'plural',
@@ -189,9 +193,11 @@ function _parseMessagesObject(
189193
},
190194
];
191195
}
196+
192197
if (depth === 1 && isChromeMessage(object)) {
193198
const message = applyChromeMessagePlaceholders(object);
194199
const substitutions = getSubstitutionCount(message);
200+
195201
return [
196202
{
197203
type: 'chrome',
@@ -227,7 +233,7 @@ function isChromeMessage(object: any): object is ChromeMessage {
227233

228234
export function generateTypeText(messages: ParsedMessage[]): string {
229235
const renderMessageEntry = (message: ParsedMessage): string => {
230-
// Use . for deep keys at runtime and types
236+
// Use '.' for deep keys at runtime and types
231237
const key = message.key.join('.');
232238

233239
const features = [

packages/i18n/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* @module @wxt-dev/i18n
33
*/
44
import {
5-
I18nStructure,
65
DefaultI18nStructure,
76
I18n,
7+
I18nStructure,
88
Substitution,
99
} from './types';
1010
import { browser } from '@wxt-dev/browser';
@@ -16,6 +16,7 @@ export function createI18n<
1616
// Resolve args
1717
let sub: Substitution[] | undefined;
1818
let count: number | undefined;
19+
1920
args.forEach((arg, i) => {
2021
if (arg == null) {
2122
// ignore nullish args
@@ -37,16 +38,19 @@ export function createI18n<
3738

3839
// Load the localization
3940
let message: string;
41+
4042
if (sub?.length) {
4143
// Convert all substitutions to strings
4244
const stringSubs = sub?.map((sub) => String(sub));
4345
message = browser.i18n.getMessage(key.replaceAll('.', '_'), stringSubs);
4446
} else {
4547
message = browser.i18n.getMessage(key.replaceAll('.', '_'));
4648
}
49+
4750
if (!message) {
4851
console.warn(`[i18n] Message not found: "${key}"`);
4952
}
53+
5054
if (count == null) return message;
5155

5256
// Apply pluralization

packages/i18n/src/module.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import 'wxt';
1414
import { addAlias, defineWxtModule } from 'wxt/modules';
1515
import {
1616
generateChromeMessagesText,
17-
parseMessagesFile,
1817
generateTypeText,
18+
parseMessagesFile,
1919
SUPPORTED_LOCALES,
2020
} from './build';
2121
import glob from 'fast-glob';
@@ -70,6 +70,7 @@ export default defineWxtModule<I18nOptions>({
7070
GeneratedPublicFile[]
7171
> => {
7272
const files = await getLocalizationFiles();
73+
7374
return await Promise.all(
7475
files.map(async ({ file, locale }) => {
7576
const messages = await parseMessagesFile(file);
@@ -86,13 +87,15 @@ export default defineWxtModule<I18nOptions>({
8687
const defaultLocaleFile = files.find(
8788
({ locale }) => locale === wxt.config.manifest.default_locale,
8889
)!;
89-
if (defaultLocaleFile == null) {
90+
91+
if (defaultLocaleFile === null) {
9092
throw Error(
9193
`\`[i18n]\` Required localization file does not exist: \`<localesDir>/${wxt.config.manifest.default_locale}.{json|json5|yml|yaml|toml}\``,
9294
);
9395
}
9496

9597
const messages = await parseMessagesFile(defaultLocaleFile.file);
98+
9699
return {
97100
path: typesPath,
98101
text: generateTypeText(messages),
@@ -152,7 +155,7 @@ export { type GeneratedI18nStructure }
152155

153156
addAlias(wxt, '#i18n', sourcePath);
154157

155-
// Generate separate declaration file containing types - this prevents
158+
// Generate a separate declaration file containing types - this prevents
156159
// firing the dev server's default file watcher when updating the types,
157160
// which would cause a full rebuild and reload of the extension.
158161

packages/i18n/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChromeMessage } from './build';
22

33
export function applyChromeMessagePlaceholders(message: ChromeMessage): string {
4-
if (message.placeholders == null) return message.message;
4+
if (message.placeholders === null) return message.message;
55

66
return Object.entries(message.placeholders ?? {}).reduce(
77
(text, [name, value]) => {
@@ -28,6 +28,7 @@ export function standardizeLocale(locale: string): string {
2828

2929
const [is_match, prefix, suffix] =
3030
locale.match(/^([a-z]{2})[-_]([a-z]{2,3})$/i) ?? [];
31+
3132
if (is_match) {
3233
return `${prefix.toLowerCase()}_${suffix.toUpperCase()}`;
3334
}

0 commit comments

Comments
 (0)