Skip to content

Commit

Permalink
feat: ✨ Added ability to specify which po files to update on export
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeychikk committed Aug 21, 2019
1 parent e2a3bb3 commit 06f1cc3
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Usage

- `export-strings [input-files-glob] [output] [--default-locale=locale]` parse through all the files provided in `input-files-glob` (`src/**/{*.js,*.jsx,*.ts,*.tsx}` by default) (uses [glob](https://www.npmjs.com/package/glob)) and generate .pot file in the output path (`./src/i18n/template.pot` by default). Then searches for all the `.po` files in the same directory and updates them with new strings to translate. If `default-locale` is provided (e.g. `en`) and this locale's `.po` file exists in the same folder (e.g. `en.po`), this file will be populated with the translations automatically.
- `export-strings [input-files-glob] [output] [--default-locale=locale] [--po-files-path]` parse through all the files provided in `input-files-glob` (`src/**/{*.js,*.jsx,*.ts,*.tsx}` by default) (uses [glob](https://www.npmjs.com/package/glob)) and generate .pot file in the output path (`./src/i18n/template.pot` by default). Then searches for all the `.po` files in the same directory and updates them with new strings to translate. If `default-locale` is provided (e.g. `en`) and this locale's `.po` file exists in the same folder (e.g. `en.po`), this file will be populated with the translations automatically. You can specify additional path(s) to update `.po` files by using `--po-files-path your/custom/folder your/other/directory`.
- `import-strings [po-files-path] [output]` parse all the `.po` files inside the directory provided as `po-files-path` (`./src/i18n/` by default) and generate [react-targem](https://github.com/trucknet-io/react-targem) or [lioness](https://github.com/alexanderwallin/lioness) compatible `.json` file in the output path (`./src/i18n/translations.json`), which is an object with each locale as a key and [gettext-parser](https://www.npmjs.com/package/gettext-parser) object for this locale as a value. By default all `translations.json` are optimized, e.g. all unnecessary fields are removed. Pass `--no-optimize` or `--optimize=false` to disabled optimizations.
- `merge-translations [po-files-dir-path] [template-path]` merge updated .pot file with .po. Done automatically by `export-strings` command. If `default-locale` is provided (e.g. `en`) and this locale's `.po` file exists in the same folder (e.g. `en.po`), this file will be populated with the translations automatically.
- `validate-strings [po-files-dir-path] [template-path]` validate all `.po` files inside `po-files-dir-path` (`./src/i18n/` by default) to have all the translations in the `.pot` file provided in `template-path` (`./src/i18n/template.pot` by default).
Expand Down
13 changes: 13 additions & 0 deletions __fixtures__/en.header.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"POT-Creation-Date: Thu Jan 01 2018 00:00:00\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Project-Id-Version: gettext-utils 0.0.0\n"
"PO-Revision-Date: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"X-Generator: Poedit 2.2.1\n"
"Last-Translator: \n"
"Language: en\n"
37 changes: 37 additions & 0 deletions __fixtures__/poPath1/en.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"POT-Creation-Date: Thu Jan 01 2018 00:00:00\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Project-Id-Version: gettext-utils 0.0.0\n"
"PO-Revision-Date: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"X-Generator: Poedit 2.2.1\n"
"Last-Translator: \n"
"Language: en\n"

#: __fixtures__/react-project/src/components/LionessHocComponent.tsx:19
msgctxt "lion.sound"
msgid "Rrrrr"
msgstr "Rrrrr"

#: __fixtures__/react-project/src/components/LionessHocComponent.tsx:23
msgctxt "lion.females"
msgid "one female lion"
msgid_plural "{{ count }} female lions"
msgstr[0] "one female lion"
msgstr[1] "{{ count }} female lions"

#: __fixtures__/react-project/src/components/LionessTComponent.jsx:22
msgctxt "lion.title"
msgid "Lion"
msgstr "Lion"

#: __fixtures__/react-project/src/components/LionessTComponent.jsx:23
msgctxt "lion.children"
msgid "Lion has one child"
msgid_plural "Lion has {{ count }} children"
msgstr[0] "Lion has one child"
msgstr[1] "Lion has {{ count }} children"
37 changes: 37 additions & 0 deletions __fixtures__/poPath2/en.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"POT-Creation-Date: Thu Jan 01 2018 00:00:00\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Project-Id-Version: gettext-utils 0.0.0\n"
"PO-Revision-Date: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"X-Generator: Poedit 2.2.1\n"
"Last-Translator: \n"
"Language: en\n"

#: __fixtures__/react-project/src/components/LionessHocComponent.tsx:19
msgctxt "lion.sound"
msgid "Rrrrr"
msgstr "Rrrrr"

#: __fixtures__/react-project/src/components/LionessHocComponent.tsx:23
msgctxt "lion.females"
msgid "one female lion"
msgid_plural "{{ count }} female lions"
msgstr[0] "one female lion"
msgstr[1] "{{ count }} female lions"

#: __fixtures__/react-project/src/components/LionessTComponent.jsx:22
msgctxt "lion.title"
msgid "Lion"
msgstr "Lion"

#: __fixtures__/react-project/src/components/LionessTComponent.jsx:23
msgctxt "lion.children"
msgid "Lion has one child"
msgid_plural "Lion has {{ count }} children"
msgstr[0] "Lion has one child"
msgstr[1] "Lion has {{ count }} children"
34 changes: 34 additions & 0 deletions __tests__/__snapshots__/exportStrings.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,37 @@ msgid_plural \\"Lion has {{ count }} children\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\""
`;

exports[`Should update translations by --po-files-path: template.pot 1`] = `
"msgid \\"\\"
msgstr \\"\\"
\\"Project-Id-Version: gettext-utils 0.0.0\\\\n\\"
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
\\"POT-Creation-Date: Thu Jan 01 2018 00:00:00\\\\n\\"
\\"Content-Transfer-Encoding: 8bit\\\\n\\"
\\"Plural-Forms: nplurals=2; plural=(n != 1);\\\\n\\"
#: __fixtures__/react-project/src/components/LionessHocComponent.tsx:19
msgctxt \\"lion.sound\\"
msgid \\"Rrrrr\\"
msgstr \\"\\"
#: __fixtures__/react-project/src/components/LionessHocComponent.tsx:23
msgctxt \\"lion.females\\"
msgid \\"one female lion\\"
msgid_plural \\"{{ count }} female lions\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#: __fixtures__/react-project/src/components/LionessTComponent.jsx:22
msgctxt \\"lion.title\\"
msgid \\"Lion\\"
msgstr \\"\\"
#: __fixtures__/react-project/src/components/LionessTComponent.jsx:23
msgctxt \\"lion.children\\"
msgid \\"Lion has one child\\"
msgid_plural \\"Lion has {{ count }} children\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\""
`;
27 changes: 27 additions & 0 deletions __tests__/exportStrings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ it("Should update .po files with new translations", async () => {
}
});

it("Should update translations by --po-files-path", async () => {
const tmpPath = await getTmpPath();
const filePath = path.join(tmpPath, templateName);
await fse.createFile(filePath);

// Reset po files before testing that they are actually updated
const poHeader = await fse.readFile("__fixtures__/en.header.po", encoding);
const poPaths = ["__fixtures__/poPath1", "__fixtures__/poPath2"];
await fse.writeFile(`${poPaths[0]}/en.po`, poHeader);
await fse.writeFile(`${poPaths[1]}/en.po`, poHeader);

await exportStrings(
"__fixtures__/react-project/src/**/{*.ts,*.tsx,*.js,*.jsx}",
filePath,
"en",
poPaths,
);
const templatePot = await fse.readFile(filePath, encoding);
expect(templatePot).toMatchSnapshot(templateName);
expect(templatePot).toMatch(/msgctxt "lion\.females"/);

const po1 = await fse.readFile(`${poPaths[0]}/en.po`, encoding);
const po2 = await fse.readFile(`${poPaths[1]}/en.po`, encoding);
expect(po1).toMatch(/msgctxt "lion\.females"/);
expect(po2).toMatch(/msgctxt "lion\.females"/);
});

it("Should generate Project-Id-Version header", async () => {
const tmpPath = await getTmpPath();
const filePath = path.join(tmpPath, templateName);
Expand Down
13 changes: 11 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ yargs
.command(
"export-strings [input-files-glob] [output-file-path]",
"exports translations from to .pot and .po files",
// @ts-ignore
(yargsArguments: Argv) => {
return yargsArguments
.positional("inputFilesGlob", {
Expand All @@ -24,18 +25,26 @@ yargs
.positional("outputFilePath", {
describe: "Path to output .pot file",
default: "./src/i18n/template.pot",
});
})
.array("poFilesPath");
},
({
inputFilesGlob,
outputFilePath,
defaultLocale,
poFilesPath,
}: {
inputFilesGlob: string;
outputFilePath: string;
defaultLocale?: string;
poFilesPath?: string[];
}) => {
return exportStrings(inputFilesGlob, outputFilePath, defaultLocale);
return exportStrings(
inputFilesGlob,
outputFilePath,
defaultLocale,
poFilesPath,
);
},
)
.command(
Expand Down
6 changes: 6 additions & 0 deletions src/exportStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const exportStrings = async (
inputFilesGlob: string,
templateFilePath: string,
defaultLocale?: string,
poFilesPath?: string[],
) => {
const templateDirPath = path.dirname(templateFilePath);

Expand All @@ -24,6 +25,11 @@ const exportStrings = async (
return;
}
await writeFile(templateFilePath, templatePot, "utf-8");
if (poFilesPath) {
for (const poPath of poFilesPath) {
await updateTranslations(poPath, templateFilePath, defaultLocale);
}
}
await updateTranslations(templateDirPath, templateFilePath, defaultLocale);
};

Expand Down

0 comments on commit 06f1cc3

Please sign in to comment.