Giter VIP home page Giter VIP logo

eggs-n-cereal's Introduction

Eggs'n'Cereal

A basic, generic PHP XLIFF serialization library.

Installation

The recommended way to install Eggs'n'Cereal is of course to use Composer:

{
  "require": {
    "tableau-mkt/eggs-n-cereal": "@dev"
  }
}

Note: There is no stable release, necessarily. So...

Usage

The basic idea of this library is that you provide a series of "translatable" classes for your entities. These "translatable" classes implement the EggsCereal\Interfaces\TranslatableInterface interface.

A TranslatableInterface instance is meant to wrap your entity with a unified method to get and set data. You must do so by implementing:

  • TranslatableInterface::getData()
  • TranslatableInterface::setData()

In addition to getting and setting data, you must also provide a way to get a unique identifier for your translatable entity, as well as a label by implementing:

  • TranslatableInterface::getIdentifier()
  • TranslatableInterface::getLabel()

The identifier and label are used to validate an XLIFF file during the import / unserialization process.

Once you've implemented the interface, you can serialize and unserialize your translatable like so:

// Generated by composer.
require_once('vendor/autoload.php');

// Instantiate your translatable 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);

Sample implementation

Suppose you want to translate data stored in a flat, single-level PHP array. You might write an ArrayTranslatable class like so:

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().
}

With this ArrayTranslatable implementation, usage is straightforward.

$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',
);

Forewarning

This library is a work in progress and draws heavily from work by Cloudwords on their Cloudwords for Multilingual Drupal module.

Use at your own risk, for now.

eggs-n-cereal's People

Contributors

iameap avatar sayten avatar svc-scm avatar

Stargazers

Fred Carlsen avatar Nico Müller avatar Chase Tingley avatar Paolo Agostinetto avatar Gerardo Moad avatar Andrew Macpherson avatar

Watchers

Joel avatar Gary Gao avatar  avatar James Cloos avatar  avatar Thomas Wagner avatar Eric F avatar  avatar Laud Tetteh avatar Angela Wilson avatar Isaac Cheung avatar Blake L. avatar Shannon Dunn avatar

eggs-n-cereal's Issues

How use unserialize only

Hi, i have use xlf translition for my php app. In you sample
$yourTranslatable = new YourTranslatable();

Fatal error: Class 'YourTranslatable' not found

Support custom source language

Currently, source language is hard-coded as "en," but that's not very international of us!

Perhaps we just add a "source language" property and take it in as an optional constructor argument, defaulting to the pre-existing "en."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.