feat(ivy): support i18n without closure (#28689)
So far using runtime i18n with ivy meant that you needed to use Closure and `goog.getMsg` (or a polyfill). This PR changes the compiler to output both closure & non-closure code, while the unused option will be tree-shaken by minifiers. This means that if you use the Angular CLI with ivy and load a translations file, you can use i18n and the application will not throw at runtime. For now it will not translate your application, but at least you can try ivy without having to remove all of your i18n code and configuration. PR Close #28689
This commit is contained in:

committed by
Igor Minar

parent
387fbb8106
commit
91c7b451d5
@ -6,6 +6,8 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import '../util/ng_i18n_closure_mode';
|
||||
|
||||
import {SRCSET_ATTRS, URI_ATTRS, VALID_ATTRS, VALID_ELEMENTS, getTemplateContent} from '../sanitization/html_sanitizer';
|
||||
import {InertBodyHelper} from '../sanitization/inert_body';
|
||||
import {_sanitizeUrl, sanitizeSrcset} from '../sanitization/url_sanitizer';
|
||||
@ -1604,3 +1606,38 @@ function parseNodes(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let TRANSLATIONS: {[key: string]: string} = {};
|
||||
export interface I18nLocalizeOptions { translations: {[key: string]: string}; }
|
||||
|
||||
/**
|
||||
* Set the configuration for `i18nLocalize`.
|
||||
*
|
||||
* @deprecated this method is temporary & should not be used as it will be removed soon
|
||||
*/
|
||||
export function i18nConfigureLocalize(options: I18nLocalizeOptions = {
|
||||
translations: {}
|
||||
}) {
|
||||
TRANSLATIONS = options.translations;
|
||||
}
|
||||
|
||||
const LOCALIZE_PH_REGEXP = /\{\$(.*?)\}/g;
|
||||
|
||||
/**
|
||||
* A goog.getMsg-like function for users that do not use Closure.
|
||||
*
|
||||
* This method is required as a *temporary* measure to prevent i18n tests from being blocked while
|
||||
* running outside of Closure Compiler. This method will not be needed once runtime translation
|
||||
* service support is introduced.
|
||||
*
|
||||
* @publicApi
|
||||
* @deprecated this method is temporary & should not be used as it will be removed soon
|
||||
*/
|
||||
export function Δi18nLocalize(input: string, placeholders: {[key: string]: string} = {}) {
|
||||
if (typeof TRANSLATIONS[input] !== 'undefined') { // to account for empty string
|
||||
input = TRANSLATIONS[input];
|
||||
}
|
||||
return Object.keys(placeholders).length ?
|
||||
input.replace(LOCALIZE_PH_REGEXP, (match, key) => placeholders[key] || '') :
|
||||
input;
|
||||
}
|
||||
|
@ -107,6 +107,8 @@ export {
|
||||
Δi18nEnd,
|
||||
Δi18nApply,
|
||||
Δi18nPostprocess,
|
||||
i18nConfigureLocalize,
|
||||
Δi18nLocalize,
|
||||
} from './i18n';
|
||||
|
||||
export {NgModuleFactory, NgModuleRef, NgModuleType} from './ng_module_ref';
|
||||
|
@ -117,6 +117,7 @@ export const angularCoreEnv: {[name: string]: Function} = {
|
||||
'Δi18nEnd': r3.Δi18nEnd,
|
||||
'Δi18nApply': r3.Δi18nApply,
|
||||
'Δi18nPostprocess': r3.Δi18nPostprocess,
|
||||
'Δi18nLocalize': r3.Δi18nLocalize,
|
||||
'ΔresolveWindow': r3.ΔresolveWindow,
|
||||
'ΔresolveDocument': r3.ΔresolveDocument,
|
||||
'ΔresolveBody': r3.ΔresolveBody,
|
||||
|
Reference in New Issue
Block a user