alibaba / react-intl-universal

Internationalize React apps. Not only for Component but also for Vanilla JS.
1.34k stars 154 forks source link

Words defined before intl initialization cannot be internationalized #195

Closed grantCHG closed 2 years ago

cwtuan commented 2 years ago

It's by design because react-intl-universal could be used in non-react component.

If you declare constants before intl.init(...), the intl.get method is executed when js script loaded. So it can't get the locale data.

const myData = {
  label: intl.get('key1').d('message'), 
  value: 1
}

const MyComp = () => {
   return <div>{myData.label}</div>   
}

Changing constants to function will work:

const myData = {
  get label() {return intl.get('key1').d('message')}
  value: 1
}

const MyComp = () => {
   return <div>{myData.label}</div>   
}