-
Notifications
You must be signed in to change notification settings - Fork 27.7k
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
Exporting localised sites #2665
Comments
Since its static shouldn't you need both files to be exported during the export? So you need to not dynamically import them. Maybe something like this below? import en from 'locales/en.json'
import fr from 'locales/fr.json'
var selectedLocale = en
if (locale === ' en') selectedLocale = fr (Or with require) Apology if this isn't helpful. But I think you'd need both imported and ready to be exported and not dynamically. |
Hey @wagerfield Here's a bit modified version what @Kalcode mentioned which includes dynamic imports. // pages/index.js
import { Component } from 'react'
const Locales = {
'en': import('../static/locales/en.json'),
'fr': import('../static/locales/fr.json'),
}
class Index extends Component {
static async getInitialProps({ req, query }) {
const { locale = 'en' } = query
const data = await Locales[locale]
return { data }
}
render() {
return <h1>{this.props.data.hello}</h1>
}
}
export default Index This will work everywhere. |
Closing since this works. |
This is great, thanks very much! For anyone else wanting to see a working example of this, please take a look at: |
This is great, but I have since found an issue, I wish to construct the import path from data passed into the query
but this blows up with the following cryptic error |
I am trying to
export
a localised site whereby the translations for the site exist in JSON files within thestatic
directory like so:I have created a very simple example repository that demonstrates my approach using
isomorphic-fetch
within thegetInitialProps
method of a page:https://github.com/wagerfield/next-fetch-from-static
As you will see, I am simply loading the locale JSON file based on a
locale
query param.This approach works when running a node server both in dev and production, however when I come to
export
the site statically I run into the problem of not being able to load the JSON files since there is no server running to host these files.In addition to using
fetch
I have also tried dynamically requiring the JSON files usingeval('require('path/to/dynamic-locale.json')')
. This approach is also included in the example repository I posted above (though the implementation is commented out in the code).Any help on this would be much appreciated!
The text was updated successfully, but these errors were encountered: