Skip to content

Instantly share code, notes, and snippets.

@briankung
Created April 19, 2019 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briankung/3f38b5dffb71e745dc93ebd7cd9ecca6 to your computer and use it in GitHub Desktop.
Save briankung/3f38b5dffb71e745dc93ebd7cd9ecca6 to your computer and use it in GitHub Desktop.
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;
}
}
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')
);
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