1. Go to this page and download the library: Download mnapoli/doctrine-translated 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/ */
mnapoli / doctrine-translated example snippets
namespace Acme\Model;
/**
* @Entity
*/
class Product
{
/**
* @var TranslatedString
* @Embedded(class = "Acme\Model\TranslatedString")
*/
protected $name;
public function __construct()
{
$this->name = new TranslatedString();
}
public function getName()
{
return $this->name;
}
}
namespace Acme\Model;
/**
* @Embeddable
*/
class TranslatedString extends \Mnapoli\Translated\AbstractTranslatedString
{
/**
* @Column(type = "string", nullable=true)
*/
public $en;
/**
* @Column(type = "string", nullable=true)
*/
public $fr;
}
$product = new Product();
$product->getName()->en = 'Some english here';
$product->getName()->fr = 'Un peu de français là';
echo $product->getName()->en;
// The default locale is "en" (you can provide a locale like "en_US" too, it will be parsed)
$translator = \Mnapoli\Translated\Translator('en');
// If a user is logged in, we can set the locale to the user's one
$translator->setLanguage('fr');
$str = new TranslatedString();
$str->en = 'foo';
$str->fr = 'bar';
// No need to manipulate the locale here
echo $translator->get($str); // foo
$extension = new \Mnapoli\Translated\Integration\Twig\TranslatedTwigExtension($translator);
$twig->addExtension($extension);
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
new \Mnapoli\Translated\Integration\Symfony2\TranslatedBundle(),
];
// ...
class LocaleListener
{
private $translator;
private $session;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
public function setSession(Session $session)
{
$this->session = $session;
}
public function onRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$locale = $request->getSession()->get('_locale');
if ($locale) {
$this->translator->setLanguage($locale);
}
}
public function onLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
$lang = $user->getLanguage();
if ($lang) {
$this->session->set('_locale', $lang);
}
}
}
// In your Bootstrap
protected function _initViewHelpers()
{
$this->bootstrap('View');
// Create or get $translator (\Mnapoli\Translated\Translator)
// Create the helper
$helper = new Mnapoli\Translated\Integration\Zend1\TranslateZend1Helper($translator);
// The view helper will be accessible through the name "translate"
$this->getResource('view')->registerHelper($helper, 'translate');
}
echo $this->translate($someTranslatedString);
// Get the translation for the current locale
echo $translator->get($str);
// Set the translation for the current locale
$translator->set($str, 'Hello');
// Set the translation for several locales
$translator->setMany($str, [
'en' => 'Hello',
'fr' => 'Salut',
]);
$str = $translator->set(new TranslatedString(), 'Hello');
// Same as:
$str = new TranslatedString();
$translator->set($str, 'Hello');
$str1 = new TranslatedString();
$str1->en = 'Hello';
$str1->fr = 'Bonjour';
// $result is a TranslatedString
$result = $str1->concat(' ', $user->getName());
// Will echo "Hello John" or "Bonjour John" according to the locale
echo $translator->get($result);
$result = TranslatedString::join([
new TranslatedString('Hello', 'en'),
'!'
]);
public function getParentLabel() {
if ($this->parent === null) {
return '-';
}
return $this->parent->getLabel();
}
return TranslatedString::untranslated('-');
$translator = new Translator('en', [
'fr' => ['en'], // french fallbacks to english if not found
'es' => ['fr', 'en'], // spanish fallbacks to french, then english if not found
]);
$str = new TranslatedString();
$str->en = 'Hello!';
// Will show nothing (no FR value)
echo $str->fr;
$translator->setLanguage('fr');
// Will show "Hello!" because the french falls back to english if not defined
echo $translator->get($str);
// Nothing
echo $str->fr;
// Nothing
echo $str->get('fr');
// Will show "Hello!" (the fallback is "en")
echo $str->get('fr', ['en']);
$query = $em->createQuery(sprintf(
"SELECT p FROM Product p WHERE p.name.%s = 'Hello'",
$lang
));
$products = $query->getResult();
$query = $em->createQuery(sprintf(
"SELECT p FROM Product p ORDER BY p.name.%s ASC",
$lang
));
$products = $query->getResult();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.