PHP views internationalization

<p>Hello world!</p><p><?php _('Hello world') ?></p>
<!-- messages.po: msgid "Hello world" -->

<p>Hello world, <?php $user ?>!</p><p><?php sprintf(_('Hello world, %1$s!'), $user) ?></p>
<!-- messages.po: msgid "Hello world, %1$s!" -->

Features supported

Configure hardcoded strings extraction from PHP views

The plugin should automatically configure itself for CodeIgniter, CakePHP, Zend and Laminas projects, but adjustments could be needed for custom setup and other frameworks.

PHP Source Code Preferences screenshot

Scope

i18n Ally is applying inspections for files that have .php extension and are included into a PhpStorm’s scope.

Create a new scope or adjust existing by clicking on button and handpicking only the meaningful directories and files.

Select Project files to include all .php files in your project.

Important! This source would only looks for hardcoded strings within HTML and outside of PHP snippets. To extract hardcoded strings from PHP snippets you have to configure a PHP source.

Inline tags

List of tags that would be taken inside translations, like a, strong or span. Filled by default with all “inline” tags listed on MDN.

An example of extraction result difference between block and inline tags:

Three
<p>different</p>
keys.
<!-- ⬇ will be extracted into -->
<?php _('Three') ?>
<p><?php _('different') ?></p>
<?php _('keys.') ?>


One <b>inclusive</b> key.
<!-- ⬇ will be extracted into -->
<?php _('One <b>inclusive</b> key.') ?>

You can add custom tags, like icon, by appending a new tag to the comma-separated list.

Translatable attribute names

Translatable attributes are also checked for the translatable text:

<img src="…"
     alt="Checked by default"
     title="Checked by default"
     data-content="Requires configuration" />

You can add custom attributes, like data-content, by appending a new attribute to the comma-separated list.

Replacement template

The “Replacement template” reflects the result of the hardcoded string extraction.function name and arguments template.

Recommended value for gettext, CodeIgniter, CakePHP and Zend/Laminas: _('%key%') with sprintf mode enabled.
Recommended value for Yii v2: _('%namespace%', '%key%', %map%).
Recommended value for Yii v3: _('%key%', %map%, '%namespace%').

It could be any callable PHP structure that wraps arguments into parentheses:

  • function: _(…), __(…),
  • object method: $this->trans(…), $translator->trans(…),
  • static method: \Yii:app(…).

%key%

Short key or a natural language string that defines a translation.

%namespace%

Namespace (called ‘domain’ in Symfony) usually means a part of language file path from where translations would be searched for. The default namespace is usually messages, but could be changed by putting a namespace in first position in “Namespaces” field.

%map%

If there are no variables in the string, then nothing would be added.

Map will be replaced with an associative short syntax array if there are any placeholders detected: <?php echo trans('key', ['foo' => $foo, 'bar' => $bar]) ?>.

Placeholder names will be determined automatically based on a respective variable, function or method name.

In language files placeholder syntax will be determined based on the Placeholder format setting of the language file.

%list%

If there are no variables in the string, then nothing would be added.

List will be replaced with an array if there are any placeholders detected: <?php echo trans('key', [$foo, $bar]) ?>.

In language files the ordered placeholder syntax {0}, {1} will be enforced.

%varargs%

If there are no variables in the string, then nothing would be added.

Varargs will be replaced with placeholder passed directly to the translation function if there are any placeholders detected: <?php echo trans('key', $foo, $bar) ?>.

In language files the ordered placeholder syntax {0}, {1} will be enforced.

Supported language constructs

i18n Ally finds hardcoded user-facing strings are only detected inside HTML tags.

Placeholder names are determined automatically.

What’s not supported

  • Using an array for message retrieval (common approach in PHP legacy codebases, for example $lang['key']).

What strings are skipped

  • Pure HTML markup with PHP snippets expressions, like <a href="<?php route('home')?>"><img …></a>.
  • All attributes except ones listed in “Translatable attribute names” preference.
  • Content inside script and pre tags.
  • Strings that looks like code: without letters, multiple words without spaces or camelCased ones.

Best practice: dealing with branching in messages

It’s common to have small and simple branching for presentation purposes:

Webhook <strong><?php echo $success ? 'succeeded' : 'failed' ?></strong>.

The best practice it to separate this message into two different ones so translators would have a full context and would be able to adjust word order according the target language grammar.

1st step: manually extract the condition out of the message to get two messages without condition

<?php if ($success) { ?>
    Webhook <strong>succeeded</strong>.
<?php } else { ?>
    Webhook <strong>failed</strong>.
<?php } ?>

2nd step: replace simple messages with i18n Ally

<?php
    if ($success) {
        echo _('Webhook <strong>succeeded</strong>.')
    } else {
        echo _('Webhook <strong>failed</strong>.')
    }
?>