Skip to content

Commit

Permalink
fix(core): pound symbol being replaced outside plural and selectordin…
Browse files Browse the repository at this point in the history
…al (#1928)
  • Loading branch information
dan-dr authored May 3, 2024
1 parent 8a3b9c3 commit e94c6fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/interpolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ describe("interpolate", () => {
)
})

it("should not replace `#` symbol outside plural and selectordinal", () => {
const cache = compile("#{place} in best seller list")
expect(interpolate(cache, "en", [])({ place: 7 })).toEqual(
"#7 in best seller list"
)
})

it("should replace more than one octothorpe symbols in message", () => {
const plural = prepare("{value, plural, one {} other {# and #}}")

Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ export function interpolate(
return (values: Values = {}, formats?: Formats): string => {
const formatters = getDefaultFormats(locale, locales, formats)

const formatMessage = (tokens: CompiledMessage | number | undefined) => {
const formatMessage = (
tokens: CompiledMessage | number | undefined,
replaceOctothorpe: boolean = false
) => {
if (!Array.isArray(tokens)) return tokens

return tokens.reduce<string>((message, token) => {
if (token === "#") {
if (token === "#" && replaceOctothorpe) {
return message + OCTOTHORPE_PH
}

Expand All @@ -100,7 +103,10 @@ export function interpolate(
) {
Object.entries(format as CompiledIcuChoices).forEach(
([key, value]) => {
interpolatedFormat[key] = formatMessage(value)
interpolatedFormat[key] = formatMessage(
value,
type === "plural" || type === "selectordinal"
)
}
)
} else {
Expand Down

0 comments on commit e94c6fd

Please sign in to comment.