8000 [Validator] Integrated the Translator in the Validator component by webmozart · Pull Request #6137 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Integrated the Translator in the Validator component #6137

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

Merged
merged 12 commits into from
Jan 9, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Validator] Improved the inline documentation of DefaultTranslator
  • Loading branch information
webmozart committed Jan 8, 2013
commit 58bfd60b0fdc24328cac5f43e360dce500fa6c73
108 changes: 103 additions & 5 deletions src/Symfony/Component/Validator/DefaultTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,114 @@
* Simple translator implementation that simply replaces the parameters in
* the message IDs.
*
* Does not support translation domains or locales.
* Example usage:
*
* $translator = new DefaultTranslator();
*
* echo $translator->trans(
* 'This is a {{ var }}.',
* array('{{ var }}' => 'donkey')
* );
*
* // -> This is a donkey.
*
* echo $translator->transChoice(
* 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
* 3,
* array('{{ count }}' => 'three')
* );
*
* // -> These are three donkeys.
*
* This translator does not support message catalogs, translation domains or
* locales. Instead, it implements a subset of the capabilities of
* {@link \Symfony\Component\Translation\Translator} and can be used in places
* where translation is not required by default but should be optional.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class DefaultTranslator implements TranslatorInterface
{
/**
* {@inheritdoc}
* Interpolates the given message.
*
* Parameters are replaced in the message in the same manner that
* {@link strtr()} uses.
*
* Example usage:
*
* $translator = new DefaultTranslator();
*
* echo $translator->trans(
* 'This is a {{ var }}.',
* array('{{ var }}' => 'donkey')
* );
*
* // -> This is a donkey.
*
* @param string $id The message id
* @param array $parameters An array of parameters for the message
* @param string $domain Ignored
* @param string $locale Ignored
*
* @return string The interpolated string
*/
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
return strtr($id, $parameters);
}

/**
* {@inheritdoc}
* Interpolates the given choice message by choosing a variant according to a number.
*
* The variants are passed in the message ID using the format
* "<singular>|<plural>". "<singular>" is chosen if the passed $number is
* exactly 1. "<plural>" is chosen otherwise.
*
* This format is consistent with the format supported by
* {@link \Symfony\Component\Translation\Translator}, but it does not
* have the same expressiveness. While Translator supports intervals in
* message translations, which are needed for languages other than English,
* this translator does not. You should use Translator or a custom
* implementation of {@link TranslatorInterface} if you need this or similar
* functionality.
*
* Example usage:
*
* echo $translator->transChoice(
* 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
* 0,
* array('{{ count }}' => 0)
* );
*
* // -> These are 0 donkeys.
*
* echo $translator->transChoice(
* 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
* 1,
* array('{{ count }}' => 1)
* );
*
* // -> This is 1 donkey.
*
* echo $translator->transChoice(
* 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
* 3,
* array('{{ count }}' => 3)
* );
*
* // -> These are 3 donkeys.
*
* @param string $id The message id
* @param integer $number The number to use to find the index of the message
* @param array $parameters An array of parameters for the message
* @param string $domain Ignored
* @param string $locale Ignored
*
* @return string The translated string
*
* @throws InvalidArgumentException If the message id does not have the format
* "singular|plural".
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
Expand All @@ -52,15 +144,21 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
}

/**
* {@inheritdoc}
* Not supported.
*
* @param string $locale The locale
*
* @throws BadMethodCallException
*/
public function setLocale($locale)
{
throw new BadMethodCallException('Unsupported method.');
}

/**
* {@inheritdoc}
* Not supported.
*
* @throws BadMethodCallException
*/
public function getLocale()
{
Expand Down
0