Skip to content
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

Closed
wagerfield opened this issue Jul 27, 2017 · 5 comments
Closed

Exporting localised sites #2665

wagerfield opened this issue Jul 27, 2017 · 5 comments

Comments

@wagerfield
Copy link

wagerfield commented Jul 27, 2017

I am trying to export a localised site whereby the translations for the site exist in JSON files within the static directory like so:

static
- locales
  - en.json
  - fr.json

I have created a very simple example repository that demonstrates my approach using isomorphic-fetch within the getInitialProps 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 using eval('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!

@Kalcode
Copy link

Kalcode commented Jul 27, 2017

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.

@arunoda
Copy link
Contributor

arunoda commented Jul 28, 2017

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.

@arunoda
Copy link
Contributor

arunoda commented Jul 28, 2017

Closing since this works.

@arunoda arunoda closed this as completed Jul 28, 2017
@wagerfield
Copy link
Author

wagerfield commented Jul 28, 2017

This is great, thanks very much!

For anyone else wanting to see a working example of this, please take a look at:

https://github.com/wagerfield/next-fetch-from-static

@sammdec
Copy link

sammdec commented Aug 17, 2017

This is great, but I have since found an issue, I wish to construct the import path from data passed into the query

setStatic('getInitialProps', async ({ query: { locale } }) => {
    const data = await import (`../data/${locale}`)
    return data 
  })

but this blows up with the following cryptic error Cannot read property '0' of undefined, I was under the impression this should work?

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants