{translation1} {translation2} {translation3}
; } ``` ### Fragmented sentences You can use `declareStatic` to handle fragmented sentences with function calls. ```jsx copy import { declareStatic, gt } from 'gt-react'; function getSubject(gender) { return gender === 'male' ? 'boy' : 'girl'; } function Component({ gender }) { const translation = gt(`The ${declareStatic(getSubject(gender))} is playing.`); return{translation}
; } ``` ### Agreement Without `declareStatic`, translating agreement is syntactically heavy. You have to add a select statement to handle agreement (plurality, gender, etc.). Then, you must also enumerate each possible outcome. ```jsx copy import { gt } from 'gt-react'; function getSubject(gender) { return gender === 'male' ? 'boy' : 'girl'; } function Component({ gender }) { const translation = gt( '{gender, select, boy {The boy is playing.} girl {The girl is playing.} other {}}', { gender: getSubject(gender) , }, ); return{translation}
; } ``` With `declareStatic`, agreement becomes trivial. No select statment is required nor do you need to specify every outcome. ```jsx copy import { declareStatic, declareVar, gt } from 'gt-react'; function getSubject(gender) { return gender === 'male' ? 'boy' : 'girl'; } function Component({ gender }) { const translation = gt(`The ${declareStatic(getSubject(gender))} is playing.`); return{translation}
; } ``` By using `declareStatic`, the CLI tool identifies that `getSubject` has two possible outcomes, and creates a translation entry for each outcome. By doing so, agreement is handled automatically. - "The boy is playing" -> "*El* niño está jugando" - "The girl is playing" -> "*La* niña está jugando" ### With variables You can combine `declareStatic` with `declareVar` for dynamic content. ```jsx copy import { declareStatic, declareVar, gt } from 'gt-react'; function getGreeting(name) { return name ? `Hello, ${declareVar(name)}` : 'Hello, stranger'; } function Component({ name }) { const translation = gt(`${declareStatic(getGreeting(name))}! How are you?`); return{translation}
; } ``` ### Complex functions Functions can contain multiple conditional branches and return statements. ```jsx copy import { declareStatic, gt } from 'gt-react'; function getStatusMessage(status, priority) { if (status === 'complete') { return priority === 'high' ? 'Urgent task completed' : 'Task completed'; } else if (status === 'pending') { return priority === 'high' ? 'Urgent task pending' : 'Task pending'; } return 'Task status unknown'; } function Component({ status, priority }) { const message = gt(`${declareStatic(getStatusMessage(status, priority))}.`); return{message}
; } ``` ### Inline expressions and logic You can embed logic directly inside of the `declareStatic` call. ```jsx copy import { declareStatic, gt } from 'gt-react'; function Component({ gender }) { const message = gt(`The ${declareStatic(gender === 'male' ? 'boy' : 'girl')} is playing.`); return{message}
; } ``` --- ## Notes * Use `declareStatic` judiciously as it can exponentially increase translation entries * All possible outcomes must be statically analyzable at build time * Variables within static functions should be wrapped with `declareVar` ## Next steps * See [`declareVar`](/docs/react/api/strings/declare-var) for marking dynamic content within static functions * See [`decodeVars`](/docs/react/api/strings/decode-vars) for extracting original values from declared variables * See [`