Fluent React Components
Basic fluent-react makes use of Localized
components to wrap jsx components, like this:
{ return <div ='container'> <Localized ='NotAuthorized_oopsMessage'> <h1> /* here is a comment for the localizer */ Oops! looks like not authorized to use this app </h1> </Localized> <Localized ='NotAuthorized_warningImg' = > <img = ="warning sign" /> </Localized> <Localized ='NotAuthorized_possibleActions' = = = > <p> '<link>Try again</link>, go <a>Home</a> or <button>Logout</button>' </p> </Localized> </div> ;}
This makes for a lot of Localized
components on the page, and can make it hard
to visually scan for specific HTML elements. This package comes with standard HTML
elements already wrapped in Localized
with their typical attrs
enabled in the
Loc
component. This turns the above into:
import Loc from 'fluent-react-utils'; { return <div ="container"> <Loc.H1 ="NotAuthorized_oopsMessage"> /* here is a comment for the localizer */ Oops! looks like not authorized to use this app </Loc.H1> <Loc.Img ="NotAuthorized_warningImg" ="warning sign" = /> <Loc.P ="NotAuthorized_possibleActions" = > '<link>Try again</link>, go <a>Home</a> or <button>Logout</button>' </Loc.P> </div> ;}
Props
In the Loc
components you have access to three l10n
props to describe how
this message needs to be translated.
l10nId (Required)
The localization key for that message
l10nVars
This is a plain object with the message variables as keys. It's only used if the message has a variable to substitute.
<Loc.P ="welcome" => 'Welcome, { $name }'</Loc.P>
l10nJsx
A plain object with the element alias as the key and a JSX component as the value. This is used for any components that are included inline in the message, with the alias used in HTML-like syntax to wrap the translated message.
<Loc.P ="homeLink" => 'Go <link>home</link>' </Loc.P>
Custom Loc Components
Your project may have components that would be great to include in the Loc
object, but which are not standard HTML. Say you have a styled MyButton
component, for example.
<Localized ="Button_click" => <MyButton ="click me!" = /></Localized>
You can make use of two helpers from fluent-react-utils to quickly wrap a
component in Localized
and / or add it to the Loc
object.
makeLocalizedElement(Element[, attrs])
const MyLocalizedButton =
MyLocalizedButton
will now have access to the l10nId
, l10nVars
, and
l10nJsx
props.
augmentLoc(customElements)
In order for your custom elements to be recognized by the string extraction tool,
they need to be prefixed by Loc.
(or a custom name of your choice -
see information about the .l10nrc
file).
// my-utils; const customElements = MyButton: MyLocalizedButton; const Loc = ; // elsewhere ; ...<LocH1 l10nId="ButtonPage_title"> Look a button!</LocH1><LocMyButton l10nId="ButtonPage_clickButton" label="click me!" onClick=handleClick/>