1. Help Center
  2. Supported formats

PHP Array

What is a PHP array and how to implement localization with the simple PHP array format in Phrase?

File Extensions .php
API Extension php_array
Import Yes
Export Yes
Pluralization supported? No
Descriptions supported? No

 

An array is an ordered list or collection of items. The items of the array can be basically any type in PHP: a number, a string, an object, another array, etc. We often use strings as values in our locale message arrays. PHP arrays come in two major kinds:

  • Indexed — these arrays are ordered implicitly, e.g. ['red', 'green', 'blue']
  • Associative — these arrays contain pairs of keys (which can be integers or strings), and associated values, e.g. ['first_name' => 'Adam', 'last_name' => 'McMan', 'age' => 22]

You can set the value of an array element during initialization or using the variable name of the array itself.

<?php

// during initialization

$my_array = ['foo' => 'bar'];

// using variable name 

$my_second_array['key'] = 'value';

You can mix and match these methods of setting values.

How Phrase works with Arrays

When doing a phrase pull on the command line, Phrase will always send message files in the following format, using an associative, named array.

<?php

$lang['key'] = 'translated message';

$lang['another_key'] = 'Another translated message';

Make sure that your app is set up to work with this kind of format. Don’t return an anonymous array in your message files, and always use the name $lang for your messages array.

Code Sample

<?php

$lang['boolean_key'] = '--- true
';
$lang['empty_string_translation'] = '';
$lang['key_with_description'] = 'Check it out! This key has a description! (At least in some formats)';
$lang['key_with_line-break'] = 'This translations contains
a line-break.';
$lang['nested.deeply.key'] = 'Wow, this key is nested even deeper.';
$lang['nested.key'] = 'This key is nested inside a namespace.';
$lang['null_translation'] = '';
$lang['sample_collection'] = '---
- first item
- second item
- third item
';
$lang['simple_key'] = 'Just a simple key with a simple message.';
$lang['unverified_key'] = 'This translation is not yet verified and waits for it. (In some formats we also export this status)';