Cor*_*ean 8 i18next react-native react-i18next react-component
我在 React Native 应用程序中使用 i18next 和 react-i18next ,更具体地说是输入验证。我试图将 t() 作为 no 组件中的参数传递,但收到错误“TypeError: n 不是函数。(在 'n('errorMessages.emailNoMatch')' 中,'n' 未定义) ”。非常感谢任何建议,因为我对编码相对较新,并且我已经为此问题搜索了几天。
这是 i18n.js 代码:
import i18next from 'i18next';
import common_de from './translations/de/common.json';
import common_en from './translations/en/common.json';
import i18n from 'i18n-js';
import * as Localization from 'expo-localization';
// Set up translations
i18next.init({
interpolation: { escapeValue: false }, // React already does escaping
lng: 'en', // language to use
resources: {
de: {
common: common_de, // 'common' is our custom namespace
},
en: {
common: common_en,
},
},
debug: true,
});
export default i18next;
Run Code Online (Sandbox Code Playgroud)
RegisterValidator.js 代码的一部分,我尝试将 t() 作为参数传递,但它没有将 t 读取为 switch 语句内的参数。:
import React from 'react';
import { useTranslation } from 'react-i18next';
// import { withTranslation } from 'react-i18next';
const RegisterValidator = (type, value, email, t) => {
let validated = true;
let warningMsg = [];
switch (type) {
case 'username':
if (!value.includes(' ')) return [true, ''];
else {
warningMsg = t('errorMessages.username');
return [false, warningMsg];
}
default:
return [validated, ''];
}
};
export default RegisterValidator;
Run Code Online (Sandbox Code Playgroud)
这是 App.js 的一部分
import React from 'react';
import './i18n';
import i18n from 'i18n-js';
import i18next from 'i18next';
import { I18nextProvider } from 'react-i18next';
function App() {
return (
<I18nextProvider i18n={i18next}>
<Insert more code here />
</I18nextProvider>
);
}
export default App;
Run Code Online (Sandbox Code Playgroud)
小智 0
t() 外部组件将不起作用,请发送一个消息对象RegisterValidator
并使用它。
例子 -
function Component() {
// t is declared here
const message = {
error: t('errorMessage'),
success: t('successMessage')
}
// and then send this message object to RegisterValidator
RegisterValidator(.., .., .., message);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20232 次 |
最近记录: |