Created
April 19, 2019 21:15
-
-
Save briankung/3f38b5dffb71e745dc93ebd7cd9ecca6 to your computer and use it in GitHub Desktop.
fluent-react hello world example (stolen wholesale from https://github.com/projectfluent/fluent.js/blob/master/fluent-react/examples/hello-world/src/)
This file contains hidden or 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
import { FluentBundle } from 'fluent/compat'; | |
import { negotiateLanguages } from 'fluent-langneg/compat'; | |
const MESSAGES_ALL = { | |
'pl': ` | |
title = Witaj świecie! | |
today-is = Dziś jest { DATETIME($date, month: "long", day: "numeric") }. | |
`, | |
'en-US': ` | |
title = Hello, world! | |
today-is = Today is { DATETIME($date, month: "long", day: "numeric") }. | |
`, | |
}; | |
export function* generateBundles(userLocales) { | |
// Choose locales that are best for the user. | |
const currentLocales = negotiateLanguages( | |
userLocales, | |
['en-US', 'pl'], | |
{ defaultLocale: 'en-US' } | |
); | |
for (const locale of currentLocales) { | |
const bundle = new FluentBundle(locale); | |
bundle.addMessages(MESSAGES_ALL[locale]); | |
yield bundle; | |
} | |
} |
This file contains hidden or 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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { LocalizationProvider } from 'fluent-react/compat'; | |
import { generateBundles } from './l10n'; | |
import App from './App'; | |
ReactDOM.render( | |
<LocalizationProvider bundles={generateBundles(navigator.languages)}> | |
<App /> | |
</LocalizationProvider>, | |
document.getElementById('root') | |
); |
This file contains hidden or 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
import React from 'react'; | |
import { Localized } from 'fluent-react/compat'; | |
export default function App() { | |
return ( | |
<div> | |
<Localized id="title"> | |
<h1>Hello, world!</h1> | |
</Localized> | |
<Localized id='today-is' $date={new Date()}> | |
<p> | |
{'Today is { DATETIME($date, month: "long", day: "numeric") }.'} | |
</p> | |
</Localized> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment