Merge pull request #8026 from nupplaphil/task/l10n_immutable

Make L10n immutable
This commit is contained in:
Hypolite Petovan 2019-12-29 13:54:32 -05:00 committed by GitHub
commit a9220aa83b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 171 deletions

View file

@ -24,30 +24,15 @@ class L10n extends BaseObject
}
/**
* This function should be called before formatting messages in a specific target language
* different from the current user/system language.
* @param string $lang
*
* It saves the current translation strings in a separate variable and loads new translations strings.
* @return L10nClass The new L10n class with the new language
*
* If called repeatedly, it won't save the translation strings again, just load the new ones.
*
* @param string $lang Language code
*
* @throws \Exception
* @see popLang()
* @brief Stores the current language strings and load a different language.
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function pushLang($lang)
public static function withLang(string $lang)
{
self::getClass(L10nClass::class)->pushLang($lang);
}
/**
* Restores the original user/system language after having used pushLang()
*/
public static function popLang()
{
self::getClass(L10nClass::class)->popLang();
return self::getClass(L10nClass::class)->withLang($lang);
}
/**

View file

@ -23,12 +23,6 @@ class L10n
* @var string
*/
private $lang = '';
/**
* A language code saved for later after pushLang() has been called.
*
* @var string
*/
private $langSave = '';
/**
* An array of translation strings whose key is the neutral english message.
@ -36,12 +30,6 @@ class L10n
* @var array
*/
private $strings = [];
/**
* An array of translation strings saved for later after pushLang() has been called.
*
* @var array
*/
private $stringsSave = [];
/**
* @var Database
@ -53,7 +41,7 @@ class L10n
*/
private $logger;
public function __construct(Configuration $config, Database $dba, LoggerInterface $logger, ISession $session, array $server, array $get)
public function __construct(Configuration $config, Database $dba, LoggerInterface $logger, ISession $session, array $server, array $get)
{
$this->dba = $dba;
$this->logger = $logger;
@ -101,50 +89,6 @@ class L10n
}
}
/**
* This function should be called before formatting messages in a specific target language
* different from the current user/system language.
*
* It saves the current translation strings in a separate variable and loads new translations strings.
*
* If called repeatedly, it won't save the translation strings again, just load the new ones.
*
* @param string $lang Language code
*
* @throws \Exception
* @see popLang()
* @brief Stores the current language strings and load a different language.
*/
public function pushLang($lang)
{
if ($lang === $this->lang) {
return;
}
if (empty($this->langSave)) {
$this->langSave = $this->lang;
$this->stringsSave = $this->strings;
}
$this->loadTranslationTable($lang);
}
/**
* Restores the original user/system language after having used pushLang()
*/
public function popLang()
{
if (!isset($this->langSave)) {
return;
}
$this->strings = $this->stringsSave;
$this->lang = $this->langSave;
$this->stringsSave = null;
$this->langSave = null;
}
/**
* Loads string translation table
*
@ -458,4 +402,24 @@ class L10n
return $arr;
}
/**
* Creates a new L10n instance based on the given langauge
*
* @param string $lang The new language
*
* @return static A new L10n instance
* @throws \Exception
*/
public function withLang(string $lang)
{
// Don't create a new instance for same language
if ($lang === $this->lang) {
return $this;
}
$newL10n = clone $this;
$newL10n->loadTranslationTable($lang);
return $newL10n;
}
}

View file

@ -252,26 +252,25 @@ class Update
$sent[] = $admin['email'];
$lang = (($admin['language'])?$admin['language']:'en');
L10n::pushLang($lang);
$l10n = L10n::withLang($lang);
$preamble = Strings::deindent(L10n::t("
$preamble = Strings::deindent($l10n->t("
The friendica developers released update %s recently,
but when I tried to install it, something went terribly wrong.
This needs to be fixed soon and I can't do it alone. Please contact a
friendica developer if you can not help me on your own. My database might be invalid.",
$update_id));
$body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
$body = $l10n->t("The error message is\n[pre]%s[/pre]", $error_message);
notification([
'uid' => $admin['uid'],
'type' => SYSTEM_EMAIL,
'to_email' => $admin['email'],
'subject' => l10n::t('[Friendica Notify] Database update'),
'subject' => $l10n->t('[Friendica Notify] Database update'),
'preamble' => $preamble,
'body' => $body,
'language' => $lang]
);
L10n::popLang();
}
//try the logger
@ -295,9 +294,9 @@ class Update
$sent[] = $admin['email'];
$lang = (($admin['language']) ? $admin['language'] : 'en');
L10n::pushLang($lang);
$l10n = L10n::withLang($lang);
$preamble = Strings::deindent(L10n::t("
$preamble = Strings::deindent($l10n->t("
The friendica database was successfully updated from %s to %s.",
$from_build, $to_build));
@ -310,7 +309,6 @@ class Update
'body' => $preamble,
'language' => $lang]
);
L10n::popLang();
}
}

View file

@ -916,6 +916,7 @@ class User
*
* It's here as a function because the mail is sent from different parts
*
* @param L10n\L10n $l10n The used language
* @param array $user User record array
* @param string $sitename
* @param string $siteurl
@ -923,9 +924,9 @@ class User
* @return NULL|boolean from notification() and email() inherited
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function sendRegisterOpenEmail($user, $sitename, $siteurl, $password)
public static function sendRegisterOpenEmail(L10n\L10n $l10n, $user, $sitename, $siteurl, $password)
{
$preamble = Strings::deindent(L10n::t(
$preamble = Strings::deindent($l10n->t(
'
Dear %1$s,
Thank you for registering at %2$s. Your account has been created.
@ -933,7 +934,7 @@ class User
$user['username'],
$sitename
));
$body = Strings::deindent(L10n::t(
$body = Strings::deindent($l10n->t(
'
The login details are as follows:

View file

@ -228,6 +228,7 @@ class Register extends BaseModule
// Only send a password mail when the password wasn't manually provided
if (empty($_POST['password1']) || empty($_POST['confirm'])) {
$res = Model\User::sendRegisterOpenEmail(
L10n::withLang($arr['language']),
$user,
Config::get('config', 'sitename'),
$base_url,