Classes

File core/lang/LangLoader.class.php

File core/lang/LangLoader.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 
<?php
/**
 * @package     Core
 * @subpackage  Lang
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 10 30
 * @since       PHPBoost 3.0 - 2009 09 29
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class LangLoader
{
    private static $locale = '';

    /**
     * @var CacheFactory
     */
    private static $ram_cache = null;

    /**
     * sets the language locale
     * @param string $locale the locale
     */
    public static function set_locale($locale)
    {
        self::$locale = in_array($locale, self::get_available_langs()) ? $locale : self::get_default_lang();
    }

    /**
     * Returns the current language locale
     * @return string the current language locale
     */
    public static function get_locale()
    {
        return self::$locale ? self::$locale : self::get_default_lang();
    }

    public static function get_available_langs()
    {
        $langs_folder = new Folder(PATH_TO_ROOT . '/lang');
        $langs_list = $langs_folder->get_folders();

        $available_langs = array();
        foreach ($langs_list as $lang)
        {
            $available_langs[] = $lang->get_name();
        }

        return $available_langs;
    }

    public static function get_default_lang()
    {
        $langs = self::get_available_langs();
        return $langs[0];
    }

    /**
     * @param string $message_id the language message identifier
     * @param string $filename the language filename
     * @param string $module the module to look for languages files in
     * @return string the localized message
     */
    public static function get_message($message_id, $filename, $module = '')
    {
        $lang = self::get($filename, $module);
        if (!isset($lang[$message_id]))
        {
            $lang = self::get($filename, $module, 'real_lang');
            if (!isset($lang[$message_id]))
                $lang = self::get($filename, $module, 'default');
        }
        return $lang[$message_id];
    }

    /**
     * Retrieves the language file <code>$filename</code> in
     * <code>/$module/lang/$locale/$filename.php</code>
     * If module is empty, the kernel lang folder will be used
     * @param string $filename the language filename
     * @param string $module the module to look for languages files in
     * @param string $forced_file the language filename to return inevitably
     * @return string[string] the lang array which keys are languages identifiers and values the
     * translated messages
     */
    public static function get($filename, $module = '', $forced_file = '')
    {
        $module_name = trim($module, '/');
        return self::get_raw($module_name, $filename, $forced_file);
    }

    private static function get_raw($folder, $filename, $forced_file = '')
    {
        $lang_id = $folder . '/' . $filename;
        $ram_cache = self::get_ram_cache();
        if (!$ram_cache->contains($lang_id))
        {
            self::load($lang_id, $folder, $filename, $forced_file);
        }
        return $ram_cache->get($lang_id);
    }

    private static function load($lang_id, $folder, $filename, $forced_file = '')
    {
        $lang = array();
        include self::get_real_lang_path($folder, $filename, $forced_file);
        if (empty($lang) && !empty($LANG) && is_array($LANG))
        {
            $lang = $LANG;
        }
        self::get_ram_cache()->store($lang_id, $lang);
    }

    /**
     * returns the real language file path, trying first to load the localized language file
     * and if it's not possible, use the default locale one.
     * @param string $folder the folder to look in
     * @param string $filename the language filename
     * @param string $forced_file the language filename to return inevitably
     * @return string the real language file path
     */
    private static function get_real_lang_path($folder, $filename, $forced_file = '')
    {
        $real_folder = PATH_TO_ROOT . (!empty($folder) ? '/' . $folder : '') . '/lang/';
        $filename_with_extension = '/' . $filename . '.php';

        if (!empty($folder) && empty($forced_file))
        {
            // Module - Langs priority order
            //      /lang/$lang/modules/$module/$file.php
            //      /$module/lang/$lang/$file.php
            $real_lang_file = PATH_TO_ROOT . '/lang/' . self::$locale . '/modules/' . $folder . $filename_with_extension;
            if (file_exists($real_lang_file))
            {
                return $real_lang_file;
            }
        }

        if (empty($forced_file) || $forced_file = 'real_lang')
        {
            $real_lang_file = $real_folder . self::$locale . $filename_with_extension;
            if (file_exists($real_lang_file))
            {
                return $real_lang_file;
            }
        }

        // Get default lang file if nothing else is found
        if (empty($forced_file) || $forced_file = 'default')
        {
            $real_lang_file = $real_folder . self::get_default_lang() . $filename_with_extension;
            if (file_exists($real_lang_file))
            {
                return $real_lang_file;
            }
        }

        throw new LangNotFoundException($folder, $filename);
    }

    /**
     * clear the lang cache (for unit test only)
     */
    public static function clear_lang_cache()
    {
        self::$ram_cache = null;
        self::get_ram_cache();
    }

    /**
     * @return RAMDataStore
     */
    private static function get_ram_cache()
    {
        if (self::$ram_cache === null)
        {
            self::$ram_cache = new RAMDataStore('lang');
        }
        return self::$ram_cache;
    }
}
?>