Skip to content

Commit f5a7aa2

Browse files
committed
Refactored the loadL10N method.
1 parent dc7ab3e commit f5a7aa2

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,7 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co
151151
### v3.5.1
152152

153153
[2024.03.21; Bug-fix; jedso]: Changed `$this->IPAddr` to `$this->Loader->IPAddr` in `Scanner.php`.
154+
155+
### v3.5.2
156+
157+
[2024.07.02; Maikuolan]: Refactored the `loadL10N` method.

src/Loader.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The loader (last modified: 2024.03.21).
11+
* This file: The loader (last modified: 2024.07.01).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -563,24 +563,20 @@ public function fallback(array $Fallbacks): void
563563
*/
564564
public function loadL10N(string $Path = ''): void
565565
{
566-
if ($this->Configuration['core']['lang'] === 'en') {
567-
$Primary = $this->readFile($Path . 'en.yml');
568-
$Fallback = '';
569-
} else {
570-
if (($Primary = $this->readFile($Path . $this->Configuration['core']['lang'] . '.yml')) === '') {
571-
if (isset($this->ConfigurationDefaults['core']['lang']['defer'][$this->Configuration['core']['lang']])) {
572-
$Primary = $this->readFile($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$this->Configuration['core']['lang']] . '.yml');
566+
if (($Primary = $this->readFile($Path . $this->Configuration['core']['lang'] . '.yml')) === '') {
567+
if (isset($this->ConfigurationDefaults['core']['lang']['defer'][$this->Configuration['core']['lang']])) {
568+
if (($Primary = $this->readFile($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$this->Configuration['core']['lang']] . '.yml')) === '') {
569+
$Primary = $this->readFile($Path . preg_replace('~-.*$~', '', $this->ConfigurationDefaults['core']['lang']['defer'][$this->Configuration['core']['lang']]) . '.yml');
573570
}
574-
if ($Primary === '') {
575-
$Try = preg_replace('~-.*$~', '', $this->Configuration['core']['lang']);
576-
if (($Primary = $this->readFile($Path . $Try . '.yml')) === '') {
577-
if (isset($this->ConfigurationDefaults['core']['lang']['defer'][$Try])) {
578-
$Primary = $this->readFile($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$Try] . '.yml');
579-
}
571+
}
572+
if ($Primary === '') {
573+
$Try = preg_replace('~-.*$~', '', $this->Configuration['core']['lang']);
574+
if (($Primary = $this->readFile($Path . $Try . '.yml')) === '') {
575+
if (isset($this->ConfigurationDefaults['core']['lang']['defer'][$Try])) {
576+
$Primary = $this->readFile($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$Try] . '.yml');
580577
}
581578
}
582579
}
583-
$Fallback = $this->readFile($Path . 'en.yml');
584580
}
585581
if ($Primary !== '') {
586582
$Accepted = $this->ConfigurationDefaults['core']['lang']['assume'][$this->Configuration['core']['lang']] ?? $this->Configuration['core']['lang'];
@@ -594,6 +590,7 @@ public function loadL10N(string $Path = ''): void
594590
if ($this->L10NAccepted === '' && $Accepted !== '') {
595591
$this->L10NAccepted = $Accepted;
596592
}
593+
$Fallback = substr($this->L10NAccepted, 0, 3) === 'en-' ? '' : $this->readFile($Path . 'en.yml');
597594
if ($Fallback !== '') {
598595
$Arr = [];
599596
$this->YAML->process($Fallback, $Arr);
@@ -612,12 +609,12 @@ public function loadL10N(string $Path = ''): void
612609
}
613610
} else {
614611
$this->L10N = new \Maikuolan\Common\L10N($Primary, $Fallback);
615-
if ($this->Configuration['core']['lang'] === 'en') {
616-
$this->L10N->autoAssignRules('en-AU');
612+
if (substr($this->L10NAccepted, 0, 3) === 'en-') {
613+
$this->L10N->autoAssignRules($this->L10NAccepted);
617614
} else {
618615
$this->L10N->autoAssignRules($this->L10NAccepted, 'en-AU');
619616
}
620-
$this->L10N->PreferredVariant = $this->L10NAccepted;
617+
$this->L10N->PreferredVariant = $this->ConfigurationDefaults['core']['lang']['defer'][$this->L10NAccepted] ?? $this->L10NAccepted;
621618
}
622619

623620
/** Load client-specified L10N data if possible. */
@@ -637,17 +634,18 @@ public function loadL10N(string $Path = ''): void
637634
$IsSameAs = true;
638635
break 2;
639636
}
640-
if (is_readable($Path . $Accepted . '.yml')) {
641-
$Primary = $this->readFile($Path . $Accepted . '.yml');
637+
if (($Primary = $this->readFile($Path . $Accepted . '.yml')) !== '') {
642638
break 2;
643639
}
644640
if (isset($this->ConfigurationDefaults['core']['lang']['defer'][$Accepted])) {
645641
if ($this->L10NAccepted === $this->ConfigurationDefaults['core']['lang']['defer'][$Accepted]) {
646642
$IsSameAs = true;
647643
break 2;
648644
}
649-
if (is_readable($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$Accepted] . '.yml')) {
650-
$Primary = $this->readFile($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$Accepted] . '.yml');
645+
if (
646+
($Primary = $this->readFile($Path . $this->ConfigurationDefaults['core']['lang']['defer'][$Accepted] . '.yml')) !== '' ||
647+
($Primary = $this->readFile($Path . preg_replace('~-.*$~', '', $this->ConfigurationDefaults['core']['lang']['defer'][$Accepted]) . '.yml')) !== ''
648+
) {
651649
break 2;
652650
}
653651
}
@@ -661,6 +659,7 @@ public function loadL10N(string $Path = ''): void
661659
if ($IsSameAs) {
662660
if (!($this->ClientL10N instanceof \Maikuolan\Common\L10N)) {
663661
$this->ClientL10N = &$this->L10N;
662+
$this->ClientL10NAccepted = &$this->L10NAccepted;
664663
}
665664
} elseif ($Primary !== '') {
666665
$Arr = [];
@@ -673,6 +672,7 @@ public function loadL10N(string $Path = ''): void
673672
} else {
674673
$this->ClientL10N = new \Maikuolan\Common\L10N($Arr, $this->L10N);
675674
$this->ClientL10N->autoAssignRules($Accepted);
675+
$this->ClientL10N->PreferredVariant = $this->ConfigurationDefaults['core']['lang']['defer'][$Accepted] ?? $Accepted;
676676
}
677677
} elseif (!($this->ClientL10N instanceof \Maikuolan\Common\L10N)) {
678678
$this->ClientL10N = new \Maikuolan\Common\L10N([], $this->L10N);
@@ -683,7 +683,7 @@ public function loadL10N(string $Path = ''): void
683683
/** Fallback for missing accepted client L10N choice. */
684684
if ($this->ClientL10NAccepted === '') {
685685
$this->ClientL10NAccepted = $this->L10NAccepted;
686-
$this->ClientL10N->PreferredVariant = $this->L10NAccepted;
686+
$this->ClientL10N->PreferredVariant = $this->ConfigurationDefaults['core']['lang']['defer'][$this->L10NAccepted] ?? $this->L10NAccepted;
687687
}
688688
}
689689

0 commit comments

Comments
 (0)