-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac6741
commit 5c6a67d
Showing
16 changed files
with
250 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @flow | ||
|
||
import R from "ramda" | ||
import pseudolocale from "pseudolocale" | ||
|
||
const delimiter = "%&&&%" | ||
|
||
pseudolocale.option.delimiter = delimiter | ||
// We do not want prepending and appending because of Plurals structure | ||
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 | ||
*/ | ||
const HTMLRegex = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g | ||
/* | ||
Regex should match js-lingui plurals | ||
Example: https://regex101.com/r/zXWiQR/3 | ||
*/ | ||
const PluralRegex = /{\w*,\s*plural,\s*\w*\s*{|}\s*\w*\s*({|})/g | ||
/* | ||
Regex should match js-lingui variables | ||
Example: https://regex101.com/r/kD7K2b/1 | ||
*/ | ||
const VariableRegex = /({|})/g | ||
|
||
function addDelimitersHTMLTags(message) { | ||
return message.replace(HTMLRegex, matchedString => { | ||
return `${delimiter}${matchedString}${delimiter}` | ||
}) | ||
} | ||
|
||
function addDelimitersPlural(message) { | ||
return message.replace(PluralRegex, matchedString => { | ||
return `${delimiter}${matchedString}${delimiter}` | ||
}) | ||
} | ||
|
||
function addDelimitersVariables(message) { | ||
return message.replace(VariableRegex, matchedString => { | ||
return `${delimiter}${matchedString}${delimiter}` | ||
}) | ||
} | ||
|
||
const addDelimiters = R.compose( | ||
addDelimitersVariables, | ||
addDelimitersPlural, | ||
addDelimitersHTMLTags | ||
) | ||
|
||
function removeDelimiters(message) { | ||
return message.replace(new RegExp(delimiter, "g"), "") | ||
} | ||
|
||
export default function(message: string) { | ||
message = addDelimiters(message) | ||
message = pseudolocale.str(message) | ||
|
||
return removeDelimiters(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pseudoLocalize from "./pseudoLocalize" | ||
|
||
describe("PseudoLocalization", () => { | ||
it("should pseudolocalize strings", () => { | ||
expect(pseudoLocalize("Martin Černý")).toEqual("Màŕţĩń Čēŕńý") | ||
}) | ||
|
||
it("should not pseudolocalize HTML tags", () => { | ||
expect(pseudoLocalize('Martin <span id="spanId">Černý</span>')).toEqual( | ||
'Màŕţĩń <span id="spanId">Čēŕńý</span>' | ||
) | ||
expect( | ||
pseudoLocalize("Martin Cerny 123a<span id='id'>Černý</span>") | ||
).toEqual("Màŕţĩń Ćēŕńŷ 123à<span id='id'>Čēŕńý</span>") | ||
expect(pseudoLocalize("Martin <a title='>>'>a</a>")).toEqual( | ||
"Màŕţĩń <a title='>>'>à</a>" | ||
) | ||
expect(pseudoLocalize("<a title=TITLE>text</a>")).toEqual( | ||
"<a title=TITLE>ţēxţ</a>" | ||
) | ||
}) | ||
|
||
it("should pseudlocalize plurals with HTML tags", () => { | ||
expect( | ||
pseudoLocalize( | ||
"{messagesCount, plural, zero {There's # <span>message</span>} other {There're # messages}" | ||
) | ||
).toEqual( | ||
"{messagesCount, plural, zero {Ţĥēŕē'ś # <span>mēśśàĝē</span>} other {Ţĥēŕē'ŕē # mēśśàĝēś}" | ||
) | ||
}) | ||
|
||
it("should pseudolocalize plurals", () => { | ||
expect( | ||
pseudoLocalize("{value, plural, one {# book} other {# books}}") | ||
).toEqual("{value, plural, one {# ƀōōķ} other {# ƀōōķś}}") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"lingui": { | ||
"format": "po", | ||
"format": "lingui", | ||
"localeDir": "./locale", | ||
"sourceLocale": "en" | ||
"sourceLocale": "en", | ||
"pseudoLocale": "en-PT" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.