@open-xchange/rolldown-plugin-i18next-gettext
v1.5.0
Published
Rolldown integration of i18next using gettext PO files
Maintainers
Keywords
Readme
@open-xchange/rolldown-plugin-i18next-gettext
A rolldown plugin that allows building libraries or applications using i18next in source code with gettext's .po and .pot files under the hood.
This plugin has the following responsibilities:
- All
.pofiles imported in source code will be converted to i18next translation catalogs. - The source code will be scanned for translation strings (
tfunction calls), and.potfiles containing all strings will be generated into the output directory. Supports i18next namespaces (one.potfile per namespace).
Installation
npm install -D @open-xchange/rolldown-plugin-i18next-gettext
# or
pnpm add -D @open-xchange/rolldown-plugin-i18next-gettext
# or
yarn add -D @open-xchange/rolldown-plugin-i18next-gettextConfiguration
Add the plugin to your rolldown configuration (or compatible configurations like Vite or tsdown):
// rolldown.config.ts
import { defineConfig } from 'rolldown'
import i18nextPlugin from '@open-xchange/rolldown-plugin-i18next-gettext'
export default defineConfig(() => {
plugins: [
i18nextPlugin({
poFiles: 'i18n/**/*.po',
srcFiles: 'src/**/*.{js,jsx,ts,tsx}',
potFile: '[NAMESPACE].pot',
projectName: 'My Project',
}),
],
})Options
| Name | Type | Default | Description |
| - | - | - | - |
| poFiles | string\|string[] | **/*.po | Glob pattern(s) for all PO files to be converted when imported in source code. |
| srcFiles | string\|string[] | src/**/*.{js,jsx,ts,tsx} | Glob pattern(s) for all source files to be scanned for UI strings for the POT file generator. Resolved against rolldown's resolved cwd option. When used as Vite plugin without an explicit build.rolldownOptions.cwd option, resolved against Vite's resolved root option instead. |
| potFile | string | [NAMESPACE].pot | Path to and filename of the generated POT files, relative to the build output directory. Must contain the placeholder [NAMESPACE] if the project contains translation strings in different i18next namespaces. |
| projectName | string | required | The project name to be inserted into the POT file under the key 'Project-Id-Version'. May contain the placeholder [NAMESPACE] |
Usage
Register TypeScript type definitions for the PO file imports:
// tsconfig.json
{
"types": [
"@open-xchange/rolldown-plugin-i18next-gettext/client"
],
}Load translation catalogs in source code on demand via i18next backend plugin:
import i18next from 'i18next'
import resourcesToBackend from 'i18next-resources-to-backend'
i18next.use(resourcesToBackend(async (lang: string, namespace: string) => {
// build appropriate path to the PO files
return await import(`../i18n/${namespace}/${lang}.po`)
}))