我开始在react-native中进行代码,并且在尝试使用reactlocalization库时,我会收到以下错误代码:
undefined is not an object (evaluating 'localization.language')
<unknown>
LocalizedStrings.js:17
loadModuleImplementation
require.js:122
guardedLoadModule
require.js:65
_require
require.js:49
<unknown>
localizedStrings.js:3
loadModuleImplementation
require.js:122
guardedLoadModule
require.js:65
_require
require.js:49
<unknown>
myWebView.js:25
loadModuleImplementation
require.js:122
guardedLoadModule
require.js:65
_require
require.js:49
<unknown>
app.js:30
loadModuleImplementation
require.js:122
guardedLoadModule
require.js:65
_require
require.js:49
<unknown>
index.js:15
loadModuleImplementation
require.js:122
guardedLoadModule
require.js:65
_require
require.js:49
<unknown>
index.android.js:11
loadModuleImplementation
require.js:122
guardedLoadModule
require.js:58
_require
require.js:49
global code
require-0.js:1
我试图进行研究,但是由于框架是新的,因此周围没有很多信息。
预先感谢您!
编辑:这是类代码
'use strict';
/**
* Simple module to localize the React interface using the same syntax
* used in the ReactNativeLocalization module
* (https://github.com/stefalda/ReactNativeLocalization)
*
* Originally developed by Stefano Falda (stefano.falda@gmail.com)
*
* It uses a call to the Navigator/Browser object to get the current interface language,
* then display the correct language strings or the default language (the first
* one if a match is not found).
*
* How to use:
* Check the instructions at:
* https://github.com/stefalda/react-localization
*/
export default class LocalizedStrings {
_getBestMatchingLanguage(language, props) {
//If an object with the passed language key exists return it
if (props[language]) return language;
//if the string is composed try to find a match with only the first language identifiers (en-US --> en)
var idx = language.indexOf("-");
if (idx >= 0) {
language = language.substring(0, idx);
if (props[language]) return language;
}
//Return the default language (the first coded)
return Object.keys(props)[0];
}
constructor(props) {
this.interfaceLanguage = (typeof navigator !== 'undefined' && navigator.languages && typeof navigator.languages !== 'undefined' && navigator.languages[0] && typeof navigator.languages[0] !== 'undefined') ? navigator.languages[0] :
((typeof navigator !== 'undefined' && navigator.language && typeof navigator.language !== 'undefined') ? navigator.language :
((typeof navigator !== 'undefined' && navigator.userLanguage && typeof navigator.userLanguage !== 'undefined') ? navigator.userLanguage :
'en-US'));
//Store locally the passed strings
this.props = props;
this.defaultLanguage = Object.keys(props)[0];
//Set language to its default value (the interface)
this.setLanguage(this.interfaceLanguage);
}
//Can be used from ouside the class to force a particular language
//independently from the interface one
setLanguage(language) {
//Check if a translation exists for the current language or if the default
//should be used
var bestLanguage = this._getBestMatchingLanguage(language, this.props);
this.language = bestLanguage;
//Associate the language object to the this object
if (this.props[bestLanguage]) {
//console.log("There are strings for the language:"+language);
//Merge default
var localizedStrings = {...this.props[this.defaultLanguage], ...this.props[this.language] };
for (var key in localizedStrings) {
//console.log("Checking property:"+key);
if (localizedStrings.hasOwnProperty(key)) {
//console.log("Associating property:"+key);
this[key] = localizedStrings[key];
}
}
}
}
//The current language displayed (could differ from the interface language
// if it has been forced manually and a matching translation has been found)
getLanguage() {
return this.language;
}
//The current interface language (could differ from the language displayed)
getInterfaceLanguage() {
return this.interfaceLanguage;
}
//Return an array containing the available languages passed as props in the constructor
getAvailableLanguages() {
if (!this.availableLanguages) {
this.availableLanguages = [];
for (let language in this.props) {
this.availableLanguages.push(language);
}
}
return this.availableLanguages;
}
//Format the passed string replacing the numbered placeholders
//i.e. I'd like some {0} and {1}, or just {0}
//Use example:
// strings.formatString(strings.question, strings.bread, strings.butter)
formatString(str, ...values) {
var res = str;
for (let i = 0; i < values.length; i++) {
res = this._replaceAll("{" + i + "}", values[i], res);
}
return res;
}
//Return a string with the passed key in a different language
getString(key, language) {
try {
return this.props[language][key];
} catch (ex) {
console.log("No localization found for key " + key + " and language " + language);
}
return null;
}
//Replace all occorrencies of a string in another using RegExp
_replaceAll(find, replace, str) {
//Escape find
find = find.replace(/([.*+?^=!:${}()|[]/\])/g, "\$1");
return str.replace(new RegExp(find, 'g'), replace);
}
}
使用此代码更改上述代码:
https://github.com/stefalda/react-localization/blob/master/src/localizedstrings.js
错误消失了。