po-loader
raw JSON → 0.7.0 verified Sat Apr 25 auth: no javascript maintenance
Webpack loader for translating .po files (GNU gettext) into JSON. Uses po2json under the hood, supports mozilla/nunjucks, Jed, and other formats. Version 0.7.0 (latest, 2017). Sync and async usage; often paired with Jed for i18n. No updates since 2017, works with webpack v1 only. Not actively maintained; consider @lingui/webpack or i18n-webpack-plugin.
Common errors
error Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. ↓
cause No loader configured for .po files in webpack
fix
Add po-loader and json-loader to webpack config: { test: /\.po$/, loader: 'json!po' }
error Cannot find module 'po2json' ↓
cause Missing peer dependency po2json
fix
npm install po2json@>=1.0.0-beta-3 --save-dev or --save
Warnings
breaking po-loader requires webpack 1.x only; does not work with webpack 2+ modular loaders. Use json!po syntax which breaks in webpack 2+ ↓
fix Upgrade to webpack 1 or switch to a modern i18n webpack plugin like @lingui/webpack
deprecated po2json peer dependency must be >=1.0.0-beta-3, but that version may have breaking changes. Always pin exact version. ↓
fix Use po2json@1.0.0-beta-3 exactly in package.json
gotcha Requiring .po files without the json loader prefix (json!po) will output raw po2json format, not run through po-loader ↓
fix Always use json!po prefix in require or configure in webpack.config with json!po loader string
breaking Dynamic import path must match the regex for webpack to create separate bundles; otherwise returns undefined ↓
fix Use import(`${LOCALE_ROOT}/${locale}/LC_MESSAGES/messages.po`) with LOCALE_ROOT as a constant string
Install
npm install po-loader yarn add po-loader pnpm add po-loader Imports
- po-loader wrong
require('./locale.po');correctimport messages from './locale.po'; - po-loader wrong
var messages = require('po-loader!./locale.po');correctvar messages = require('json!po!./locale.po'); - po-loader wrong
import locale from './locale.po'; // missing async loadingcorrectimport localePromise from './locale.po'; // then call .then() or await
Quickstart
// webpack.config.js
{
test: /\.po$/,
loader: 'json!po',
exclude: /node_modules/
}
// entry.js
import Jed from 'jed';
async function init() {
const localeData = await import('./locale/en_US/LC_MESSAGES/messages.po');
const i18n = new Jed(localeData);
console.log(i18n.gettext('Hello'));
}
init();