When I manage to fix it, I'll be home. The current issue is saving the result from an async function. If I do a console.log after that function, I receive the translated value. However, a few moments later when I check how my verbs look like after being packed into an object, I receive the non-translated value. Does anyone know why?
private translate(file: any) {
const keys = Object.keys(file)
const values = Object.values(file) // [!] - English Value at start, it's ok
values.forEach( async (value, index) => {
if( typeof(value) === 'object') {
value = this.translate(value)
} else {
const translateResult = await this.GetTranslatedByResstring(keys[index], 1045).toPromise()
value = translateResult.getPhrase()
console.log(value) // [!] <- Polish Value, it's ok
}
})
const zipObj = xs => ys => xs.reduce( (obj, x, i) => ({ ...obj, [x]: ys[i] }), {})
const obj = zipObj (keys) (values)
console.log(obj) // [!] <- English Value, it's not ok, i need Polish value inside
return obj
}
#Update 1
The location where translate is called:
public async getTranslatedJson(
sourceFile: File,
originLanguage: Language,
destinatonLanguage: Language
): Promise<string> {
const file = await this.getFile(sourceFile)
const parsedFile = JSON.parse(file)
const translatedFile = this.translate(parsedFile)
return null
}
#Update 2
My getFile function:
private getFile(
sourceFile: File
): Promise<string> {
return new Promise((resolve) => {
const file = sourceFile[0]
const fileReader = new FileReader();
fileReader.onloadend = (e) => {
const testResult = fileReader.result.toString();
resolve(testResult);
}
fileReader.readAsText(file, "UTF-8");
});
}