PHP Smarty: a modifier for internationalization tool gettext

2018-04-22 12:30:18 PHP,Smarty,Internationalization,i18n,Locale,gettext,EN

This Smarty modifier helps instantly translating Strings into other Languages using the PHP Extension 'gettext'


Usage Examples

In all Examples, the File "backend" ( will be consulted for Translation.

Simple Output of a String:


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:



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.: php gettext translation-files structure


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
 * @access public
 * @var $sString String to be translated
 * @var $sDomain e.g. "backend"; means the File ( 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 
    \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/

    // return, so that further modifiers could handle it
    return gettext($sString);


Diese Website verwendet Cookies, um Ihnen den bestmöglichen Service zu gewährleisten. Weiterführende Informationen finden Sie in unserer Datenschutzerklärung. Klicken Sie in die nachfolgende Checkbox um Cookies zu akzeptieren. Bestätigen Sie anschließend durch Klick auf "Speichern".