Skip to content

Commit

Permalink
feat: Add support for configuring the locales directory (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
breadgrocery authored Oct 23, 2024
1 parent cd7285c commit ab83031
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
15 changes: 15 additions & 0 deletions packages/i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ And you're done! Using WXT, you get type-safety out of the box.
i18n.t('helloWorld'); // "Hello world!";
```

## Configuration

The module can be configured via the `i18n` config:

```ts
export default defineConfig({
modules: ['@wxt-dev/i18n'],
i18n: {
// ...
},
});
```

Options have JSDocs available in your editor, or you can read them in the source code: [`I18nOptions`](https://github.com/wxt-dev/wxt/blob/main/packages/i18n/src/module.ts).

## Messages File Format

> [!DANGER]
Expand Down
34 changes: 27 additions & 7 deletions packages/i18n/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { watch } from 'chokidar';
import { GeneratedPublicFile, WxtDirFileEntry } from 'wxt';
import { writeFile } from 'node:fs/promises';

export default defineWxtModule({
export default defineWxtModule<I18nOptions>({
name: '@wxt-dev/i18n',
configKey: 'i18n',
imports: [{ from: '#i18n', name: 'i18n' }],

setup(wxt) {
setup(wxt, options) {
if (wxt.config.manifest.default_locale == null) {
wxt.logger.warn(
`\`[i18n]\` manifest.default_locale not set, \`@wxt-dev/i18n\` disabled.`,
Expand All @@ -38,9 +38,12 @@ export default defineWxtModule({
'`[i18n]` Default locale: ' + wxt.config.manifest.default_locale,
);

const { localesDir = resolve(wxt.config.srcDir, 'locales') } =
options ?? {};

const getLocalizationFiles = async () => {
const files = await glob('locales/*', {
cwd: wxt.config.srcDir,
const files = await glob('*.{json,json5,yml,yaml,toml}', {
cwd: localesDir,
absolute: true,
});
return files.map((file) => ({
Expand Down Expand Up @@ -71,7 +74,7 @@ export default defineWxtModule({
)!;
if (defaultLocaleFile == null) {
throw Error(
`\`[i18n]\` Required localization file does not exist: \`<srcDir>/locales/${wxt.config.manifest.default_locale}.{json|json5|yml|yaml|toml}\``,
`\`[i18n]\` Required localization file does not exist: \`<localesDir>/${wxt.config.manifest.default_locale}.{json|json5|yml|yaml|toml}\``,
);
}

Expand Down Expand Up @@ -154,9 +157,26 @@ export { type GeneratedI18nStructure }

if (wxt.config.command === 'serve') {
wxt.hooks.hookOnce('build:done', () => {
const watcher = watch(resolve(wxt.config.srcDir, 'locales'));
const watcher = watch(localesDir);
watcher.on('change', updateLocalizations);
});
}
},
});

/**
* Options for the i18n module
*/
export interface I18nOptions {
/**
* Directory containing files that define the translations.
* @default "${config.srcDir}/locales"
*/
localesDir?: string;
}

declare module 'wxt' {
export interface InlineConfig {
i18n?: I18nOptions;
}
}

0 comments on commit ab83031

Please sign in to comment.