PHP code example of tableau-mkt / eggs-n-cereal

1. Go to this page and download the library: Download tableau-mkt/eggs-n-cereal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tableau-mkt / eggs-n-cereal example snippets


// Generated by composer.
slatable here.
$yourTranslatable = new YourTranslatable(/*...*/);

// Instantiate the serializer:
$xliffSerializer = new EggsCereal\Serializer();
$targetLanguage = 'pt-br';

// Serialize your translatable like so:
$xlf = $xliffSerializer->serialize($yourTranslatable, $targetLanguage);

// Unserialize an xliff file like so:
$translatedFile = file_get_contents('/path/to/translated-pt-br.xlf');
$xliffSerializer->unserialize($yourTranslatable, $targetLanguage, $translatedFile);

use EggsCereal\Interfaces\TranslatableInterface;

class ArrayTranslatable implements TranslatableInterface {

  public $data = array();

  public function __construct(array $data) {
    $this->data = $data;
  }

  /**
   * {@inheritdoc}
   */
  public function getData() {
    $response = array();

    // Iterate through each item in the array.
    foreach ($data as $key => $value) {
      // For each item, set a #label and #text value.
      $response[$key] = array(
        '#label' => $key,
        '#text' => $value,
      );
    }

    return $response;
  }

  /**
   * {@inheritdoc}
   */
  public function setData(array $data, $targetLanguage) {
    foreach ($data as $key => $value) {
      $this->data[$key] = $value['#text'],
    }
  }

  // Note, you'll also need implementations for getIdentifier() and getLabel().
}

$xliffSerializer = new EggsCereal\Serializer();
$targetLang = 'fr-fr';

$arrayTranslatable = new ArrayTranslatable(array(
  'foo' => 'Translatable foo value',
  'bar' => 'Translatable bar value',
));

// Generate an XLIFF file from your translatable.
$xlf = $xliffSerializer->serialize($arrayTranslatable, $targetLang);

// Import a translated XLIFF file.
$translatedFile = file_get_contents('/path/to/translated-array-fr-fr.xlf');
$xliffSerializer->unserialize($translatable, $targetLang, $translatedFile);

// Now, your array will be translated and might be available like so:
print_r($arrayTranslatable->data);
array(
  'foo' => 'Valeur de foo traduisible',
  'bar' => 'Valeur de bar traduisible',
);