Prompt
How do I automate string extraction and publishing?
Latest observation
To automate string extraction and publishing, the usual goal is:
- Extract translatable text from source code / UI files
- Write it into localization files (JSON, PO, XLIFF, etc.)
- Push those files to a translation platform or repo
- Pull translated strings back
- Publish them as part of your build/deploy pipeline
Here’s a practical way to set it up.
1) Pick a localization format
Common choices:
- JSON: simple for web/apps
- PO/POT: common in GNU/Drupal/PHP ecosystems
- XLIFF: common in enterprise localization workflows
- CSV: easy for translators, less ideal for automation
If you’re starting from scratch, JSON + translation platform is usually easiest.
2) Mark strings in code
Use a consistent extraction pattern:
t("hello")_("hello")<FormattedMessage id="..." defaultMessage="..." />
Example:
const label = t("Save changes");
For React/i18n tools, you might use something like:
import { defineMessages } from "react-intl";
3) Use an extraction tool
Pick a tool based on your stack:
JavaScript/TypeScript
- i18next-parser
- formatjs
- lingui
- gettext-extract
Python
- Babel
Ruby
- gettext
PHP
- Laravel localization tools
- Symfony translation tools
Generic or polyglot
- custom script using regex/AST parsing
- gettext-based workflows
4) Automate extraction in a script
Example with npm scripts:
{
"scripts": {
"extract:i18n": "i18next-parser 'src/**/*.{js,ts,jsx,tsx}' --output locales/$LOCALE/{{ns}}.json"
}
}
Or a typical CI step:
npm run extract:i18n
Better yet, use a dedicated localization command:
npm run i18n:extract
npm run i18n:push
npm run i18n:pull
npm run build
5) Push extracted strings to a translation service
Popular services:
- Lokalise
- Crowdin
- Phrase
- Transifex
- POEditor
These usually provide:
- CLI tools
- API keys
- GitHub/GitLab integration
- webhooks
Typical flow:
- extraction creates/update source language file
- CLI uploads it to the translation platform
- translators work in the platform
- CLI downloads translated files
Example pseudocode:
translation-cli upload locales/en.json
translation-cli download --all
6) Automate with CI/CD
A common pipeline:
On every merge to main:
- extract strings
- compare with existing source file
- upload source strings to translation platform
- commit/publish updated translation files
On translation updates:
- download translated files
- run validation checks
- build app
- deploy
Example GitHub Actions outline:
name: i18n-sync
on:
push:
branches: [main]
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run extract:i18n
- run: npm run i18n:push
- run: npm run i18n:pull
- run: npm run build
7) Validate and fail early
Add checks so broken translations don’t ship:
- missing keys
- invalid JSON
- placeholder mismatches
- duplicate keys
- untranslated source strings in non-source locales
Example checks:
eslint-plugin-i18n- custom script to compare keys
- schema validation for JSON
8) Publish automatically
Depending on “publishing” means for you, automate one of these:
If publishing app builds
- bundle translation files into the app during build
- deploy from CI
If publishing docs or website content
- regenerate static pages
- commit output or deploy via CI
If publishing to a package registry
- include locale assets in package release
- version them with the app
9) Recommended workflow
A solid pattern is:
- Developers add strings using a
t()helper - Extraction runs in CI or pre-commit
- Source strings are pushed to translation platform
- Translations are pulled periodically or on webhook
- CI validates and publishes translated assets
10) Example “simple” setup
If you want a low-friction setup:
- React app
- i18next
- i18next-parser
- Crowdin or Lokalise
- GitHub Actions
Flow:
npm run extract:i18n
git commit -am "Update source strings"
npm run i18n:push
npm run i18n:pull
npm run build
11) If you want fully automatic publishing
You can use:
- webhooks from translation platform
- CI triggers on translation updates
- scheduled sync jobs nightly/hourly
That way, when translators finish work, the system:
- downloads new translations
- runs tests
- deploys automatically
If you want, I can give you:
- a concrete setup for your stack (React, Python, PHP, etc.)
- a GitHub Actions pipeline
- a CLI script example for extraction + upload + download