I have created a custom loader for ngx-translate within an Ionic (v4) app. I have done this because I want to download the latest translation files from a server and then save into the data directory so they are accessible when the app is offline.
I have succeeded with this part. However, I am now experiencing issues with CORS within my getTranslation function.
How can I get around this issue?
getTranslation(lang: string): Observable<any> {
const subDirectory = 'i18n';
const fileName = lang;
const fileExtension = '.json';
let rootDirectory = '';
let rootDirectoryPath = '';
let directory = '';
let directoryPath = '';
if (this.platform.is('cordova')) {
this.platform.ready()
.then(() => {
rootDirectory = this.file.dataDirectory;
rootDirectoryPath = rootDirectory;
directory = rootDirectoryPath + subDirectory;
directoryPath = directory + '/';
console.log(directoryPath + fileName + fileExtension + ':');
this.http
.get(directoryPath + fileName + fileExtension)
.pipe(map((res: any) => res))
.subscribe(translationData => {
console.log(translationData);
});
const translation = this.http.get(directoryPath + fileName + fileExtension);
return translation;
});
} else {
rootDirectory = '/assets/data';
rootDirectoryPath = rootDirectory + '/';
directory = rootDirectoryPath + subDirectory;
directoryPath = directory + '/';
const translation = this.http.get(directoryPath + fileName + fileExtension);
return translation;
}
}