Project

General

Profile

1
"use strict";
2
/**
3
 * @license
4
 * Copyright Google Inc. All Rights Reserved.
5
 *
6
 * Use of this source code is governed by an MIT-style license that can be
7
 * found in the LICENSE file at https://angular.io/license
8
 */
9
Object.defineProperty(exports, "__esModule", { value: true });
10
/**
11
 * Extract i18n messages from source code
12
 */
13
// Must be imported first, because Angular decorators throw on load.
14
require("reflect-metadata");
15
var compiler = require("@angular/compiler");
16
var path = require("path");
17
var compiler_host_1 = require("./compiler_host");
18
var path_mapped_compiler_host_1 = require("./path_mapped_compiler_host");
19
var Extractor = (function () {
20
    function Extractor(options, ngExtractor, host, ngCompilerHost, program) {
21
        this.options = options;
22
        this.ngExtractor = ngExtractor;
23
        this.host = host;
24
        this.ngCompilerHost = ngCompilerHost;
25
        this.program = program;
26
    }
27
    Extractor.prototype.extract = function (formatName, outFile) {
28
        var _this = this;
29
        // Checks the format and returns the extension
30
        var ext = this.getExtension(formatName);
31
        var promiseBundle = this.extractBundle();
32
        return promiseBundle.then(function (bundle) {
33
            var content = _this.serialize(bundle, formatName);
34
            var dstFile = outFile || "messages." + ext;
35
            var dstPath = path.join(_this.options.genDir, dstFile);
36
            _this.host.writeFile(dstPath, content, false);
37
            return [dstPath];
38
        });
39
    };
40
    Extractor.prototype.extractBundle = function () {
41
        var _this = this;
42
        var files = this.program.getSourceFiles().map(function (sf) { return _this.ngCompilerHost.getCanonicalFileName(sf.fileName); });
43
        return this.ngExtractor.extract(files);
44
    };
45
    Extractor.prototype.serialize = function (bundle, formatName) {
46
        var _this = this;
47
        var format = formatName.toLowerCase();
48
        var serializer;
49
        switch (format) {
50
            case 'xmb':
51
                serializer = new compiler.Xmb();
52
                break;
53
            case 'xliff2':
54
            case 'xlf2':
55
                serializer = new compiler.Xliff2();
56
                break;
57
            case 'xlf':
58
            case 'xliff':
59
            default:
60
                serializer = new compiler.Xliff();
61
        }
62
        return bundle.write(serializer, function (sourcePath) { return _this.options.basePath ?
63
            path.relative(_this.options.basePath, sourcePath) :
64
            sourcePath; });
65
    };
66
    Extractor.prototype.getExtension = function (formatName) {
67
        var format = (formatName || 'xlf').toLowerCase();
68
        switch (format) {
69
            case 'xmb':
70
                return 'xmb';
71
            case 'xlf':
72
            case 'xlif':
73
            case 'xliff':
74
            case 'xlf2':
75
            case 'xliff2':
76
                return 'xlf';
77
        }
78
        throw new Error("Unsupported format \"" + formatName + "\"");
79
    };
80
    Extractor.create = function (options, program, tsCompilerHost, locale, compilerHostContext, ngCompilerHost) {
81
        if (!ngCompilerHost) {
82
            var usePathMapping = !!options.rootDirs && options.rootDirs.length > 0;
83
            var context = compilerHostContext || new compiler_host_1.ModuleResolutionHostAdapter(tsCompilerHost);
84
            ngCompilerHost = usePathMapping ? new path_mapped_compiler_host_1.PathMappedCompilerHost(program, options, context) :
85
                new compiler_host_1.CompilerHost(program, options, context);
86
        }
87
        var ngExtractor = compiler.Extractor.create(ngCompilerHost, locale || null).extractor;
88
        return new Extractor(options, ngExtractor, tsCompilerHost, ngCompilerHost, program);
89
    };
90
    return Extractor;
91
}());
92
exports.Extractor = Extractor;
93
//# sourceMappingURL=extractor.js.map
(11-11/30)