Compile-time issues with React tutorial #537
Copy link
Copy link
Open
Labels
Description
In https://github.com/projectfluent/fluent.js/wiki/React-Tutorial, I had to make the following adjustments to the final generateBundles function to get it to compile:
Current:
// Generate bundles for each locale.
async function generateBundles() {
return languages.map(locale => {
const translations = await getMessages(locale);
const bundle = new FluentBundle(locale);
bundle.addMessages(translations);
return bundle;
});
}Fixed:
// Generate bundles for each locale.
async function generateBundles() {
return languages.map(async (locale) => {
const translations = await getMessages(locale);
const bundle = new FluentBundle(locale);
bundle.addResource(new FluentResource(translations));
return bundle;
});
}At first I was getting Unexpected reserved word 'await' for getMessages; I fixed that by marking the map call as async. I then had to update addMessages to addResource because addMessages doesn't exist.
Reactions are currently unavailable
Activity
sandalwoodbox commentedon Apr 3, 2021
This is not a compile-time error, but the tutorial also leaves me with:
It looks like I need to find a way to merge the final tutorial output with the ReactLocalization usage here: https://github.com/projectfluent/fluent.js/wiki/React-Bindings
sandalwoodbox commentedon Apr 3, 2021
It looks like the
ReactLocalizationclass requires synchronicity, since it only supports CachedSyncIterable / mapBundleSync: https://github.com/projectfluent/fluent.js/blob/master/fluent-react/src/localization.ts#L25sandalwoodbox commentedon Apr 3, 2021
If anyone else runs into this, I was able to work around it by wrapping LocalizationProvider in a component that handles loading the bundles asynchronously.
ManuelTS commentedon Feb 7, 2022
@sandalwoodbox thanks for you code, it helped a lot! I had to remove
staticin your URL to make it work. @Pike or @stasm, I did the setup in Typescript within myAppcomponent and I think its parts or the whole below code would fit nicely into the following wiki pages:Here the TS code:
I tried to find how I can update the wiki pages with the
gh-pagesbranch, but editing the raw HTML seams to be an overhead. Is there another way?