I made some more progress on this. I have changed the function to:
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 + ':');
const translationFilePath = this.webview.convertFileSrc(directoryPath + fileName + fileExtension);
// return this.http.get(translationFilePath);
return this.http.get(translationFilePath)
.pipe(map((res: JSON) => {
return res;
}));
});
} else {
rootDirectory = '/assets/data';
rootDirectoryPath = rootDirectory + '/';
directory = rootDirectoryPath + subDirectory;
directoryPath = directory + '/';
console.log(directoryPath + fileName + fileExtension + ':');
return this.http.get(directoryPath + fileName + fileExtension);
}
}
So now I have resolved the CORS issue by using WebView to convert the file source.
Now the only issue I have is that the Observable returned is obviously not understood by ngx-translate. So I have created a StackOverflow post.