[Question]: Registering custom locale data file for L10N (Dates) #586
Unanswered
gunnarburmeister
asked this question in
Q&A
Replies: 1 comment
-
Ok, in the end I resorted to using a custom transformer with a little bit of mapping and then using import { formatDate } from '@angular/common';
import { DateUtils } from '@core/utility';
import { DateFormatOptions, TranslocoDateTransformer } from '@ngneat/transloco-locale';
export class CustomDateTransformer implements TranslocoDateTransformer {
transform(date: Date, locale: string, options: DateFormatOptions): string {
if (DateUtils.isDate(date)) {
let format: string = 'mediumDate';
if(options.dateStyle && options.timeStyle) format = options.dateStyle;
else if(options.dateStyle) format = `${options.dateStyle}Date`;
else if(options.timeStyle) format = `${options.timeStyle}Time`;
let timeZone: string | undefined = options.timeZone === 'UTC' ? 'UTC' : undefined;
return formatDate(date, format, locale, timeZone);
}
return '';
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to support a couple different locales in my project (only for l10n, i18n would use the same base language), one of them actually being basically a custom one. I register them using
registerLocalData
. Currently I have a custom pipe to localize dates (it pulls the language from my language settings service which also feeds into TranslocoService/TranslocoLocaleService), but I would like to switch to usingtranslocoDate
pipe.The problem I'm facing is that I can't seem to get the
TranslocoLocaleService
to pick up the custom locale data.For testing I changed them to be distinguishable:
Using my
localeDate
pipe vstranslocoDate
pipe:It's not even consistent for the
en-DE
(which doesn't really exist, so it should fall back toen-US
I thought).Is it not possible to provide custom locale data with
registerLocalData
fortranslocoDate
etc?I can add more implementation details if needed, but I left them out for now since my pipe is working so the locale data should be loaded correctly.
Beta Was this translation helpful? Give feedback.
All reactions