Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

Translation class

This class allows to set simple translation for any project. It uses files with JSON encoded array for each language. - The new keywords are automatically added to the language file. - The class also enables translation of text with placeholders. Very useful for templates. - The translations are available thru shot named static methods. Usage example: Set the default language:
t::set_language('en', '/path/to/language/files/');
Set the translations file:
$translations = ['TEST1' => 'Test one in English'];
t::set_translations('bg', $translations);
Get keyword translation:
echo t::t('TEST1');
Get text placeholders replacement:
echo t::tt('Hello %%TEST1%%!');
<?php
/**
 * This class allowes simple translation using json encoded arrays in files 
 * @package Sami's encrypt class
 * @version $Id: t.php v.1.1 2018-10-06 16:20:00 $
 * @author Samuil Banti https://www.samiwell.eu/
 * @copyright (C) 2018 - Samuil Banti
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/
class t {

    /**
    * Path to the language files:
    */
    private static $lang_path = false;

    /**
    * The default language to be translated to.
    */
    private static $lang = false;

    /**
    * Avoud accepting t() functon as a constructor.
    */
    public function __construct()
    {
    
    }
    
    public static function set_language($lang, $lang_path)
    {
        self::$lang = $lang;
        self::$lang_path = $lang_path;
    }

    /**
    * Translate keyword. If the keyword is not available it will be added to the language file.
    * @param string $keyword - The keyword to translate.
    * @param string $lang - Language different than the default one.
    * @return string - The keyword translation.
    */
    public static function t($keyword, $lang = false)
    {
        $translations = self::get_translations();
        if(!isset($translations[$keyword])) {
            self::set_new_keyword($keyword);
            return $keyword;
        }
        return $translations[$keyword];
    }

    /**
    * Translate text with placeholders in format %%KEYWORD%%:
    * @param string $text - The text template with placeholders in format %%KEYWORD%%.
    * @param string $lang - Language different than the default one.
    * @return string - The text with replaced placeholders.
    */
    public static function tt($text, $lang = false)
    {
        $translations = self::get_translations();
        $placeholders = array_keys($translations);
        foreach($placeholders as $k => $v) {
            $placeholders[$k] = "%%{$v}%%";
        }
        return str_replace($placeholders, $translations, $text);
    }

    /**
    * This function is used to set the translation file.
    * WARNIING: This function will replace the whole laguage file content.
    * @param array $lang - The langage that will be translated.
    * @param array $translations - The translations to be set in the file.
    */
    public static function set_translations($lang, $translations = [])
    {
        $lang_file = self::$lang_path.$lang;
        file_put_contents($lang_file, json_encode($translations));
    }
    
    /**
    * Get all available translations
    * @return array $translations - All available translations.
    */
    private function get_translations()
    {
        $lang_file = self::$lang_path.self::$lang;
        if(!file_exists($lang_file)) {
            file_put_contents($lang_file, json_encode([]));
            return [];
        }
        $translations = file_get_contents($lang_file);
        $translations = json_decode($translations, true);
        return $translations;
    }
    
    /**
    * Add unknown keyword to the translations file.
    * @param string $keyword - The keyword to be added to file.
    */
    private function set_new_keyword($keyword)
    {
        $translations = self::get_translations();
        $translations[$keyword] = $keyword;
        self::set_translations(self::$lang, $translations);
    }
    
}
Download...