@open-xchange/i18next-po-parser
v1.7.0
Published
Scans source code for i18next translations, and generates the contents for PO/POT files.
Downloads
4,282
Maintainers
Keywords
Readme
@open-xchange/i18next-po-parser
A small utility that scans source code for calls of the i18next translation function t() and the react-i18next component <Trans>, extracts all translation keys, and generates the contents for a PO/POT file (GetText translation catalog) containing all extracted strings.
Essentially, this utility glues together the packages i18next-cli (parses source code for t function calls etc.), and gettext-converter (generates the PO file contents from i18next catalog data).
Installation
npm install @open-xchange/i18next-po-parser
# or
pnpm add @open-xchange/i18next-po-parser
# or
yarn add @open-xchange/i18next-po-parserUsage
API
import { parsePoFile, parseSourceFiles } from '@open-xchange/i18next-po-parser'
// returns a `Record<string, string>` (i18next translation catalog)
const catalog = await parsePoFile('i18n/de-DE.po')
// returns a `Map<string, string>` (namespaces to PO catalog data)
const map = await parseSourceFiles({
project: 'Example App',
files: 'src/**/*.{js,ts,jsx,tsx}',
cwd: '/path/to/project',
})Options for parseSourceFiles
| Name | Type | Default | Description |
| - | - | - | - |
| project | string | required | The project name to be inserted into the generated PO catalog under the key 'Project-Id-Version'. May contain the placeholder [NAMESPACE]. |
| files | string \| readonly string[] | required | Glob pattern(s) for all source files to be scanned for UI strings, resolved against cwd. |
| locale | string | 'en' | The ISO locale tag to be inserted into the PO catalog under the key 'Language'. |
| cwd | string | process.cwd() | The directory that the glob pattern(s) in files are resolved against. |
CLI
The command line tool generates separate PO files per namespace. The placeholder [NAMESPACE] in the option --output will be replaced with the actual namespaces.
# 'npx' or 'pnpm' or 'yarn'
npx i18next-po-parser --project "Example App" --files "src/**/*.{js,ts,jsx,tsx}" --output dist/[NAMESPACE].pot