How to get string in react-intl internationalization

background

react+react-intl do internationalization

question

how can I get the current language file as quickly as possible by passing the title string to Native through jsbridge?

1. Initialize

as follows
<IntlProvider
      locale={chooseLocale()}
      messages={chooseLocaleFile()}>
            <App />    
</IntlProvider>

2. Rendering DOM has no pressure

< FormattedMessage id="section_title" defaultMessage="name" / >

3. But what about passing strings directly through JS?
the following example, how can "Zhang San" be obtained from localeFile?

window.location.href = "JSBridge://alert/?message=" + "";

ask the Great God for an answer!

Apr.19,2022

using injectintl, you can inject intl objects to get the messages configuration.
for example: export default injectIntl (MyComponent);

class MyComponent extend Components {    
   const {messages }=this.props.intl; 
   }

https://github.com/yahoo/reac...


give me a concise method!

// 
import { injectIntl } from 'react-intl';

export function language(target) {
  return injectIntl(target);
}

// 
@language
class MyComponent extend Components {    
    componentDidMount() {
        const {messages }=this.props.intl; 
        console.log(messages);
    }
    render() {
        return ...
    }
}

this confusion is also encountered in the development, which has been solved ;

sharing solution:
in fact, it is not necessary for us to acquire strings through react-intl . react-intl is internationalized during the rendering phase of DOM nodes, which depends on the messages parameters of < IntlProvider / > nodes.

while our business code is performed before or after DOM node rendering (usually after), so there is no need to call react-intl at this point, but to get the corresponding value of messages !

that is, en_US.js , zh_CN.js and other files in the i18n file;
these files are all exported a JSON object, so the business code can be written like this:

import en_US from "./i18n/en_US";
import zh_CN from "./i18n/zh_CN";
//
let { lang } = this.state;
let name = Object.is(lang, "en")?en_US.name:zh_CN.name;
window.location.href = `JSBridge://alert/?message=${name}`;

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b434d1-2bed5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b434d1-2bed5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?