2

I am creating some translations for a form in PHP, I am translating all Japanese to English, then in a separate PHP file I have all the translations based on specific country.

Previously I translated using PHP Echo, like below:

<?php echo __('Subject'); ?>

Which in my translation file I would then write for example:

'Subject' => 'ご用件',

How would I add a translation to the following, I don't think my attempt is correct:

Previous Code:

$body = $applicant['username'].' さま
            <br/><br/>

My Attempt:

$body = $userData['username'].' __('Dear:username', array(':username' => $userData));さま
                    <br/><br/>
Karina
  • 665
  • 6
  • 17
  • 35
  • Please show the code for your `__()` function. – naththedeveloper Nov 22 '13 at 09:35
  • 1
    There is an extra quote in your last code : change it to `$body = $userData['username'].' '. __('Dear:username', array(':username' => $userData)).'

    ';` Maybe that was the problem ?
    – Asenar Nov 22 '13 at 09:37

2 Answers2

2

Placeholders look as best solution in that place.

You can easily realize unnamed placeholders with "sprintf" like here:

$t = ['Hello, %s!' => 'Hello in Japanese, %s!'];
$result = sprintf($t['Hello, %s!'], $username);

Or you can do it with named placeholders using "strtr" function:

$t = ['Hello, :username!' => 'Hello in Japanese, :username!'];
$result = strtr($t['Hello, %s!'], ['username' => $username]);

PS I like Yii-way very much. Look here: http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n

Stalinko
  • 3,319
  • 28
  • 31
0

Is very common create a __ function allowing receive an array with var, you can see a standard implementation at:

https://github.com/oscarotero/Gettext/blob/master/Gettext/translator_functions.php#L4

He use a % as special char trought the vsprinf function.

Maybe will be interesting intead of reinventing the wheel.

mcuadros
  • 4,098
  • 3
  • 37
  • 44