使用gettext解析.po翻译文件的问题

问题描述 投票:0回答:1

我尝试在.po加载程序中使用ngx-translate;但是在编译期间,我收到以下警告:

WARNING in ./node_modules/encoding/lib/iconv-loader.js 9:12-34
Critical dependency: the request of a dependency is an expression

此警告与以下代码行有关:

const po = gettext.po.parse(contents, 'utf-8');

在运行时,我看到以下错误并且该应用程序无法运行:

index.js:43 Uncaught ReferenceError: global is not defined
    at Object../node_modules/buffer/index.js (index.js:43)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/iconv-lite/lib/index.js (index.js:5)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/encoding/lib/encoding.js (encoding.js:3)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/gettext-parser/lib/poparser.js (poparser.js:1)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/gettext-parser/index.js (index.js:1)
    at __webpack_require__ (bootstrap:79)

我搜索了网络和堆栈溢出,但是找不到解决此问题的方法。

是否有gettext解析.po文件的替代方法;或如何解决这个问题?

angular typescript gettext
1个回答
0
投票

您可以通过在index.html中添加这些代码来快速解决此问题。>

 <script>
    var global = global || window;
    var Buffer = Buffer || [];
    var process = process || {
       env: { DEBUG: undefined },
       version: []
    };
  </script>

更好的修复

步骤1:将脚本添加到package.json

"postinstall": "node patch-iconv.js && node patch-ng-devkit.js"

步骤2:使用package.json创建一个与文件相同的目录,并命名为“ patch-iconv.js

”内容:

// IIFE
(function() {

  'use strict';

  // node
  var fs = require('fs');
  var path = require('path');

  // Patch encoding module due to iconv issues -> make it use iconv-lite
  (function() {
    var PATCH_VERSION = '0.1.12';
    var PATCH_MODULE = 'encoding';
    var PATCH_REASON = 'Use iconv-lite instead of iconv, helpful for webpack bundling';
    console.log('patching `%s`(%s) module', PATCH_MODULE, PATCH_VERSION);
    var pathToModule = path.join(__dirname, 'node_modules', PATCH_MODULE);
    var pathToModulePackage = path.join(pathToModule, 'package.json');
    var pathToModulePatchedFile1 = path.join(pathToModule, 'lib/iconv-loader.js');
    var pathToModulePatchedFile2 = path.join(pathToModule, 'lib/encoding.js');
    var moduleInfo = require(pathToModulePackage);
    if (moduleInfo.version !== PATCH_VERSION) {
      console.error(
        'patching `encoding` failed - expected `%s` but detected `%s`',
        PATCH_VERSION,
        moduleInfo.version
      );
      process.exit(1);
    }
    var contents;
    if (fs.existsSync(pathToModulePatchedFile1)) {
      contents = [
        '\'use strict\';',
        'module.exports = require(\'iconv-lite\');',
        '',
      ].join('\n');
      fs.writeFileSync(pathToModulePatchedFile1, contents);
    } else {
      console.error('patching `%s` failed because the file does not exist in', PATCH_MODULE, pathToModule);
      process.exit(1);
    }
    if (fs.existsSync(pathToModulePatchedFile2)) {
      contents = fs.readFileSync(pathToModulePatchedFile2).toString();
      contents = contents.replace('console.error(E);','');
      fs.writeFileSync(pathToModulePatchedFile2, contents);
    } else {
      console.error('patching `%s` failed because the file does not exist in', PATCH_MODULE, pathToModule);
      process.exit(1);
    }
    console.log('patching `%s`, reason: `%s` - completed', PATCH_MODULE, PATCH_REASON);
  })();


  })(this);

第3步:使用package.json创建一个与文件相同的目录,并命名为“ patch-ng-devkit.js

”内容:

const fs = require('fs');
const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';

fs.readFile(f, 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');

  fs.writeFile(f, result, 'utf8', function (err) {
    if (err) return console.log(err);
  });
});

步骤4:然后运行

npm run postinstall

完成,再次构建并工作

© www.soinside.com 2019 - 2024. All rights reserved.