Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
feat(text): Add txOptions prop to Text to pass options through to tra…
Browse files Browse the repository at this point in the history
…nslate (#191 by @ryanlntn)
  • Loading branch information
ryanlntn authored May 10, 2019
1 parent 1efdceb commit 09fd617
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
6 changes: 6 additions & 0 deletions boilerplate/app/components/text/text.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export interface TextProps extends TextProperties {
*/
tx?: string

/**
* Optional options to pass to i18n. Useful for interpolation
* as well as explicitly setting locale or translation fallbacks.
*/
txOptions?: object

/**
* The text to display if not using `tx` or nested components.
*/
Expand Down
4 changes: 2 additions & 2 deletions boilerplate/app/components/text/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { mergeAll, flatten } from "ramda"
*/
export function Text(props: TextProps) {
// grab the props
const { preset = "default", tx, text, children, style: styleOverride, ...rest } = props
const { preset = "default", tx, txOptions, text, children, style: styleOverride, ...rest } = props

// figure out which content to use
const i18nText = tx && translate(tx)
const i18nText = tx && translate(tx, txOptions)
const content = i18nText || text || children

const style = mergeAll(flatten([presets[preset] || presets.default, styleOverride]))
Expand Down
16 changes: 3 additions & 13 deletions boilerplate/app/i18n/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import i18n from "i18n-js"
*
* @param key The i18n key.
*/
export function translate(key: string) {
return key ? i18n.t(key) : null
}

/**
* Translates with variables.
*
* @param key The i18n key
* @param vars Additional values sure to replace.
*/
export function translateWithVars(key: string, vars: object) {
return key ? i18n.t(key, vars) : null
}
export function translate(key: string, options?: object) {
return key ? i18n.t(key, options) : null
}

0 comments on commit 09fd617

Please sign in to comment.