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:
Olivier Combe
2019-04-11 11:17:49 +02:00
committed by Igor Minar
parent 387fbb8106
commit 91c7b451d5
22 changed files with 1397 additions and 564 deletions

View File

@ -7,5 +7,4 @@
*/
export * from './src/render3';
export * from './src/goog_get_msg';
export * from './src/ivy_test_selectors';

View File

@ -1,28 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* A method that injects goog.getMsg-like function into global scope.
*
* 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.
*/
export function polyfillGoogGetMsg(translations: {[key: string]: string} = {}): void {
const glob = (global as any);
glob.goog = glob.goog || {};
glob.goog.getMsg = function(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(/\{\$(.*?)\}/g, (match, key) => placeholders[key] || '') :
input;
};
}