Skip to content

Commit

Permalink
fix(): country-coded language breaks plurals with po-gettext (#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal authored Sep 7, 2021
1 parent cbc87b5 commit 6b60b8a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
39 changes: 39 additions & 0 deletions packages/cli/src/api/formats/__snapshots__/po-gettext.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[` 1`] = `
Object {
message_with_id: Object {
comments: Array [],
extractedComments: Array [],
flags: Array [],
obsolete: false,
origin: Array [],
translation: {someCount, plural, one {Singular case} other {Case number {someCount}}},
},
message_with_id_but_without_translation: Object {
comments: Array [],
extractedComments: Array [
Comment made by the developers.,
],
flags: Array [],
obsolete: false,
origin: Array [],
translation: ,
},
{anotherCount, plural, one {Singular case} other {Case number {anotherCount}}}: Object {
comments: Array [],
extractedComments: Array [],
flags: Array [],
obsolete: false,
origin: Array [],
translation: {anotherCount, plural, one {Singular case} other {Case number {anotherCount}}},
},
{count, plural, one {Singular} other {Plural}}: Object {
comments: Array [],
extractedComments: Array [],
flags: Array [],
obsolete: false,
origin: Array [],
translation: ,
},
}
`;

exports[`po-gettext format should convert ICU plural messages to gettext plurals 1`] = `
msgid ""
msgstr ""
Expand Down
33 changes: 33 additions & 0 deletions packages/cli/src/api/formats/fixtures/messages_plural-4-letter.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
msgid ""
msgstr ""
"POT-Creation-Date: 2018-08-27 10:00+0000\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: @lingui/cli\n"
"Language: en_CA\n"

#. js-lingui:pluralize_on=someCount
msgid "message_with_id"
msgid_plural "message_with_id_plural"
msgstr[0] "Singular case"
msgstr[1] "Case number {someCount}"

#. js-lingui:icu=%7BanotherCount%2C+plural%2C+one+%7BSingular+case%7D+other+%7BCase+number+%7BanotherCount%7D%7D%7D&pluralize_on=anotherCount
msgid "Singular case"
msgid_plural "Case number {anotherCount}"
msgstr[0] "Singular case"
msgstr[1] "Case number {anotherCount}"

#. Comment made by the developers.
#. js-lingui:pluralize_on=count
msgid "message_with_id_but_without_translation"
msgid_plural "message_with_id_but_without_translation_plural"
msgstr[0] ""
msgstr[1] ""

#. js-lingui:icu=%7Bcount%2C+plural%2C+one+%7BSingular%7D+other+%7BPlural%7D%7D&pluralize_on=count
msgid "Singular"
msgid_plural "Plural"
msgstr[0] ""
msgstr[1] ""
11 changes: 11 additions & 0 deletions packages/cli/src/api/formats/po-gettext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,15 @@ msgstr[2] "# dní"
})
})
})

describe("convertPluralsToIco handle correctly locales with 4-letter", () => {
const pofile = fs
.readFileSync(
path.join(path.resolve(__dirname), "fixtures", "messages_plural-4-letter.po")
)
.toString()

const catalog = format.parse(pofile)
expect(catalog).toMatchSnapshot()
})
})
6 changes: 4 additions & 2 deletions packages/cli/src/api/formats/po-gettext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ const deserialize: (Object) => Object = R.map(
* https://github.com/LLK/po2icu/blob/9eb97f81f72b2fee02b77f1424702e019647e9b9/lib/po2icu.js#L148.
*/
const getPluralCases = (lang: string): string[] | undefined => {
const gettextPluralsInfo = gettextPlurals[lang]
// If users uses locale with underscore or slash, es-ES, es_ES, gettextplural is "es" not es-ES.
const [correctLang] = lang.split(/[-_]/g)
const gettextPluralsInfo = gettextPlurals[correctLang]

return gettextPluralsInfo?.examples.map((pluralCase) =>
pluralsCldr(lang, pluralCase.sample)
pluralsCldr(correctLang, pluralCase.sample)
)
}

Expand Down

1 comment on commit 6b60b8a

@vercel
Copy link

@vercel vercel bot commented on 6b60b8a Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.