6

I am trying to deploy an Angular 7 app that uses ngx-translate and translated language files in the /dist/assets folder. I specified the correct base href in build. After deployment, I see everything loads, except the languages file which returns an error code 404 (not found).

Ive tried changing the angular.json file a few different ways. I tried changing the TranslateHttpLoader. Nothing seems to be working. I can see the i18n folder with all the language files in the /dist folder. However its not being referenced.

in app.module.ts

export function HttpLoaderFactory(httpClient: HttpClient) {
  return new TranslateHttpLoader(httpClient, './assets/i18n/', '.json');
}

in angular.json

"assets": [
  "src/favicon.ico",
  "src/assets"
],

But i end up getting this error in the browser:

/assets/i18n/en.json 404 not found

Any help would be appreciated!

npabs18
  • 157
  • 2
  • 9

5 Answers5

9

On Angular 9.0.4 I faced the same issue.

Only when I had a dot slash prefix in the path as ./assets/i18n/ would the issue be resolved:

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/');
}
Stephane
  • 10,017
  • 21
  • 96
  • 152
  • TranslateHttpLoader's default prefix is '/assets/i18n/', making this a required step when using a different baseHref. https://github.com/ngx-translate/http-loader#usage – Carsten Sep 09 '21 at 12:18
4

I faced the same issue like you, In my case it worked PERFECTLY fine in my local IIS server, but it throws 404 for the live IIS server (i18n/en.json not found 404).

I went round and round, thinking it is a angular i18n path error. In the end it's boils down to a server problem.

by default IIS server will allows only few file extensions type. You have to manually add mimeType for json file (From a web config file).

<staticContent>
  <remove fileExtension=".json"/>
  <mimeMap fileExtension=".json" mimeType="application/json"/>
</staticContent>

I added this to a web config file (If file isn't there you have to create it in website root folder).

ur welcome...

gamal
  • 1,487
  • 2
  • 20
  • 39
  • Helped me too, was using the azure web app, tried every other option. Only this worked for me. – Alex Jan 08 '21 at 17:33
3

I faced the same issue like you, I believe that it is related to this command:

ng build --prod

Try to use this command:

ng build --base-href=/deployed_app_name/ --prod

For example if your deployed application name is: stackoverflow the command should be like this:

ng build --base-href=/stackoverflow/ --prod

And here you will have the correct path to your i18n json files

Nikaido
  • 4,004
  • 5
  • 32
  • 42
2

Have you imported into the AppModule ?

export function HttpLoaderFactory(httpClient: HttpClient) {
    return new TranslateHttpLoader(httpClient, './assets/i18n/', '.json');
}

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient]
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
Tan Nguyen
  • 169
  • 1
  • 3
  • Thanks for the response. Yes, I had imported into the AppModule. Just like you have shown in your snippet. Still not working. – npabs18 May 24 '19 at 03:18
  • Make sure your assets folder is at this level: _/src/assets/_ Are you use angular-cli ? – Tan Nguyen May 24 '19 at 03:42
  • Yes i am using the Angular-CLI. When i run ng build --prod, the dist folder creates the build with the assets folder at /dist//assets. Am i not supposed to deploy the app structure this way? The app before i build has the assets folder located in /src/assets like you mentioned – npabs18 May 24 '19 at 14:50
0

Here is the path I went to solve a similar problem:

  1. Check if the files with 404 error were in dist folder correct path, if not, confirm if all assets parameters are ok in angular.json.
  2. Verify base-ref parameter as:
  3. And look if you have a correct value in i18n parameter of environment.prod.ts (or environment.ts, etc.)
sikmowe
  • 35
  • 6