<?php
/**
 * CAI CP v.0.9
 * Localization class
 * 
 * build date : 2017-07-23
 *
 * @package   : System
 * @author    : Alexander I. Chebykin <alex.chebykin@gmail.com>
 * @copyright : Copyright (c) 2017 Alexander I. Chebykin
 * @version   : 1.0
 * @license   : MIT
 * @link      : https://github.com/CAI79/CAI-CP
 ******************************************************************/

namespace CAI\CAICP;

class Localization
{
    protected $cp_root;
    protected $lang;
    protected $locale;

    /**
     * Constructor
     * 
     * @param string $root_dir Control panel's root directory
     * @param string $settings Control panel settings
     */
    function __construct($root_dir, $settings)
    {
        $this->cp_root = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT') . $root_dir;

        $this->lang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);
        if (!array_key_exists($this->lang, $settings['langs'])) {
            $this->lang = $settings['lang'];
        }
        if ($this->lang == '') {
            $this->lang = 'en';
        }
        $this->locale = json_decode(file_get_contents($this->cp_root . 
                                                    '/system/json/locale/' . 
                                                    $this->lang . '.json'),
                                    true);
    }
    
    public function translate($id)
    {
        return $this->locale[$id];
    }
}
