2020-09-04 11:26:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-14 19:45:30 +00:00
|
|
|
/*
|
2022-02-06 20:37:02 +00:00
|
|
|
This file is a part of myTinyTodo.
|
|
|
|
|
(C) Copyright 2020-2022 Max Pozdeev <maxpozdeev@gmail.com>
|
|
|
|
|
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
|
2022-01-14 19:45:30 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-09-04 11:26:28 +00:00
|
|
|
/*
|
2022-02-06 20:37:02 +00:00
|
|
|
myTinyTodo language class
|
2020-09-04 11:26:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class Lang
|
|
|
|
|
{
|
2022-02-06 20:37:02 +00:00
|
|
|
protected static $instance;
|
2022-07-10 14:57:36 +00:00
|
|
|
protected static $langDir = MTTINC . 'lang/';
|
2022-02-06 20:37:02 +00:00
|
|
|
protected $code = 'en';
|
|
|
|
|
protected $default = 'en';
|
|
|
|
|
protected $strings;
|
|
|
|
|
|
|
|
|
|
public static function instance()
|
|
|
|
|
{
|
2020-09-04 11:26:28 +00:00
|
|
|
if (!isset(self::$instance)) {
|
2022-02-06 20:37:02 +00:00
|
|
|
$c = __CLASS__;
|
|
|
|
|
self::$instance = new $c;
|
2020-09-04 11:26:28 +00:00
|
|
|
}
|
2022-02-06 20:37:02 +00:00
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function loadLangOrDie($code, $die = 1)
|
|
|
|
|
{
|
|
|
|
|
$lang = self::instance();
|
|
|
|
|
|
|
|
|
|
//check if json file exists
|
|
|
|
|
if ( self::langExists($code) ) {
|
|
|
|
|
$jsonString = file_get_contents( self::$langDir. "{$code}.json" );
|
|
|
|
|
$lang->loadJsonString($code, $jsonString);
|
|
|
|
|
}
|
|
|
|
|
else if ( $die == 0 ) {
|
|
|
|
|
//notice?
|
|
|
|
|
$lang->code = $lang->default; //sure?
|
|
|
|
|
$lang->loadDefaultStrings();
|
|
|
|
|
}
|
|
|
|
|
else if ( $die == 1 ) {
|
|
|
|
|
die("Language file not found (". htmlspecialchars($code). ".json)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function loadLang($code)
|
|
|
|
|
{
|
|
|
|
|
self::loadLangOrDie($code, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function langExists($code)
|
|
|
|
|
{
|
|
|
|
|
return file_exists(self::$langDir. $code. '.json');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadJsonString($code, $jsonString)
|
|
|
|
|
{
|
|
|
|
|
$this->code = $code;
|
|
|
|
|
$json = json_decode($jsonString, true);
|
2022-04-15 18:29:59 +00:00
|
|
|
if ($json === null) {
|
2022-07-10 12:35:19 +00:00
|
|
|
$json = array();
|
|
|
|
|
}
|
2022-02-06 20:37:02 +00:00
|
|
|
|
|
|
|
|
//load default language
|
|
|
|
|
if ( $code != $this->default ) {
|
|
|
|
|
$this->loadDefaultStrings();
|
|
|
|
|
$this->strings = array_replace($this->strings, $json);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$this->strings = $json;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadDefaultStrings()
|
|
|
|
|
{
|
|
|
|
|
if ( ! self::langExists($this->default) ) {
|
|
|
|
|
die("Default language file not found (". htmlspecialchars($this->default). ".json)");
|
|
|
|
|
}
|
|
|
|
|
$defStr = file_get_contents($this->langDir(). "{$this->default}.json");
|
|
|
|
|
$this->strings = json_decode($defStr, true);
|
2022-04-15 18:29:59 +00:00
|
|
|
if ($this->strings === null) {
|
2022-07-10 12:35:19 +00:00
|
|
|
die("Invalid JSON in default language file (". htmlspecialchars($this->default). ".json)");
|
|
|
|
|
}
|
2022-02-06 20:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get($key)
|
|
|
|
|
{
|
|
|
|
|
if ( isset($this->strings[$key]) ) {
|
|
|
|
|
return $this->strings[$key];
|
|
|
|
|
}
|
|
|
|
|
return $key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function rtl()
|
|
|
|
|
{
|
|
|
|
|
if ( isset($this->strings['_rtl']) ) {
|
|
|
|
|
return intval($this->strings['_rtl']);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* minimal number of translated strings to use in js front-end */
|
|
|
|
|
function jsStrings()
|
|
|
|
|
{
|
|
|
|
|
$a = array();
|
|
|
|
|
$a['daysMin'] = $this->get('days_min');
|
|
|
|
|
$a['daysLong'] = $this->get('days_long');
|
|
|
|
|
$a['monthsShort'] = $this->get('months_short');
|
2022-08-01 09:48:58 +00:00
|
|
|
$a['monthsLong'] = $this->get('months_calendar');
|
2022-02-06 20:37:02 +00:00
|
|
|
|
|
|
|
|
$this->fillWithValues($a, [
|
|
|
|
|
'confirmDelete',
|
|
|
|
|
'confirmLeave',
|
|
|
|
|
'actionNoteSave',
|
|
|
|
|
'actionNoteCancel',
|
|
|
|
|
'error',
|
|
|
|
|
'denied',
|
|
|
|
|
'listNotFound',
|
|
|
|
|
'noPublicLists',
|
|
|
|
|
'invalidpass',
|
|
|
|
|
'addList',
|
|
|
|
|
'addListDefault',
|
|
|
|
|
'renameList',
|
|
|
|
|
'deleteList',
|
|
|
|
|
'clearCompleted',
|
|
|
|
|
'settingsSaved',
|
|
|
|
|
'tags',
|
|
|
|
|
'tasks',
|
|
|
|
|
'f_past',
|
|
|
|
|
'f_today',
|
|
|
|
|
'f_soon',
|
|
|
|
|
'alltasks',
|
|
|
|
|
'set_header'
|
|
|
|
|
]);
|
|
|
|
|
$a['_rtl'] = $this->rtl() ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
return $a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makeJS($pretty = 0)
|
|
|
|
|
{
|
|
|
|
|
$a = $this->jsStrings();
|
|
|
|
|
$opts = JSON_UNESCAPED_UNICODE;
|
|
|
|
|
if ($pretty) {
|
|
|
|
|
$opts |= JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES;
|
|
|
|
|
}
|
|
|
|
|
return json_encode($a, $opts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function fillWithValues(array &$a, array $keys)
|
|
|
|
|
{
|
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
|
$a[$key] = $this->get($key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function langDir()
|
|
|
|
|
{
|
|
|
|
|
return self::$langDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function langCode()
|
|
|
|
|
{
|
|
|
|
|
return $this->code;
|
|
|
|
|
}
|
2020-09-04 11:26:28 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|