Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n translate possible by override the validate.translate method #10

Merged
merged 1 commit into from Sep 15, 2015

Conversation

huacnlee
Copy link
Contributor

with #9

When the users want custom the validate messages, they can through override the validate.translate to done that.

var validate = require('parameter');

validate.translate = function() {
  var args = Array.prototype.slice.call(arguments);
  // Assume there have I18n.t method for convert language.
  return I18n.t.apply(this, args);
}

if (typeof validate.translate === 'function') {
return validate.translate.apply(this, args);
} else {
return util.format.apply(this, args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 this 是指向谁呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t 呀?这么写有问题吗?还是我把 apply 理解错了?看起来能跑的呀。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没问题,这里应该传 util util.format.apply(util, args)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好了

function t() {
var args = Array.prototype.slice.call(arguments);
if (typeof validate.translate === 'function') {
return validate.translate.apply(this, args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也应该换成 validate 吧?validate.translate.apply(validate, args);

@fengmk2
Copy link
Member

fengmk2 commented Sep 15, 2015

+1

@fengmk2 fengmk2 self-assigned this Sep 15, 2015
fengmk2 added a commit that referenced this pull request Sep 15, 2015
I18n translate possible by override the `validate.translate` method
@fengmk2 fengmk2 merged commit 67228ca into master Sep 15, 2015
@fengmk2 fengmk2 deleted the i18n branch September 15, 2015 04:06
@fengmk2
Copy link
Member

fengmk2 commented Sep 15, 2015

parameter@1.1.0

@hotoo
Copy link
Member

hotoo commented Sep 15, 2015

I'm agree #9, but I don't like #10 .

My opinion:

  1. Localization in the parameter: paramter module buildin the localization, developers just need to set locale name.
  2. Localization in the app: as 需要 I18n,所以返回验证信息里面的 message 不应该保函 key 的字符串 #9 said: message field exclude the key name, App can use gettext or koa-locales to localization.

But this MR use the third way, Localization in the app AND parameter module, I don't like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants