modify static strings in jsx code to localization friendly formats
input
const jsxText = (
<>
<p>hello world</p>
<Static>hello world</Static>
</>
);
const jsxAttribute = (
<Title name="awesome" id="title" />
);
output
import { localize } from './localizer';
const jsxText = (
<>
<p>{localize("loc_0")}</p>
<Static>hello world</Static>
</>
);
const jsxAttribute = (
<Title name={localize("loc_1")} id="title" />
);
export const localizeKeyMap = {
"loc_0": "hello world",
"loc_1": "awesome"
};
{
"elementsReplaceStringAttributes": {
"Title": ["name"]
},
"elementsPreserveJsxText": {
"Static": true
},
"keyPrefix": "loc_",
"keyType": "serial",
"localizer": "localize",
"localizerBinding": "named",
"localizerSource": "./localizer",
"keyMapIdentifier": "localizeKeyMap"
}
npm
npm install --save-dev babel-plugin-localize
yarn
yarn add -D babel-plugin-localize
.babelrc
{
"plugins": [["localize", {}]]
}
babel --plugins localize script.js
require("@babel/core").transform("code", {
plugins: [["localize", {}]]
});