Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Ngx translate using environment to get Assets

I've already used ngx in another application and it was pretty cool. However in this application instead of getting in the default location localhost:4200/assets it keeps trying to get in the environment localhost:8080. I don't know why this is happening. In the other application it is also being using the environment

/*environment*/

export const environment = {
  production: false,
  baseUrl: 'http://localhost:8080',
};


/* APP MODULE */

export function HttpLoaderFactory(httpClient: HttpClient) {
  return new TranslateHttpLoader(httpClient);
}

    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      },
    })
/*home component*/

ngOnInit() {
  this.translate.setDefaultLang('en');
  this.translate.use('en');
}

```
auth.service

  {
  provide: HTTP_INTERCEPTORS,
  useClass: AuthInterceptor,
  multi: true,
},

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Clone the request to add the new authenticated header.
const authReq = req.clone({
  url: environment.baseUrl + req.url,
  headers: req.headers.set('Authorization', SessionService.getToken())
});

// Pass on the cloned request instead of the original request.
return next.handle(authReq)
  .pipe(
    tap(event => {}, err => {
      // If unauthorized request
      if (err instanceof HttpErrorResponse && (err.status === 401 || err.status === 403)) {
        this.loginService.logout();
      }
    })
  );
  }
```

localhost:8080 and does the get in the normal localhost:4200. Could you help me with this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try the following in your HttpLoaderFactory:

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

In case you're using an interceptor (e.g. AuthInterceptor), you should force it to handle the requests start with baseUrl only, and ignore the local ones, like the following:

intercept(
  request: HttpRequest<unknown>,
  next: HttpHandler
): Observable<HttpEvent<any>> {
  if (request.url.startsWith(environment.baseUrl)) {
    // Add your logic here
    // e.g. inject the token and next.handle the request again
  } else {
    return next.handle(request);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda