PHP Smarty: a modifier for internationalization tool gettext
This Smarty modifier helps instantly translating Strings into other Languages using the PHP Extension 'gettext'
Overview
Usage Examples
In all Examples, the File "backend" (backend.mo) will be consulted for Translation.
Simple Output of a String:
{'Desktop'|gettext:backend}
Output using sprintf to set values dynamically:
{'(changed Menutitel; Original is `%s`)'|gettext:backend|sprintf:'123'}
{'Page %s of %s Pages'|gettext:backend|sprintf:'7':'100'}
Output with Translation into a certain Language:
{'Desktop'|gettext:backend:it_IT}
Requirements
- PHP >= 5.3
- Smarty Template Engine >= 3
- PHP Extension
php{Version}-intl
- gettext
You need to name the Place of Translationtables. That is the place where your Translationfolders reside. You can either do this inside the function (see bindtextdomain($sDomain, 'PATH_TO_MY_LANGUAGES_FOLDER');
) or do it outside by defining the appropriate constant, e.g.:
define('PATH_TO_MY_LANGUAGES_FOLDER', '/var/www/App/languages');
The structure of the Translationtables must follow the official declaration, e.g.:
Script
Save this as modifier.getText.php
and place it e.g. into the Smarty plugins folder.
You will also find this Script as a Repository on my github Account.
/**
* @author Guido K.B.W. Üffing `<info ueffing net>`
* @license GNU GENERAL PUBLIC LICENSE Version 3
*
* Smarty plugin
* Type: modifier
* Name: smarty_modifier_gettext
* Version: 1
* Date: 2013-07-19
* @see https://blog.ueffing.net/post/2013/07/19/php-smarty-a-modifier-for-internationalization-tool-gettext/
*
* @access public
* @var $sString String to be translated
* @var $sDomain e.g. "backend"; means the File (backend.mo) which will be consulted for Translation
* @var $sLang Translation into a certain Language, e.g. "de_DE"
* @return string translated String
*/
function smarty_modifier_getText ($sString, $sDomain = 'backend', $sLang = 'de_DE')
{
if (empty($sString))
{
return gettype ($sString);
}
// requires installation of php module: php{Version}-intl (and maybe libicu52)
//
// This function needs a "BCP 47 compliant language tag"
// what is per definition, using a dash instead of an underscore
// @see http://www.php.net/manual/de/locale.setdefault.php
// http://en.wikipedia.org/wiki/IETF_language_tag
\Locale::setDefault(str_replace('_', '-', $sLang));
// Setting the proper Codeset
// here, don't use a dash '-'
$sCodeset = 'UTF8';
putenv('LANG=' . $sLang . '.' . $sCodeset);
putenv('LANGUAGE=' . $sLang . '.' . $sCodeset);
// set Codeset
bind_textdomain_codeset($sDomain, $sCodeset);
// name the Place of Translationtables
// That is where your Translationfolders reside
bindtextdomain($sDomain, '/tmp'); # flush first
bindtextdomain($sDomain, 'PATH_TO_MY_LANGUAGES_FOLDER');
// set locale
setlocale(LC_MESSAGES, ""); # flush first
setlocale(LC_MESSAGES, $sLang.'.'.$sCodeset);
// Translation will be loaded from
// e.g.: /var/www/App/languages/de_DE/LC_MESSAGES/backend.mo
textdomain($sDomain);
// return, so that further modifiers could handle it
return gettext($sString);
}
Links
- smarty_modifier_getText on github: https://github.com/gueff/smarty_modifier_getText
- PHP.net: http://php.net/
- Smarty Template Engine: http://www.smarty.net/
- Smarty Plugins: http://www.smarty.net/docs/en/plugins
- gettext: https://www.gnu.org/software/gettext/