feat(i18n): xliff integration
This commit is contained in:

committed by
Vikram Subramanian

parent
96bf42261b
commit
f6a7d6504c
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
|
||||
<file source-language="en" datatype="plaintext" original="ng2.template">
|
||||
<body>
|
||||
<trans-unit id="76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4" datatype="html">
|
||||
<source>translate me</source>
|
||||
<target>käännä teksti</target>
|
||||
<note priority="1" from="description">desc</note>
|
||||
<note priority="1" from="meaning">meaning</note>
|
||||
</trans-unit>
|
||||
<trans-unit id="65cc4ab3b4c438e07c89be2b677d08369fb62da2" datatype="html">
|
||||
<source>Welcome</source>
|
||||
<target>tervetuloa</target>
|
||||
</trans-unit>
|
||||
</body>
|
||||
</file>
|
||||
</xliff>
|
@ -1,3 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE translationbundle [<!ELEMENT translationbundle (translation)*>
|
||||
<!ATTLIST translationbundle lang CDATA #REQUIRED>
|
||||
<!ELEMENT translation (#PCDATA|ph)*>
|
||||
<!ATTLIST translation id CDATA #REQUIRED>
|
||||
<!ELEMENT ph EMPTY>
|
||||
<!ATTLIST ph name CDATA #REQUIRED>
|
||||
]>
|
||||
<translationbundle>
|
||||
<translation id="76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4">käännä teksti</translation>
|
||||
<translation id="65cc4ab3b4c438e07c89be2b677d08369fb62da2">tervetuloa</translation>
|
||||
|
@ -73,7 +73,7 @@ describe('template codegen output', () => {
|
||||
|
||||
it('should inject the translations format into the component', () => {
|
||||
const compFixture = createComponent(BasicComp);
|
||||
expect(compFixture.componentInstance.translationsFormat).toEqual('xtb');
|
||||
expect(compFixture.componentInstance.translationsFormat).toEqual('xlf');
|
||||
});
|
||||
|
||||
it('should support i18n for content tags', () => {
|
||||
|
@ -11,11 +11,7 @@ import './init';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
describe('template i18n extraction output', () => {
|
||||
const outDir = '';
|
||||
|
||||
it('should extract i18n messages', () => {
|
||||
const EXPECTED = `<?xml version="1.0" encoding="UTF-8" ?>
|
||||
const EXPECTED_XMB = `<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE messagebundle [
|
||||
<!ELEMENT messagebundle (msg)*>
|
||||
<!ATTLIST messagebundle class CDATA #IMPLIED>
|
||||
@ -42,9 +38,39 @@ describe('template i18n extraction output', () => {
|
||||
<msg id="65cc4ab3b4c438e07c89be2b677d08369fb62da2">Welcome</msg>
|
||||
</messagebundle>`;
|
||||
|
||||
const EXPECTED_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
||||
<file source-language="en" datatype="plaintext" original="ng2.template">
|
||||
<body>
|
||||
<trans-unit id="76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4" datatype="html">
|
||||
<source>translate me</source>
|
||||
<target/>
|
||||
<note priority="1" from="description">desc</note>
|
||||
<note priority="1" from="meaning">meaning</note>
|
||||
</trans-unit>
|
||||
<trans-unit id="65cc4ab3b4c438e07c89be2b677d08369fb62da2" datatype="html">
|
||||
<source>Welcome</source>
|
||||
<target/>
|
||||
</trans-unit>
|
||||
</body>
|
||||
</file>
|
||||
</xliff>`;
|
||||
|
||||
describe('template i18n extraction output', () => {
|
||||
const outDir = '';
|
||||
|
||||
it('should extract i18n messages as xmb', () => {
|
||||
const xmbOutput = path.join(outDir, 'messages.xmb');
|
||||
expect(fs.existsSync(xmbOutput)).toBeTruthy();
|
||||
const xmb = fs.readFileSync(xmbOutput, {encoding: 'utf-8'});
|
||||
expect(xmb).toEqual(EXPECTED);
|
||||
expect(xmb).toEqual(EXPECTED_XMB);
|
||||
});
|
||||
|
||||
it('should extract i18n messages as xliff', () => {
|
||||
const xlfOutput = path.join(outDir, 'messages.xlf');
|
||||
expect(fs.existsSync(xlfOutput)).toBeTruthy();
|
||||
const xlf = fs.readFileSync(xlfOutput, {encoding: 'utf-8'});
|
||||
expect(xlf).toEqual(EXPECTED_XLIFF);
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -144,7 +144,8 @@ export class CodeGenerator {
|
||||
const reflectorHost = new ReflectorHost(program, compilerHost, options, reflectorHostContext);
|
||||
const staticReflector = new StaticReflector(reflectorHost);
|
||||
StaticAndDynamicReflectionCapabilities.install(staticReflector);
|
||||
const htmlParser = new compiler.i18n.HtmlParser(new HtmlParser(), transContent);
|
||||
const htmlParser =
|
||||
new compiler.i18n.HtmlParser(new HtmlParser(), transContent, cliOptions.i18nFormat);
|
||||
const config = new compiler.CompilerConfig({
|
||||
genDebugInfo: options.debug === true,
|
||||
defaultEncapsulation: ViewEncapsulation.Emulated,
|
||||
@ -161,6 +162,7 @@ export class CodeGenerator {
|
||||
new compiler.NgModuleResolver(staticReflector),
|
||||
new compiler.DirectiveResolver(staticReflector), new compiler.PipeResolver(staticReflector),
|
||||
config, console, elementSchemaRegistry, staticReflector);
|
||||
// TODO(vicb): do not pass cliOptions.i18nFormat here
|
||||
const offlineCompiler = new compiler.OfflineCompiler(
|
||||
resolver, normalizer, tmplParser, new StyleCompiler(urlResolver), new ViewCompiler(config),
|
||||
new NgModuleCompiler(), new TypeScriptEmitter(reflectorHost), cliOptions.locale,
|
||||
|
@ -30,12 +30,29 @@ import {StaticReflector, StaticSymbol} from './static_reflector';
|
||||
function extract(
|
||||
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
|
||||
program: ts.Program, host: ts.CompilerHost) {
|
||||
const extractor = Extractor.create(ngOptions, cliOptions.i18nFormat, program, host);
|
||||
const htmlParser = new compiler.i18n.HtmlParser(new HtmlParser());
|
||||
const extractor = Extractor.create(ngOptions, cliOptions.i18nFormat, program, host, htmlParser);
|
||||
const bundlePromise: Promise<compiler.i18n.MessageBundle> = extractor.extract();
|
||||
|
||||
return (bundlePromise).then(messageBundle => {
|
||||
const serializer = new compiler.i18n.Xmb();
|
||||
const dstPath = path.join(ngOptions.genDir, 'messages.xmb');
|
||||
let ext: string;
|
||||
let serializer: compiler.i18n.Serializer;
|
||||
const format = (cliOptions.i18nFormat || 'xlf').toLowerCase();
|
||||
|
||||
switch (format) {
|
||||
case 'xmb':
|
||||
ext = 'xmb';
|
||||
serializer = new compiler.i18n.Xmb();
|
||||
break;
|
||||
case 'xliff':
|
||||
case 'xlf':
|
||||
default:
|
||||
ext = 'xlf';
|
||||
serializer = new compiler.i18n.Xliff(htmlParser, compiler.DEFAULT_INTERPOLATION_CONFIG);
|
||||
break;
|
||||
}
|
||||
|
||||
const dstPath = path.join(ngOptions.genDir, `messages.${ext}`);
|
||||
host.writeFile(dstPath, messageBundle.write(serializer), false);
|
||||
});
|
||||
}
|
||||
@ -128,7 +145,8 @@ export class Extractor {
|
||||
|
||||
static create(
|
||||
options: tsc.AngularCompilerOptions, translationsFormat: string, program: ts.Program,
|
||||
compilerHost: ts.CompilerHost, reflectorHostContext?: ReflectorHostContext): Extractor {
|
||||
compilerHost: ts.CompilerHost, htmlParser: compiler.i18n.HtmlParser,
|
||||
reflectorHostContext?: ReflectorHostContext): Extractor {
|
||||
const xhr: compiler.XHR = {
|
||||
get: (s: string) => {
|
||||
if (!compilerHost.fileExists(s)) {
|
||||
@ -143,7 +161,6 @@ export class Extractor {
|
||||
const reflectorHost = new ReflectorHost(program, compilerHost, options, reflectorHostContext);
|
||||
const staticReflector = new StaticReflector(reflectorHost);
|
||||
StaticAndDynamicReflectionCapabilities.install(staticReflector);
|
||||
const htmlParser = new compiler.i18n.HtmlParser(new HtmlParser());
|
||||
|
||||
const config = new compiler.CompilerConfig({
|
||||
genDebugInfo: options.debug === true,
|
||||
|
Reference in New Issue
Block a user