mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-04-04 15:13:40 -04:00
Add english fallback if no translation (#3995)
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
namespace Config;
|
||||
|
||||
use CodeIgniter\Config\BaseService;
|
||||
use CodeIgniter\HTTP\IncomingRequest;
|
||||
use Config\Services as AppServices;
|
||||
use HTMLPurifier;
|
||||
use HTMLPurifier_Config;
|
||||
|
||||
@@ -32,6 +34,29 @@ class Services extends BaseService
|
||||
* }
|
||||
*/
|
||||
|
||||
/**
|
||||
* Responsible for loading the language string translations.
|
||||
*
|
||||
* @return MY_Language
|
||||
*/
|
||||
public static function language(?string $locale = null, bool $getShared = true)
|
||||
{
|
||||
if ($getShared) {
|
||||
return static::getSharedInstance('language', $locale)->setLocale($locale);
|
||||
}
|
||||
|
||||
if (AppServices::get('request') instanceof IncomingRequest) {
|
||||
$requestLocale = AppServices::get('request')->getLocale();
|
||||
} else {
|
||||
$requestLocale = Locale::getDefault();
|
||||
}
|
||||
|
||||
// Use '?:' for empty string check
|
||||
$locale = $locale ?: $requestLocale;
|
||||
|
||||
return new \App\Libraries\MY_Language($locale);
|
||||
}
|
||||
|
||||
private static $htmlPurifier;
|
||||
|
||||
public static function htmlPurifier($getShared = true)
|
||||
|
||||
44
app/Libraries/MY_Language.php
Normal file
44
app/Libraries/MY_Language.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace app\Libraries;
|
||||
|
||||
use CodeIgniter\Language\Language;
|
||||
|
||||
class MY_Language extends Language {
|
||||
|
||||
public function getLine(string $line, array $args = [])
|
||||
{
|
||||
// if no file is given, just parse the line
|
||||
if ( ! str_contains($line, '.'))
|
||||
{
|
||||
return $this->formatMessage($line, $args);
|
||||
}
|
||||
|
||||
// Parse out the file name and the actual alias.
|
||||
// Will load the language file and strings.
|
||||
[$file, $parsedLine] = $this->parseLine($line, $this->locale);
|
||||
|
||||
$output = $this->getTranslationOutput($this->locale, $file, $parsedLine);
|
||||
|
||||
if ($output === NULL && strpos($this->locale, '-'))
|
||||
{
|
||||
[$locale] = explode('-', $this->locale, 2);
|
||||
|
||||
[$file, $parsedLine] = $this->parseLine($line, $locale);
|
||||
|
||||
$output = $this->getTranslationOutput($locale, $file, $parsedLine);
|
||||
}
|
||||
|
||||
// if still not found, try English
|
||||
if ($output === NULL || $output === "")
|
||||
{
|
||||
[$file, $parsedLine] = $this->parseLine($line, 'en');
|
||||
|
||||
$output = $this->getTranslationOutput('en', $file, $parsedLine);
|
||||
}
|
||||
|
||||
$output ??= $line;
|
||||
|
||||
return $this->formatMessage($output, $args);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user