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 #28

Closed
jeanpimentel opened this issue Sep 25, 2011 · 6 comments
Closed

I18n #28

jeanpimentel opened this issue Sep 25, 2011 · 6 comments

Comments

@jeanpimentel
Copy link
Contributor

Is there any plan to this?
How could I translate errors?

v::email()->assert('invalid@'); 
# "invalid@" must be valid email.

to

v::email()->assert('invalid@'); 
# "invalid@" deve ser um email válido.
@alganet alganet closed this as completed Sep 25, 2011
@alganet alganet reopened this Sep 25, 2011
@alganet
Copy link
Member

alganet commented Sep 25, 2011

(sorry for closing the issue, that wasn't intentional)

There's nothing done yet on i18n.

The messages are built only when you request them, and its possible to use an iterator to traverse and translate them:

<?php

$translator = new TranslatorFooBar();

try {
    v::email()->assert('invalid@');
} catch (ValidationException $e) {
    foreach ($e->getIterator() as $child)
        $child->setTemplate($translator->translate($child->getTemplate());
}

I'm not sure if a built-in translator is a good idea. Maybe is too much for the project itself, but for sure translation support is welcome.

Since several translating engines relies on messaging lookup (gettext style) I think something like this would be welcome:

<?php
$translatorCallback = new Anything;
try {
    v::email()->assert('invalid@');
} catch (ValidationException $e) {
    $e->translateUsing($translatorCallback);
    $messages = $e->findMessages(/* ... */);
}

This would allow users to use any translating engine they want. For example, built-in PHP's gettext ($e->translateUsing('gettext');) or Zend_Translate ($e->translateUsing(array($zendTranslate, '_'));).

Once that support is built, the next step would be to distribute the translated messages alongside the project's code.

@jeanpimentel
Copy link
Contributor Author

I'm testing some stuff about i18n.
Which is better?
Translate the message before format? I think that can be a problem because of {{variable style}} but is the most correct.
Translate after format can return unexpected results, changing {{name}} incorrectly.
In any case, messages need to change to anything like _('{{name}} must be a valid date'), right?

@alganet
Copy link
Member

alganet commented Oct 10, 2011

I believe the better solution is to translate the template itself.

{{name}} must be a valid date would be translated to {{name}} deve ser uma data válida, for example.

Each ValidationException instance (and its descendants) has getTemplate() and setTemplate() methods that we could use in a possible I18n module.

If you have any samples on the I18n API you're working on I'd be happy to take a look!

@jeanpimentel
Copy link
Contributor Author

After some attempts, this is a draft that works:

try {
    v::translateCallback(array('MyTranslate', 'translate'))->int()->max(30)->odd()->assert('jeanpimentel');
} catch (\Respect\Validation\Exceptions\ValidationException $e) {
    echo $e->getFullMessage();
}

Results

\-Essas 2 regras devem ser seguidas para "jeanpimentel"
  |-"jeanpimentel" deve ser um número inteiro
  \-"jeanpimentel" deve ser um número ímpar

This isn't the correct and final mode. I'm just reporting the status of this feature.

@alganet
Copy link
Member

alganet commented Nov 1, 2011

Seems nice! I'd love to see the code, do you have it on your fork?

@jeanpimentel
Copy link
Contributor Author

#57

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

No branches or pull requests

2 participants