2017-08-04 16:28:16 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-10-31 16:22:19 +01:00
|
|
|
use Carbon\Carbon;
|
2020-07-07 18:07:56 +02:00
|
|
|
use Carbon\CarbonInterval;
|
2018-10-31 16:22:19 +01:00
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
|
|
|
* Classe per gestire le traduzioni del progetto.
|
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
class Translator extends Util\Singleton
|
|
|
|
{
|
|
|
|
/** @var Intl\Formatter Oggetto per la conversione di date e numeri nella lingua selezionata */
|
2017-09-19 16:20:44 +02:00
|
|
|
protected static $formatter;
|
2019-04-13 00:09:48 +02:00
|
|
|
/** @var string Simbolo della valuta corrente */
|
|
|
|
protected static $currency;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
/** @var Symfony\Component\Translation\Translator Oggetto dedicato alle traduzioni */
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
/** @var array Lingue disponibili */
|
|
|
|
protected $locales = [];
|
|
|
|
/** @var string Lingua selezionata */
|
|
|
|
protected $locale;
|
|
|
|
|
2019-08-02 15:58:31 +02:00
|
|
|
public function __construct($default_locale = 'it_IT', $fallback_locales = ['it_IT'])
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-10 18:29:51 +02:00
|
|
|
$translator = new Symfony\Component\Translation\Translator($default_locale);
|
|
|
|
$translator->setFallbackLocales($fallback_locales);
|
|
|
|
// Imposta la classe per il caricamento
|
|
|
|
$translator->addLoader('default', new Intl\FileLoader());
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-10 18:29:51 +02:00
|
|
|
$this->translator = $translator;
|
2018-12-21 14:58:22 +01:00
|
|
|
|
|
|
|
$this->locale = $default_locale;
|
|
|
|
self::setFormatter($default_locale, []);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ricerca e aggiunge le traduzioni presenti nei percorsi predefiniti (cartella locale sia nella root che nei diversi moduli).
|
|
|
|
*
|
2018-06-27 18:58:50 +02:00
|
|
|
* @param string $string
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
|
|
|
public function addLocalePath($string)
|
|
|
|
{
|
|
|
|
$paths = glob($string);
|
|
|
|
foreach ($paths as $path) {
|
|
|
|
$this->addLocales($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'elenco dei linguaggi disponibili.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAvailableLocales()
|
|
|
|
{
|
|
|
|
return $this->locales;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controlla se il linguaggio indicato è disponibile.
|
|
|
|
*
|
|
|
|
* @param string $language
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isLocaleAvailable($language)
|
|
|
|
{
|
|
|
|
return in_array($language, $this->getAvailableLocales());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imposta il linguaggio in utilizzo.
|
|
|
|
*
|
|
|
|
* @param string $locale
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function setLocale($locale, $formatter = [])
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
|
|
|
if (!empty($locale) && $this->isLocaleAvailable($locale)) {
|
2017-09-10 18:29:51 +02:00
|
|
|
$this->translator->setLocale($locale);
|
2017-08-04 16:28:16 +02:00
|
|
|
$this->locale = $locale;
|
2017-09-19 16:20:44 +02:00
|
|
|
|
2020-03-19 13:04:59 +01:00
|
|
|
$result = setlocale(LC_TIME, $locale);
|
2019-11-22 16:28:03 +01:00
|
|
|
Carbon::setLocale($locale);
|
|
|
|
|
|
|
|
if (empty($result)) {
|
2020-03-19 13:04:59 +01:00
|
|
|
$result = setlocale(LC_TIME, $locale.'.UTF-8');
|
2020-03-16 10:32:35 +01:00
|
|
|
} else {
|
|
|
|
Carbon::setUtf8(true);
|
2020-03-14 14:59:27 +01:00
|
|
|
}
|
2019-11-22 16:28:03 +01:00
|
|
|
|
2020-07-07 18:07:56 +02:00
|
|
|
$reduced = explode('_', $locale)[0];
|
|
|
|
CarbonInterval::setLocale($reduced);
|
|
|
|
|
2020-03-14 14:59:27 +01:00
|
|
|
if (empty($result)) {
|
2020-03-19 13:04:59 +01:00
|
|
|
$result = setlocale(LC_TIME, $reduced);
|
2019-11-22 16:28:03 +01:00
|
|
|
}
|
2018-10-31 16:22:19 +01:00
|
|
|
|
2018-12-21 14:58:22 +01:00
|
|
|
self::setFormatter($locale, $formatter);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il linguaggio attualmente in utilizzo.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCurrentLocale()
|
|
|
|
{
|
|
|
|
return $this->locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'oggetto responsabile della gestione delle traduzioni.
|
|
|
|
*
|
|
|
|
* @return Symfony\Component\Translation\Translator
|
|
|
|
*/
|
|
|
|
public function getTranslator()
|
|
|
|
{
|
|
|
|
return $this->translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce la traduzione richiesta.
|
|
|
|
*
|
|
|
|
* @param string $string
|
2020-09-23 13:36:37 +02:00
|
|
|
* @param array $parameters
|
|
|
|
* @param array $operations
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-10 14:35:41 +02:00
|
|
|
public static function translate($string, $parameters = [], $operations = [])
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-10 18:29:51 +02:00
|
|
|
$result = self::getInstance()->getTranslator()->trans($string, $parameters);
|
2017-09-10 14:35:41 +02:00
|
|
|
|
|
|
|
// Operazioni aggiuntive sul risultato
|
|
|
|
if (!empty($operations)) {
|
|
|
|
$result = new Stringy\Stringy($result);
|
|
|
|
|
|
|
|
if (!empty($operations['upper'])) {
|
|
|
|
$result = $result->toUpperCase();
|
|
|
|
} elseif (!empty($operations['lower'])) {
|
|
|
|
$result = $result->toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) $result;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 14:58:22 +01:00
|
|
|
/**
|
|
|
|
* Restituisce l'oggetto responsabile della localizzazione di date e numeri.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return Intl\Formatter
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public static function getFormatter()
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::$formatter;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 00:09:48 +02:00
|
|
|
/**
|
|
|
|
* Restituisce il simbolo della valuta del gestione.
|
|
|
|
*
|
|
|
|
* @since 2.4.9
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getCurrency()
|
|
|
|
{
|
|
|
|
if (!isset(self::$currency)) {
|
|
|
|
$id = setting('Valuta');
|
|
|
|
$valuta = database()->fetchOne('SELECT symbol FROM zz_currencies WHERE id = '.prepare($id));
|
|
|
|
|
|
|
|
self::$currency = $valuta['symbol'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$currency;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
|
|
|
* Converte il numero dalla formattazione locale a quella inglese.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function numberToEnglish($string)
|
|
|
|
{
|
2017-09-21 10:01:07 +02:00
|
|
|
return self::getFormatter()->parseNumber($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte il numero dalla formattazione inglese a quella locale.
|
|
|
|
*
|
2018-06-29 13:35:39 +02:00
|
|
|
* @param string $string
|
|
|
|
* @param string|int $decimals
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public static function numberToLocale($string, $decimals = null)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
|
|
|
$string = !isset($string) ? 0 : $string;
|
|
|
|
|
2018-06-29 13:35:39 +02:00
|
|
|
if (!empty($decimals) && is_string($decimals)) {
|
2018-07-09 10:44:54 +02:00
|
|
|
$decimals = ($decimals == 'qta') ? setting('Cifre decimali per quantità') : null;
|
2018-06-29 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->formatNumber($string, $decimals);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte la data dalla formattazione locale a quella inglese.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function dateToEnglish($string)
|
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->parseDate($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte la data dalla formattazione inglese a quella locale.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @param string $fail
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public static function dateToLocale($string)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->formatDate($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte la data dalla formattazione locale a quella inglese.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function timeToEnglish($string)
|
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->parseTime($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte la data dalla formattazione inglese a quella locale.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @param string $fail
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public static function timeToLocale($string)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->formatTime($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte un timestamp dalla formattazione locale a quella inglese.
|
|
|
|
*
|
|
|
|
* @param string $timestamp
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function timestampToEnglish($string)
|
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->parseTimestamp($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte un timestamp dalla formattazione inglese a quella locale.
|
|
|
|
*
|
|
|
|
* @param string $timestamp
|
|
|
|
* @param string $fail
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public static function timestampToLocale($string)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return self::getFormatter()->formatTimestamp($string);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
2018-12-29 12:03:22 +01:00
|
|
|
|
2021-07-07 07:57:10 +02:00
|
|
|
/**
|
|
|
|
* Converte il numero in ore.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function numberToHours($string)
|
|
|
|
{
|
|
|
|
$string = !isset($string) ? 0 : $string;
|
|
|
|
|
|
|
|
$ore = number_format($string, 2);
|
|
|
|
$splitted_hour = explode('.', $ore);
|
|
|
|
$hour = $splitted_hour[0];
|
|
|
|
$minutes = ($splitted_hour[1] / 100) * 60;
|
|
|
|
$time = $hour.':'.sprintf('%02d', $minutes);
|
|
|
|
|
|
|
|
return $time;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
/**
|
|
|
|
* Aggiunge i contenuti della cartella specificata alle traduzioni disponibili.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
protected function addLocales($path)
|
|
|
|
{
|
|
|
|
// Individua i linguaggi disponibili
|
|
|
|
$dirs = glob($path.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
|
|
|
|
foreach ($dirs as $dir) {
|
|
|
|
$this->addLocale(basename($dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggiunge le singole traduzioni
|
|
|
|
foreach ($this->locales as $lang) {
|
|
|
|
$done = [];
|
|
|
|
|
|
|
|
$files = glob($path.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.'*.*');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (!in_array(basename($file), $done)) {
|
|
|
|
$this->translator->addResource('default', $file, $lang);
|
|
|
|
|
|
|
|
$done[] = basename($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Aggiunge il linguaggio indicato all'elenco di quelli disponibili.
|
|
|
|
*
|
|
|
|
* @param string $language
|
|
|
|
*/
|
|
|
|
protected function addLocale($language)
|
|
|
|
{
|
|
|
|
if (!$this->isLocaleAvailable($language)) {
|
|
|
|
$this->locales[] = $language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imposta l'oggetto responsabile della localizzazione di date e numeri.
|
|
|
|
*/
|
|
|
|
protected static function setFormatter($locale, $options)
|
|
|
|
{
|
|
|
|
self::$formatter = new Intl\Formatter(
|
|
|
|
$locale,
|
|
|
|
empty($options['timestamp']) ? 'd/m/Y H:i' : $options['timestamp'],
|
|
|
|
empty($options['date']) ? 'd/m/Y' : $options['date'],
|
|
|
|
empty($options['time']) ? 'H:i' : $options['time'],
|
|
|
|
empty($options['number']) ? [
|
|
|
|
'decimals' => ',',
|
|
|
|
'thousands' => '.',
|
|
|
|
] : $options['number']
|
|
|
|
);
|
|
|
|
|
|
|
|
self::$formatter->setPrecision(auth()->check() ? setting('Cifre decimali per importi') : 2);
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|