0

I have a non-Symfony PHP project in which I want to use Symfony Translate component with MoFileLoader. I have setup PoEdit so it recognizes trans("string") function when parsing file looking for translations. This part of code <?php echo $translator->trans("Welcome"); ?> is parsed as it should be.

Problem is I can't make PoEdit to recognize this as plural string <?php echo $translator->trans('Found {result} search result', ['{result}' => 1]); ?>

1 is clearly number of results and with that data I want to be able to use correct plural string.

Big issue is that my main language is Croatian which plural form ‪nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); is way more complex than English one nplurals=2; plural=(n!=1)

I have added this Sources keywords in PoEdit

trans
trans:1,2

I already tried to use PHP and gettext to accomplish this but it is just not working for me due to server restrictions and other problems with gettex, Apache and PHP.

This is the way I'm using Symfony Translate component:

<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\MoFileLoader;

$lang = "hr";
$translator = new Translator($lang, new MessageSelector());
$translator->setFallbackLocales(['en']);
$translator->addLoader('mo', new MoFileLoader());
$translator->addResource('mo', "locale/$lang/LC_MESSAGES/messages.mo", $lang);

echo $translator->trans("Welcome");
echo $translator->trans('Found {result} search result', ['{result}' => 1]);
?>

Is there a way to make PoEdit recognize this as plural string:

<?php echo $translator->trans('Found {result} search result', ['{result}' => 1]); ?>
mmijic
  • 25
  • 7

1 Answers1

0
trans
trans:1,2

You gave two conflicting instructions there - one to treat trans as singular, one as plural. Additionally, the plural one, trans:1,2 doesn't make sense: you're asking to extract 1st parameter as singular and 2nd as plural, but you don't even have two string parameters. See documentation for the syntax.

$translator->trans('Found {result} search result', ['{result}' => 1]);

There's no way trans() could work like this — how would it know what number to use to determine the plural version to choose? And how would it know what to use for plural version in English if it's nowhere to be found in the code?

You need to use transChoice() instead, as documented in Symfony docs. Plurals are always a moderately complicated subject, you absolutely must read the documentation to understand the concepts before writing any code.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Václav Slavík
  • 6,445
  • 2
  • 28
  • 25
  • I read Symfony documentation and didn't use transChoice() because it is depreciated in version 4.2. However i will try to use it since it is not Symfony project, I'm only using translate component. – mmijic Sep 03 '19 at 17:07
  • @mmijic Then the same deprecation notice tells you what to do instead if you don't want `transChoice()`… – Václav Slavík Sep 03 '19 at 17:30