-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compile-time issues with React tutorial #537
Comments
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 |
It looks like the |
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. import React from "react";
import "intl-pluralrules";
import { negotiateLanguages } from "@fluent/langneg";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { LocalizationProvider, ReactLocalization } from "@fluent/react";
import PropTypes from "prop-types";
const AVAILABLE_LOCALES = ["en-US", "zh-CN"];
// Negotiate user language.
const languages = negotiateLanguages(navigator.languages, AVAILABLE_LOCALES, {
defaultLocale: "en-US",
});
// Load locales from files.
async function getMessages(locale) {
const url = `/static/locale/${locale}/content.ftl`;
const response = await fetch(url);
return await response.text();
}
// 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;
});
}
class BundleLoader extends React.Component {
constructor(props) {
super(props);
this.state = {
l10n: new ReactLocalization([]),
};
}
componentDidMount() {
generateBundles().then((bundlePromises) => {
Promise.all(bundlePromises).then((bundles) => {
console.log(bundles)
this.setState({ l10n: new ReactLocalization(bundles) });
})
});
}
render() {
return (
<LocalizationProvider l10n={this.state.l10n}>
{this.props.children}
</LocalizationProvider>
);
}
}
BundleLoader.propTypes = {
children: PropTypes.element.isRequired,
};
export default BundleLoader; |
@sandalwoodbox thanks for you code, it helped a lot! I had to remove
Here the TS code:
I tried to find how I can update the wiki pages with the |
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:
Fixed:
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 updateaddMessages
toaddResource
becauseaddMessages
doesn't exist.The text was updated successfully, but these errors were encountered: