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
{{ message }}
Repository navigation
Update i18n doc with loadTranslations and $localize #37563
Reacted by Hugo Des, Front-end developer, Zack Yang, Trevor Karjanis, Ronaldo Ribeiro, martijn-van-den-bergh, Alex Okrushko, Valentin, Jónatan Núñez, Jonas Schramm and 2 more
What I'm having a really hard time to understand is if it's ok to use $localize.locale or if we are supposed to keep using the injected LOCALE_ID since it's never mentioned anywhere.
I think soluing (no affiliation) has a pretty good doc on a i18n and a few $localize functionalities. But I would prefer an official doc to know what's actually a supported functionality.
$localize.locale is only available if you are using runtime translation of $localize. So if you are using compile time inlining of translations then $localize.localize will not exist. You should be able to rely upon the LOCALE_ID injectable. The Angular CLI automatically includes the locale data and sets the LOCALE_ID value when you use the --localize option with ng build.
@petebacondarwin At least in Angular 10.0 $localzie.locale works during compile time translation as it gets replaced with a string of the locale (e.g. "de").
It's not accessible during runtime with ng serve for whatever reason ($localize has a function value), but with ng build you can also access it during runtime.
This is what I meant when I said, the documentation is lacking and the usage is confusing 😅
It doesn't work on stackblitz (I guess they are using some special build configurations) but when you run it locally via ng serve or ng build (even with AOT) you get the right values for $localize.locale in the .ts files, but it fails in the template files. But even then you can just set it to a property from the code and it works, so it's not that different to using LOCALE_ID.
So is this an official function and legitimate use case or not?
You cannot access any globals in templates (except for null) and so it is expected that {{ $localize.locale }} would not work. It is not related to $localize itself.
You are basically right in your first description of this issue. This whole area is not documented well (or at all).
Hopefully #40317 will begin to resolve this. But we do need to add more to the guides.
Activity
stefan-schweiger commentedon Feb 11, 2021
What I'm having a really hard time to understand is if it's ok to use
$localize.localeor if we are supposed to keep using the injectedLOCALE_IDsince it's never mentioned anywhere.I think soluing (no affiliation) has a pretty good doc on a i18n and a few $localize functionalities. But I would prefer an official doc to know what's actually a supported functionality.
petebacondarwin commentedon Feb 11, 2021
$localize.localeis only available if you are using runtime translation of $localize. So if you are using compile time inlining of translations then$localize.localizewill not exist. You should be able to rely upon theLOCALE_IDinjectable. The Angular CLI automatically includes the locale data and sets the LOCALE_ID value when you use the --localize option with ng build.stefan-schweiger commentedon Feb 11, 2021
@petebacondarwin At least in Angular 10.0 $localzie.locale works during compile time translation as it gets replaced with a string of the locale (e.g. "de").
It's not accessible during runtime with
ng servefor whatever reason ($localize has a function value), but withng buildyou can also access it during runtime.This is what I meant when I said, the documentation is lacking and the usage is confusing 😅
stefan-schweiger commentedon Feb 12, 2021
@petebacondarwin it's also working with Angular 11 as you can see in this repro https://github.com/stefan-schweiger/angular-localize-test
It doesn't work on stackblitz (I guess they are using some special build configurations) but when you run it locally via
ng serveorng build(even with AOT) you get the right values for$localize.localein the .ts files, but it fails in the template files. But even then you can just set it to a property from the code and it works, so it's not that different to usingLOCALE_ID.So is this an official function and legitimate use case or not?
petebacondarwin commentedon Feb 12, 2021
Oh yes! I have forgotten that the CLI also does this.
petebacondarwin commentedon Feb 12, 2021
You cannot access any globals in templates (except for
null) and so it is expected that{{ $localize.locale }}would not work. It is not related to$localizeitself.petebacondarwin commentedon Feb 12, 2021
You are basically right in your first description of this issue. This whole area is not documented well (or at all).
Hopefully #40317 will begin to resolve this. But we do need to add more to the guides.
michaelfaith commentedon Jul 24, 2021
Would love to have more documentation on this. Especially handling runtime localization and about building libraries with translations.
jessicajaniuk commentedon Dec 13, 2021
Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular version.
If the problem still exists in your application, please open a new issue and follow the instructions in the issue template.
angular-automatic-lock-bot commentedon Jan 13, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.