PHP

Many PHP programs use localisable string arrays. The toolkit supports these files with php2po and po2php.

Translation values

PHP translation entries may contain quoted text, simple variable interpolation, or concatenations of quoted text and simple variables. Unsupported expressions in translation entries raise a parse error that identifies the entry. Literal numbers, booleans, and null are skipped as metadata.

The toolkit exports variables as explicit placeholders such as {$name}. Edited translations also accept standalone $name in prose and HTML, as in Hello $name! or href="$url". A bare placeholder starts at the beginning of the value or follows whitespace, a quote, >, or ;. It ends before whitespace, a quote, <, &, or the end of the value. A single sentence punctuation mark (.,!?;:) may precede that ending boundary. Laravel’s | plural separator also counts as a boundary on both sides.

Use the braced form when a placeholder touches other text, as in {$name}s or ({$name}). PHP expressions such as $items[$key] remain literal under the boundary rule. Escape literal placeholders as \$name or \{$name}. Immediately before a placeholder or escaped placeholder, write two backslashes for each literal backslash you want in the PHP value.

Conformance

Our format support allows:

  • Single and double quoted strings (both for keys and values)

    <?php
    $variable = 'string';
    $messages["language"] = 'Language';
    define('item', "another string");
    
  • PHP simple variable syntax

    <?php
    $variable = 'string';
    $another_variable = "another string";
    
  • PHP square bracket array syntax

    <?php
    $messages['language'] = 'Language';
    $messages['file'] = "File";
    $messages["window"] = 'Window';
    $messages["firewall"] = "Firewall";
    
  • PHP array syntax

    Added in version 1.7.0.

    <?php
    // Can be 'array', 'Array' or 'ARRAY'.
    $lang = array(
       'name' => 'value',
       'name2' => "value2",
       "key1" => 'value3',
       "key2" => "value4",
    );
    
  • PHP define syntax

    Added in version 1.10.0.

    <?php
    define('item', 'string');
    define('another_item', "another string");
    define("key", 'and another string');
    define("another_key", "yet another string");
    
  • PHP short array syntax

    Added in version 2.3.0.

    <?php
    $variable = [
        "foo" => "bar",
        "bar" => "foo",
    ];
    
  • Heredoc

    Added in version 2.3.0.

    <?php
    $variable = <<<EOT
    bar
    EOT;
    
  • Nowdoc

    Added in version 2.3.0.

    <?php
    $variable = <<<'EOD'
    Example of string
    spanning multiple lines
    using nowdoc syntax.
    EOD;
    
  • Escape sequences (both for single and double quoted strings)

    <?php
    $variable = 'He said: "I\'ll be back"';
    $another_variable = "First line \n second line";
    $key = "\tIndented string";
    
  • Multiline entries

    <?php
    $lang = array(
       'name' => 'value',
       'info' => 'Some hosts disable automated mail sending
            on their servers. In this case the following features
            cannot be implemented.',
       'name2' => 'value2',
    );
    
  • Various layouts of the id

    <?php
    $string['name'] = 'string';
    $string[name] = 'string';
    $string[ 'name' ] = 'string';
    
  • Comments

    Changed in version 1.10.0.

    <?php
    # Hash one-line comment
    $messages['language'] = 'Language';
    
    // Double slash one-line comment
    $messages['file'] = 'File';
    
    /*
       Multi-line
       comment
    */
    $messages['help'] = 'Help';
    
  • Whitespace before end delimiter

    Added in version 1.10.0.

    <?php
    $variable = 'string'     ;
    
    $string['name'] = 'string'     ;
    
    $lang = array(
       'name' => 'value'           ,
    );
    
    define('item', 'string'    );
    
  • Nested arrays with any number of nesting levels

    Added in version 1.11.0.

    <?php
    $lang = array(
       'name' => 'value',
       'datetime' => array(
          'TODAY' => 'Today',
          'YESTERDAY' => 'Yesterday',
          'AGO' => array(
              0 => 'less than a minute ago',
              2 => '%d minutes ago',
              60 => '1 hour ago',
          ),
          'Converted' => 'Converted',
          'LAST' => 'last',
       ),
    );
    
  • Whitespace in the array declaration

    Added in version 1.11.0.

    <?php
    $variable = array    (
       "one" => "this",
       "two" => "that",
    );
    
  • Blank array declaration, then square bracket syntax to fill that array

    Added in version 1.12.0.

    <?php
    global $messages;
    $messages = array();
    
    $messages['language'] = 'Language';
    $messages['file'] = 'File';
    
  • Unnamed arrays:

    Added in version 2.2.0.

    <?php
    return array(
       "one" => "this",
    );
    
  • Array entries without ending comma:

    Added in version 2.3.0.

    <?php
    $variable = array(
       "one" => "this",
       "two" => "that"
    );
    
  • Array entries with space before comma:

    Added in version 2.3.0.

    <?php
    $variable = array(
       "one" => "this",
       "two" => "that"   ,
    );
    
  • Nested arrays declared on the next line:

    Added in version 2.3.0.

    <?php
    $variable = array(
        "one" =>
            array(
                "two" => "dous",
            ),
    );
    
  • Nested arrays with blank entries:

    Added in version 2.3.0.

    <?php
    $variable = array(
        "one" => array(
                "" => "",
                "two" => "dous",
            ),
    );
    
  • Strings with slash asterisk on them:

    Added in version 2.3.0.

    <?php
    $variable = array(
        'foo' => 'Other value /* continued',
     );
    
  • Array entries with value on next line:

    Added in version 2.3.0.

    <?php
    $variable = array(
        'foo' =>
            'bar',
     );
    
  • Array defined in a single line:

    Added in version 2.3.0.

    <?php
    $variable = array( 'item1' => 'value1', 'item2' => 'value2', 'item3' => 'value3' );
    
  • Keyless arrays:

    Added in version 2.3.0.

    <?php
    $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    
  • Nested arrays without key for a nested array:

    Added in version 2.3.0.

    <?php
    $lang = array(array("key" => "value"));
    
  • Concatenation of strings and variables:

    Added in version 2.3.0.

    <?php
    $messages['welcome'] = 'Welcome ' . $name . '!';
    $messages['greeting'] = 'Hi ' . $name;
    
  • Assignment in the same line a multiline comment ends:

    Added in version 2.3.0.

    <?php
    /*
       Multi-line
       comment
    */ $messages['help'] = 'Help';
    
  • Keyless arrays assigned to another array:

    <?php
    $messages['days_short'] = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
    
  • Laravel plurals are supported in the LaravelPHPFile class:

    <?php
    return [
        'apples' => 'There is one apple|There are many apples',
    ];
    

Non-Conformance

The following are not yet supported:

  • There are currently no known limitations.