Giter VIP home page Giter VIP logo

Comments (6)

alganet avatar alganet commented on May 11, 2024

(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.

from validation.

jeanpimentel avatar jeanpimentel commented on May 11, 2024

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?

from validation.

alganet avatar alganet commented on May 11, 2024

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!

from validation.

jeanpimentel avatar jeanpimentel commented on May 11, 2024

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.

from validation.

alganet avatar alganet commented on May 11, 2024

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

from validation.

jeanpimentel avatar jeanpimentel commented on May 11, 2024

#57

from validation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.