fix(core): move generated i18n statements to the consts
field of ComponentDef (#38404)
This commit updates the code to move generated i18n statements into the `consts` field of ComponentDef to avoid invoking `$localize` function before component initialization (to better support runtime translations) and also avoid problems with lazy-loading when i18n defs may not be present in a chunk where it's referenced. Prior to this change the i18n statements were generated at the top leve: ``` var I18N_0; if (typeof ngI18nClosureMode !== "undefined" && ngI18nClosureMode) { var MSG_X = goog.getMsg(“…”); I18N_0 = MSG_X; } else { I18N_0 = $localize('...'); } defineComponent({ // ... template: function App_Template(rf, ctx) { i0.ɵɵi18n(2, I18N_0); } }); ``` This commit updates the logic to generate the following code instead: ``` defineComponent({ // ... consts: function() { var I18N_0; if (typeof ngI18nClosureMode !== "undefined" && ngI18nClosureMode) { var MSG_X = goog.getMsg(“…”); I18N_0 = MSG_X; } else { I18N_0 = $localize('...'); } return [ I18N_0 ]; }, template: function App_Template(rf, ctx) { i0.ɵɵi18n(2, 0); } }); ``` Also note that i18n template instructions now refer to the `consts` array using an index (similar to other template instructions). PR Close #38404
This commit is contained in:

committed by
Andrew Scott

parent
5f90b64328
commit
cb05c0102f
@ -255,9 +255,24 @@ export type TAttributes = (string|AttributeMarker|CssSelector)[];
|
||||
* Constants that are associated with a view. Includes:
|
||||
* - Attribute arrays.
|
||||
* - Local definition arrays.
|
||||
* - Translated messages (i18n).
|
||||
*/
|
||||
export type TConstants = (TAttributes|string)[];
|
||||
|
||||
/**
|
||||
* Factory function that returns an array of consts. Consts can be represented as a function in case
|
||||
* any additional statements are required to define consts in the list. An example is i18n where
|
||||
* additional i18n calls are generated, which should be executed when consts are requested for the
|
||||
* first time.
|
||||
*/
|
||||
export type TConstantsFactory = () => TConstants;
|
||||
|
||||
/**
|
||||
* TConstants type that describes how the `consts` field is generated on ComponentDef: it can be
|
||||
* either an array or a factory function that returns that array.
|
||||
*/
|
||||
export type TConstantsOrFactory = TConstants|TConstantsFactory;
|
||||
|
||||
/**
|
||||
* Binding data (flyweight) for a particular node that is shared between all templates
|
||||
* of a specific type.
|
||||
|
Reference in New Issue
Block a user