Google i18next 模块的 Apps 脚本错误

Google Apps Script error with i18next module

我正在用 ES6 编写我的项目,目前面临 i18next 模块的问题。 https://www.i18next.com/

在我的本地系统上,当我导入 i18next import i18next from 'i18next'; 并在我的源文件中使用它时,一切正常。然而,在我 运行 npm run gulp(它将所有源文件组合成一个 javascript 文件 - main.js)并尝试将该代码上传到 Google Apps 脚本(使用gapps upload),它失败并出现 Bad Request. Upload failed. 错误。

上网查了一下发现这个错误的意思是语法有问题,所以我尝试将main.js中的代码复制粘贴到google应用程序脚本中,它显示了以下语法错误:

Invalid property ID. (Line 32, file "main")

第 32 行:

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

即使我只导入 i18next 模块而实际上没有对其进行任何操作,也会发生此错误。

这是我的 gulpfile:

import gulp from 'gulp';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import mocha from 'gulp-mocha';

const compileFile = 'main.js';

gulp.task('dest', () => {
    browserify({
        entries: ['src/'+compileFile]
    })
    .transform('babelify')
    .plugin('gasify')
    .bundle()
    .pipe(source(compileFile))
    .pipe(gulp.dest('dist'));
});

gulp.task('test', () => {
    gulp.src('test/**/*.js', {read: false})
    .pipe(mocha({
        reporter: 'spec',
        compilers: 'js:babel-core/register'
    }));
});

gulp.task('default', ['test', 'dest'], () => {});

gulp.task('watch', () => {
    gulp.watch('src/**/*.js', ['dest']);
});

也尝试过使用 i18n 模块,但没有用。

我想为我的翻译使用获取文本模块,不需要 currency/date 格式定制。只是来自翻译文件的文本 getter。不能使用 json po 或任何其他扩展名(我需要将所有文件作为一个文件上传到 GAS,不要认为他们允许除 .js 以外的任何文件)

我的模板文件是这样的en.js:

const res = {
  template: {
    "signIn":"Hello, <@#1>! Signed you in (#2)",
    ...
  },
  command: {
    "signIn": "hi",
    ...
  }
};
export default res;

刚刚找到有效的解决方案!

在尝试了所有国际化库并遇到各种与 gas 和 nongas 相关的错误后,node-polyglot 模块对我有用!

仍然不知道 i18next 不工作的原因

Google Apps 脚本对 ES6 的支持有限。据我了解,GAS 不包含 ES5 和 ES6 上引入的任何功能。

来自https://developers.google.com/apps-script/guides/services/#basic_javascript_features

Basic JavaScript features

Apps Script is based on JavaScript 1.6, plus a few features from 1.7 and 1.8. Many basic JavaScript features are thus available in addition to the built-in and advanced Google services: you can use common objects like Array, Date, RegExp, and so forth, as well as the Math and Object global objects. However, because Apps Script code runs on Google's servers (not client-side, except for HTML-service pages), browser-based features like DOM manipulation or the Window API are not available.

根据Mozilla Developer Network,JavaScript 1.6 对应于 ECMAScript 3 (ES3)。