Skip to content

Commit

Permalink
fix: <Select /> pseudolocalization (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCerny-awin committed Jan 27, 2021
1 parent bfa43e2 commit f1e1a25
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
10 changes: 10 additions & 0 deletions packages/cli/src/api/pseudoLocalize.test.ts
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
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

1 comment on commit f1e1a25

@vercel
Copy link

@vercel vercel bot commented on f1e1a25 Jan 27, 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.