You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This just started with Angular 10 and 11 (this particular version is 11.0.1). It runs fine in development mode but when running in production mode the translations are not getting loaded. I think the basic indicator is when subscribing to translation.onError an error is displayed "Translation Error TypeError: n.handle(...).do is not a function". Extremely difficult to debug in production mode with no ability to set breakpoints in the code. I hope I am providing enough info. If not, let me know. Thanks for your help.
import { Injectable, Inject, Optional } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import {
L10nConfig,
L10nLoader,
L10nStorage,
L10nLocale,
L10nTranslationLoader,
L10nProvider,
L10nValidation,
L10N_LOCALE,
L10nNumberFormatOptions,
L10nDateTimeFormatOptions,
parseDigits, L10nUserLanguage
} from 'angular-l10n';
export const l10nConfig: L10nConfig = {
format: 'language-region',
providers: [
{ name: 'app', asset: './assets/i18n/app', options: { version: '9.0.0' } }
],
fallback: false,
cache: true,
keySeparator: '.',
defaultLocale: { language: 'en-US', currency: 'USD' },
schema: [
{ locale: { language: 'en-US', currency: 'USD' }, dir: 'ltr', text: 'United States' },
],
defaultRouting: true
};
export function initL10n(l10nLoader: L10nLoader): () => Promise<void> {
return () => l10nLoader.init();
}
@Injectable() export class AppStorage implements L10nStorage {
private hasStorage: boolean;
constructor() {
this.hasStorage = typeof Storage !== 'undefined';
}
public async read(): Promise<L10nLocale | null> {
console.log('AppStorage read');
if (this.hasStorage) {
console.log('AppStorage has storage');
return Promise.resolve(JSON.parse(sessionStorage.getItem('locale')));
}
return Promise.resolve(null);
}
public async write(locale: L10nLocale): Promise<void> {
console.log('AppStorage write');
if (this.hasStorage) {
sessionStorage.setItem('locale', JSON.stringify(locale));
}
}
}
@Injectable() export class HttpTranslationLoader implements L10nTranslationLoader {
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
constructor(@Optional() private http: HttpClient) { }
public get(language: string, provider: L10nProvider): Observable<{ [key: string]: any }> {
console.log('HttpTranslationLoader');
const url = `${provider.asset}-${language}.json`;
const options = {
headers: this.headers,
params: new HttpParams().set('v', provider.options.version)
};
return this.http.get(url, options);
}
}
@Injectable() export class LocaleValidation implements L10nValidation {
constructor(@Inject(L10N_LOCALE) private locale: L10nLocale) { }
public parseNumber(value: string, options?: L10nNumberFormatOptions, language = this.locale.language): number | null {
if (value === '' || value == null) { return null };
let format = { minimumIntegerDigits: 1, minimumFractionDigits: 0, maximumFractionDigits: 0 };
if (options && options.digits) {
format = { ...format, ...parseDigits(options.digits) };
}
let decimalSeparator: string;
switch (language) {
case 'it-IT':
decimalSeparator = ',';
break;
default:
decimalSeparator = '.';
}
const pattern = `^-?[\\d]{${format.minimumIntegerDigits},}(\\${decimalSeparator}[\\d]{${format.minimumFractionDigits},${format.maximumFractionDigits}})?$`;
const regex = new RegExp(pattern);
return regex.test(value) ? parseFloat(value.replace(decimalSeparator, '.')) : null;
}
public parseDate(value: string, options?: L10nDateTimeFormatOptions, language = this.locale.language): Date | null {
return null;
}
}
@Injectable() export class UserLanguage implements L10nUserLanguage {
public get(): Promise<string | null> {
let browserLanguage = null;
if (navigator !== undefined && navigator.language) {
// Takes the complete locale, not only the language as in the the default version
console.log('UserLanguage ' + navigator.language);
browserLanguage = navigator.language;
}
return Promise.resolve(browserLanguage);
}
}
This just started with Angular 10 and 11 (this particular version is 11.0.1). It runs fine in development mode but when running in production mode the translations are not getting loaded. I think the basic indicator is when subscribing to translation.onError an error is displayed "Translation Error TypeError: n.handle(...).do is not a function". Extremely difficult to debug in production mode with no ability to set breakpoints in the code. I hope I am providing enough info. If not, let me know. Thanks for your help.
app.module code imports
app.module code providers
app.component
Firefox console log
The text was updated successfully, but these errors were encountered: