Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@formatjs/cli): add support for pseudo-locales, fix #2165
- Loading branch information
Showing
6 changed files
with
380 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
parse, | ||
MessageFormatElement, | ||
TYPE, | ||
isLiteralElement, | ||
isPluralElement, | ||
isSelectElement, | ||
} from 'intl-messageformat-parser'; | ||
|
||
export function generateXXLS( | ||
msg: string | MessageFormatElement[] | ||
): MessageFormatElement[] { | ||
const ast = typeof msg === 'string' ? parse(msg) : msg; | ||
const lastChunk = ast.pop(); | ||
if (lastChunk && isLiteralElement(lastChunk)) { | ||
lastChunk.value += 'SSSSSSSSSSSSSSSSSSSSSSSSS'; | ||
return [...ast, lastChunk]; | ||
} | ||
return [...ast, {type: TYPE.literal, value: 'SSSSSSSSSSSSSSSSSSSSSSSSS'}]; | ||
} | ||
|
||
export function generateXXAC( | ||
msg: string | MessageFormatElement[] | ||
): MessageFormatElement[] { | ||
const ast = typeof msg === 'string' ? parse(msg) : msg; | ||
ast.forEach(el => { | ||
if (isLiteralElement(el)) { | ||
el.value = el.value.toUpperCase(); | ||
} else if (isPluralElement(el) || isSelectElement(el)) { | ||
for (const opt of Object.values(el.options)) { | ||
generateXXAC(opt.value); | ||
} | ||
} | ||
}); | ||
return ast; | ||
} | ||
|
||
export function generateXXHA( | ||
msg: string | MessageFormatElement[] | ||
): MessageFormatElement[] { | ||
const ast = typeof msg === 'string' ? parse(msg) : msg; | ||
const firstChunk = ast.shift(); | ||
if (firstChunk && isLiteralElement(firstChunk)) { | ||
firstChunk.value = '[javascript]' + firstChunk.value; | ||
return [firstChunk, ...ast]; | ||
} | ||
return [{type: TYPE.literal, value: '[javascript]'}, ...ast]; | ||
} | ||
|
||
const ASCII = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
const ACCENTED_ASCII = 'âḃćḋèḟĝḫíĵǩĺṁńŏṗɋŕśṭůṿẘẋẏẓḀḂḈḊḔḞḠḢḬĴḴĻḾŊÕṔɊŔṠṮŨṼẄẌŸƵ'; | ||
|
||
export function generateENXA( | ||
msg: string | MessageFormatElement[] | ||
): MessageFormatElement[] { | ||
const ast = typeof msg === 'string' ? parse(msg) : msg; | ||
ast.forEach(el => { | ||
if (isLiteralElement(el)) { | ||
el.value = el.value | ||
.split('') | ||
.map(c => { | ||
const i = ASCII.indexOf(c); | ||
if (i < 0) { | ||
return c; | ||
} | ||
return ACCENTED_ASCII[i]; | ||
}) | ||
.join(''); | ||
} else if (isPluralElement(el) || isSelectElement(el)) { | ||
for (const opt of Object.values(el.options)) { | ||
generateENXA(opt.value); | ||
} | ||
} | ||
}); | ||
return ast; | ||
} |
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.