使用 apos.i18n 翻译模块选项?

Translating module options using apos.i18n?

我需要一个在自定义小部件模块 index.js 文件中使用 apostrophe-i18n 模块的示例。

在搜索了所有 Apostrophes 内置模块后,我意识到 Apostrophe 从不使用 i18n 来翻译 javascript 字符串。

我尝试了以下方法(在一个模块中找到),但它显示 "self is not defined"。我如何导入 apostrophe-i18n 模块来翻译 index.js 文件中的字符串?

module.exports = {
    extend: 'apostrophe-widgets',
    label: self.apos.i18n.__('Link Page'),
    addFields: {
        name: '_page',
        type: 'joinByOne',
        withType: 'apostrophe-page',
        label: self.apos.i18n.__('Page'),
        required: true,
        idField: 'pageId',
        filters: {
            projection: {
                title: 1,
                slug: 1
            }
        }
    }
};

编辑供参考:

模块选项的

i18n 不是必需的,因为撇号本身会处理管理字符串的语言环境。将小部件添加到 apos.areaapos.singleton 调用后,所有字符串都出现在 locale/en.json 中,准备翻译(根据我的观察)。

似乎 apostrophe-i18n 使用浏览器语言来确定要使用的语言环境。这种语言必须在 app.js:

中这样配置
'apostrophe-i18n': {
  locales: ['en', 'de']
},

在那之后,我在 locale/de.json 中的所有翻译字符串都打印到页面上。


在模块内部使用 self 的工作代码(同样,i18n 不需要):

module.exports = {

    construct: function(self, options) {
        options.label = self.apos.i18n.__('Link Page');
    },

    extend: 'apostrophe-widgets',
    addFields: [
        {
            name: '_page',
            type: 'joinByOne',
            withType: 'apostrophe-page',
            label: 'Page',
            required: true,
            idField: 'pageId',
            filters: {
                projection: {
                    title: 1,
                    slug: 1
                }
            }
        }
    ]
};

self 在模块的 construct 函数之外不存在。试试看

  construct: function(self, options) {
    options.label = self.apos.i18n.__('Link Page');
  }