【问题标题】:Aurelia Validation with i18n?使用 i18n 进行 Aurelia 验证?
【发布时间】:2017-02-28 10:18:57
【问题描述】:

是否有人通过 Aurelia Validation 与 i18n 插件一起处理多语言错误消息?当我添加来自 Aurelia 文档 http://aurelia.io/hub.html#/doc/article/aurelia/validation/latest/validation-basics/12 的代码时,我的应用程序甚至无法启动。

这是我的 ma​​in.js

import environment from './environment';
import {I18N} from 'aurelia-i18n';
import XHR from 'i18next-xhr-backend';
import {ValidationMessageProvider} from 'aurelia-validation';

//Configure Bluebird Promises.
//Note: You may want to use environment-specific configuration.
Promise.config({
  warnings: {
    wForgottenReturn: false
  }
});

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .feature('resources')
    .plugin('aurelia-validation');

  aurelia.use.plugin('aurelia-i18n', (instance) => {
      // register backend plugin
      instance.i18next.use(XHR);

      // adapt options to your needs (see http://i18next.com/docs/options/)
      instance.setup({
        backend: {                                  
          loadPath: '/locales/{{lng}}/{{ns}}.json',
        },
        lng : 'en',
        ns: ['translation'],
        defaultNS: 'translation',
        attributes : ['t','i18n'],
        fallbackLng : 'en',
        debug : false
      });
    });

  // Straight from Aurelia Documentation
  const i18n = aurelia.container.get(i18n);
  ValidationMessageProvider.prototype.getMessage = function(key) {
    const translation = i18n.tr(`errorMessages.${key}`);
    return this.parser.parseMessage(translation);
  };

  // Straight from Aurelia Documentation
  ValidationMessageProvider.prototype.getDisplayName = function(propertyName) {
    return i18n.tr(propertyName);
  };

  if (environment.debug) {
    aurelia.use.developmentLogging();
  }

  if (environment.testing) {
    aurelia.use.plugin('aurelia-testing');
  }

  aurelia.start().then(() => aurelia.setRoot());
}

我得到的错误是vendor-bundle.js:3394 Error: key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?(…)

如果我删除标记为// Straight from Aurelia Documentation 的两个部分,它可以正常工作(但只能使用一种语言)。

如果您在我的代码中发现错误,请指出。或者,如果您有一个使用 aurelia-validation 和 aurelia-i18n 的工作示例,请传递一个链接。谢谢!

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    也遇到了这个问题。看来这条线

    // Straight from Aurelia Documentation
    const i18n = aurelia.container.get(i18n);
    

    正在获取(或更可能创建)i18n 的不同实例,而不是

    aurelia.use.plugin('aurelia-i18n', (instance) =>
    

    我通过直接从aurelia.use.plugin() 获取i18n 实例来解决此问题,如下所示(这是打字稿,但同样的原则适用于纯 js):

    let i18n:I18N = null;
    aurelia.use.plugin('aurelia-i18n', (instance:I18N) => {
        i18n = instance;
        //rest of plugin code here
    }
    

    【讨论】:

      【解决方案2】:

      改用导入的 I18N:

      const i18n = aurelia.container.get(I18N);
      

      但实际上,i18n 之后似乎停止工作。我的解决方案是在第一次注入时更新第一页(app.js)中的 i18n 单例实例:

        constructor(i18n) {
          this.i18n = i18n;
          this.initAureliaSingletons();
        }
      
        /**
         * Some configurations breaks in 'main.js'
         * singletons can be configure here
         * @return {void}
         */
        initAureliaSingletons() {
          const i18n = this.i18n;
          ValidationMessageProvider.prototype.getMessage = function(key) {
            const translation = i18n.tr(`validation-${key}`);
            return this.parser.parseMessage(translation);
          };
        }
      

      【讨论】:

      • 不幸的是,这不起作用。事实上,它确实纠正了那个错误(这是必要的),但没有让 i18n 和验证一起工作。事实上,直接的结果是 i18n 在整个应用程序中停止工作。还有其他想法吗?
      • 我已经有一个用于单身人士的 config.js。但是由于加载功能,是否有必要将这个特定的 i18n 单例放入 app.js 中?哪个先加载——app.js 还是 main.js?如果我使用您将这些放在 App 中的模型,我如何在其他组件中访问这些单例?
      • 您可以稍后通过 aurelia 注入访问您的 i18n 单例,例如 @inject(I18n)。 App.js 首先加载,但似乎 container.get(I18N) 会创建另一个实例而不是单例
      【解决方案3】:

      我把它放在我的主服务器上,它就可以工作了。我认为诀窍是使用在插件初始化中初始化的变量:

        var i18n;
        aurelia.use.plugin('aurelia-i18n', (instance) => {
            // register backend plugin
            instance.i18next.use(Backend.with(aurelia.loader)).use(LngDetector);
            i18n = instance;
      

      (...)

            aurelia.use.plugin('aurelia-validation');
      
            var standardGetMessage = ValidationMessageProvider.prototype.getMessage;
            ValidationMessageProvider.prototype.getMessage = function (key) {
                if (i18n.i18next.exists(key)) {
                    const translation = i18n.tr(key);
                     return this.parser.parse(translation);
                } else {
                    return standardGetMessage(key);
                }
            };
      
            ValidationMessageProvider.prototype.getDisplayName = function (propertyName, displayName) {
                if (displayName !== null && displayName !== undefined) {
                    return displayName;
                }
                return i18n.tr(propertyName);
            };
      

      【讨论】:

        猜你喜欢
        • 2016-09-26
        • 1970-01-01
        • 2017-05-09
        • 2015-12-13
        • 2018-03-20
        • 2016-03-14
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多