Skip to content

Working with the Translator

kirkbushell edited this page Nov 4, 2014 · 6 revisions

The translator is the endpoint of the Localisation API, defined at the Localisation package. But to ensure we're on the same page, we'll add it here as well.

In order to translate a collection or model, simply use the Translator facade:

<?php
use Translator;

$content = Content::find(1);
$translatedContent = Translator::translate($content);

You can return this directly in your controller actions (if you're running a set of API responses), and this will automatically create the JSON for you, necessary for your responses.

The default behaviour is to return all translations for a given content piece. This is handy for managing translations in a user interface. You can, however - simply restrict it to the current user's language.

<?php
$translatedContent = Translator::translate($content, 'en_GB');

This will return only the translations for English (Great Britain), if you have translations for that content stored in your database.

You can also define default languages, just in case the requested language translations are not available to a given user. To do this, you must tell the Translator what the default language is.

``php Translator::setDefaultLanguage('en_GB');


In this manner, the behaviour for returning translations changes slightly. If none can be found for the desired language, then it will return the default language translations.
  1. Home
    1. Objectives of this package
    2. [Who this package is for](Who this package is for)
    3. [Working with the Translator](Working with the Translator)
    4. [How it works](How it works)
Clone this wiki locally