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

fix: <Select /> pseudolocalization #961

Merged
merged 2 commits into from
Jan 27, 2021
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
10 changes: 10 additions & 0 deletions packages/cli/src/api/pseudoLocalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ describe("PseudoLocalization", () => {
)
})

it("Select", () => {
expect(
pseudoLocalize(
"{gender, select, male {He} female {She} other {<span>Other</span>}}"
)
).toEqual(
"{gender, select, male {Ĥē} female {Śĥē} other {<span>Ōţĥēŕ</span>}}"
)
})

it("should not pseudolocalize variables", () => {
expect(pseudoLocalize("replace {count}")).toEqual("ŕēƥĺàćē {count}")
expect(pseudoLocalize("replace { count }")).toEqual("ŕēƥĺàćē { count }")
Expand Down
39 changes: 22 additions & 17 deletions packages/cli/src/api/pseudoLocalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ pseudolocale.option.delimiter = delimiter
pseudolocale.option.prepend = ""
pseudolocale.option.append = ""

/*
Regex should match HTML tags
It was taken from https://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/
Example: https://regex101.com/r/bDHD9z/3
*/
/**
* Regex should match HTML tags
* It was taken from https://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/
* Example: https://regex101.com/r/bDHD9z/3
*/
const HTMLRegex = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g
/*
Regex should match js-lingui plurals
Example: https://regex101.com/r/utnbQw/4
*/
const PluralRegex = /({\w*,\s*(plural|selectordinal),(.|\n)*?{)|(}\s*(offset|zero|one|two|few|many|other)\s*{)/g
/*
Regex should match js-lingui variables
Example: https://regex101.com/r/dw1QHb/2
*/

/**
* Regex should match js-lingui Plurals, Select and SelectOrdinal components
* Example:
* Plurals https://regex101.com/r/VUJXg0/1
* SelectOrdinal https://regex101.com/r/T7hSLU/2
* Select https://regex101.com/r/9JnqB9/1
*/
const MacroRegex = /({\w*,\s*(plural|selectordinal|select),(.|\n)*?{)|(}\s*\w*\s*{)/g

/**
* Regex should match js-lingui variables
* Example: https://regex101.com/r/dw1QHb/2
*/
const VariableRegex = /({\s*[a-zA-Z_$][a-zA-Z_$0-9]*\s*})/g

function addDelimitersHTMLTags(message) {
Expand All @@ -31,8 +36,8 @@ function addDelimitersHTMLTags(message) {
})
}

function addDelimitersPlural(message) {
return message.replace(PluralRegex, (matchedString) => {
function addDelimitersMacro(message) {
return message.replace(MacroRegex, (matchedString) => {
return `${delimiter}${matchedString}${delimiter}`
})
}
Expand All @@ -45,7 +50,7 @@ function addDelimitersVariables(message) {

const addDelimiters = R.compose(
addDelimitersVariables,
addDelimitersPlural,
addDelimitersMacro,
addDelimitersHTMLTags
)

Expand Down