Q:

Reading a local JSON file from Angular/Nativescript

I have tried placing an en.json file in the following locations:
src/assets/i18n/en.json
src/app/assets/i18n/en.json

And I then try to read the file like this:
import {knownFolders, File} from 'tns-core-modules/file-system';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs';

export class TranslateFileLoader implements TranslateLoader {
    constructor(public prefix: string = 'assets/i18n/', public suffix: string = '.json') {}

    public getTranslation(lang: string): Observable<Object> {
      const appFolder = knownFolders.currentApp(); // Havet tried with knownFolders.documents() also
      console.log(`${this.prefix}${lang}${this.suffix}`);
      const cfgFile = appFolder.getFile(`${this.prefix}${lang}${this.suffix}`);
      console.log(File.exists(cfgFile.path));
      console.log(cfgFile.path);
      const fileData = cfgFile.readTextSync();
      console.log(fileData);
      return JSON.parse(fileData);
    }
}

console.log #1: assets/i18n/en.json console.log #2: true
  console.log #3:
  /data/data/org.nativescript.playground/files/app/assets/i18n/en.json
  console.log #4:

So yeah, the path seems good, the File.exists(cfgFile.path) returns true, but the fileData is empty and thus I get a exception when I JSON.parse(fileData). But why? First I thought that the files where not there, but I get true from my exists test. So it seems that the file is there when the device/emulator is running, but I can not read its content?
Any ideas?
Thank you
Søren

A:

I fixed it! HttpClient does not work on Native (yes my problem was with Nativescript on native devices, where there are no http server serving the files, and yes there I do have access to local storage). My code worked, but the *.json files was not transferred to the build packages. I had to add those in mt webpack.config.js in the tns part of "CopyWebpackPlugin", then it all worked :) I could not thrust the "File.exists" because my call to ".getFile" created a empty file.

