Skip to content

Instantly share code, notes, and snippets.

@lucianovalenca
Created April 9, 2020 19:59
Show Gist options
  • Select an option

  • Save lucianovalenca/3ba8e84cfb3fad4efc81c1d7c16979bb to your computer and use it in GitHub Desktop.

Select an option

Save lucianovalenca/3ba8e84cfb3fad4efc81c1d7c16979bb to your computer and use it in GitHub Desktop.
<?php
/*
| -------------------------------------------------------------------
| Translation service (example 1)
| -------------------------------------------------------------------
*/
$di->setShared('translation', function () use ($di) {
$translate = new TranslationService(array(
'locale' => 'en_US',
'defaultDomain' => 'messages',
'directory' => APP_PATH . '/languages',
));
return $translate;
});
/*
| -------------------------------------------------------------------
| Translation service (example 2)
| @link https://forum.phalconphp.com/discussion/11393/how-can-i-realize-specific-multilingual-support
| -------------------------------------------------------------------
*/
$di->setShared('translation', function () use ($di) {
if (!function_exists('gettext')) {
throw new \Phalcon\Exception('Please install gettext extension');
}
$request = $di->get('request');
$session = $di->get('session');
/**
* Set up the default $language
* Here I am using website service to retrieve default language from my config file
*/
$default_language = $di->get('config')->website->defaultLanguage;
/**
* If we have $language in the session, use that
* instead of the browser $language
*/
// TODO mraspor Add cookie check for locale
if ($session->has('language') && null !== $session->get('language')) {
$language = $session->get('language');
/**
* Check if the translation directory exists
*/
if(file_exists(APP_PATH . '/languages/' . $language . '.utf8') == false) {
$language = $default_language;
}
} else {
$language = $default_language;
}
/**
* Return gettext for the language
* (Fall back to the default $language if $language isn't found)
*/
$translate = new TranslationService(array(
'locale' => $language . '.utf8',
'defaultDomain' => 'messages',
'directory' => APP_PATH . '/languages',
));
return $translate;
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment