-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix - icu - support nesting icu params
Support nesting icu params
- Loading branch information
Showing
14 changed files
with
182 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
|
||
## [2.1.15] - 27-06-2024 | ||
### Fixed | ||
- `icu` - add support for nested icu parameters. key format example: `Hello, {numPersons, plural, =0 {No one.} =1 {Mr. {personName}} other {# persons}}` | ||
|
||
## [2.1.4] - 29-01-2023 | ||
### Added | ||
- `icu` - add support for icu format. key format example: ` {numPersons, plural, =0 {no persons} =1 {one person} other {# persons}}` |
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,92 @@ | ||
import parse, { AST, Element, SubMessages } from 'format-message-parse'; | ||
|
||
interface Param { | ||
name: string; | ||
} | ||
|
||
type Format = | ||
| 'plural' | ||
| 'selectordinal' | ||
| 'date' | ||
| 'time' | ||
| 'select' | ||
| 'number'; | ||
|
||
const isPlural = (format: Format): boolean => { | ||
return format === 'plural' || format === 'selectordinal'; | ||
}; | ||
|
||
const isSelect = (format: Format): boolean => { | ||
return format === 'select'; | ||
}; | ||
|
||
const hasHashtagOnly = (element: Element | undefined): boolean => { | ||
return element!.length === 1 && element![0] === '#'; | ||
}; | ||
|
||
const isStringOnly = (element: Format | undefined): boolean => { | ||
return typeof element === 'string'; | ||
}; | ||
|
||
const isValidSubMessages = (subMessages: {} | undefined): boolean => { | ||
return typeof subMessages === 'object'; | ||
}; | ||
|
||
const getSubMessages = ( | ||
element: Element | undefined, | ||
format: Format | ||
): SubMessages | undefined => { | ||
if (element) { | ||
let subMessages: SubMessages | undefined; | ||
if (isPlural(format)) { | ||
subMessages = element[3] as SubMessages; | ||
} else if (isSelect(format)) { | ||
subMessages = element[2] as SubMessages; | ||
} | ||
if (isValidSubMessages(subMessages)) { | ||
return subMessages as SubMessages; | ||
} | ||
} | ||
return undefined; | ||
}; | ||
|
||
const stackWithSubMessages = ( | ||
stack: Element[], | ||
subMessages: SubMessages | ||
): Element[] => { | ||
// eslint-disable-next-line prefer-spread | ||
return stack.concat.apply(stack, Object.values(subMessages)); | ||
}; | ||
|
||
const getParamsFromPatternAst = (parsedArray: AST): Param[] => { | ||
if (!parsedArray || !parsedArray.slice) return []; | ||
let stack = parsedArray.slice(); | ||
const params: Param[] = []; | ||
const used = new Set(); | ||
while (stack.length) { | ||
const element = stack.pop(); | ||
if (isStringOnly(element as Format)) continue; | ||
if (hasHashtagOnly(element)) continue; | ||
|
||
const [name, format] = element!; | ||
|
||
if (!used.has(name)) { | ||
params.push({ name: name as string }); | ||
used.add(name); | ||
} | ||
|
||
const subMessages = getSubMessages(element, format as Format); | ||
if (subMessages) { | ||
stack = stackWithSubMessages(stack, subMessages); | ||
} | ||
} | ||
return params.reverse(); | ||
}; | ||
|
||
export const getTypedParams = (text: string): Param[] => { | ||
try { | ||
return getParamsFromPatternAst(parse(text)); | ||
} catch (e) { | ||
return []; | ||
} | ||
}; |
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,14 @@ | ||
/* eslint-disable */ | ||
/* tslint:disable */ | ||
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) { | ||
return { | ||
common: { | ||
people: { | ||
message: (data: Record<'numPersons', unknown>) => t('common.people.message', data), /* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */ | ||
messageNestedParams: (data: Record<'name' | 'numPersons', unknown>) => t('common.people.messageNestedParams', data), /* Hey, {numPersons, plural, =0 {No one here.} one {{name}. You are the only person here.} other {{name} and # other persons are here.}} */ | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export type ILocaleKeys = ReturnType<typeof LocaleKeys>; |
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,8 @@ | ||
{ | ||
"common": { | ||
"people": { | ||
"message": "Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}}", | ||
"messageNestedParams": "Hey, {numPersons, plural, =0 {No one here.} one {{name}. You are the only person here.} other {{name} and # other persons are here.}}" | ||
} | ||
} | ||
} |
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