Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

respect skipUntranslated option when used with keyasareference option #73

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib/gettext2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function gettextToI18next(locale, body, options = {}) {
return addTextLocale(locale, body, options)
.then((data) => {
if (options.keyasareference) {
setKeysAsReference(data);
setKeysAsReference(data, options);
}

return parseJSON(locale, data, options);
Expand Down Expand Up @@ -39,7 +39,7 @@ function addTextLocale(locale, body, options = {}) {
return Promise.resolve(gt.catalogs[locale] && gt.catalogs[locale][domain].translations);
}

function setKeysAsReference(data) {
function setKeysAsReference(data, options) {
const keys = [];

Object.keys(data).forEach((ctxt) => {
Expand All @@ -49,6 +49,10 @@ function setKeysAsReference(data) {
const x = data[ctxt][key];
data[ctxt][id] = x;

if (options.skipUntranslated && x.msgstr.length === 1 && !x.msgstr[0]) {
return;
}

if (x.msgstr[0] === '') {
x.msgstr[0] = x.msgid;
}
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ describe('i18next-gettext-converter', () => {
])
));

it('should skip empty values appropriately even when using key as reference', () => (
Promise.all([
gettextToI18next('en', readFileSync(testFiles.en.untranslated), {
keyasareference: true,
skipUntranslated: true,
}).then((result) => {
const expected = requireTestFile(testFiles.en.untranslated_skipped);
expect(JSON.parse(result)).to.deep.equal(expected);
}),
])
));

// -- Error States & Invalid Data --

describe('error states', () => {
Expand Down