1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 05:02:40 +00:00

Fix language-loading loop by applying incrementally

This commit is contained in:
J. King 2024-07-06 12:24:16 -04:00
parent 0170ec19c7
commit ade1c79361

View file

@ -161,12 +161,12 @@ class Lang {
if (!$this->requirementsMet) { if (!$this->requirementsMet) {
$this->checkRequirements(); $this->checkRequirements();
} }
$this->synched = true;
$this->formatter = null;
// if we've requested no locale (""), just load the fallback strings and return // if we've requested no locale (""), just load the fallback strings and return
if ($this->wanted === "") { if ($this->wanted === "") {
$this->strings = self::REQUIRED; $this->strings = self::REQUIRED;
$this->locale = $this->wanted; $this->locale = $this->wanted;
$this->synched = true;
$this->formatter = null;
return true; return true;
} }
// decompose the requested locale from specific to general, building a list of files to load // decompose the requested locale from specific to general, building a list of files to load
@ -191,24 +191,18 @@ class Lang {
$files[] = $file; $files[] = $file;
} }
// if we need to load all files, start with the fallback strings // if we need to load all files, start with the fallback strings
$strings = [];
if ($files === $loaded) { if ($files === $loaded) {
$strings[] = self::REQUIRED; $this->strings = self::REQUIRED;
} else { $this->locale = "";
// otherwise start with the strings we already have if we're going from e.g. "fr" to "fr_ca"
$strings[] = $this->strings;
} }
$loaded = array_diff($loaded, $files); $this->loaded = array_diff($loaded, $files);
$toThrow = null;
while ($files) { while ($files) {
// read files in reverse order // read files in reverse order, from most general to most specific
$file = array_pop($files); $file = array_pop($files);
if (!file_exists($this->path."$file.php")) { if (!file_exists($this->path."$file.php")) {
$toThrow = ["fileMissing", $file]; throw new Lang\Exception("fileMissing", $file);
break;
} elseif (!is_readable($this->path."$file.php")) { } elseif (!is_readable($this->path."$file.php")) {
$toThrow = ["fileUnreadable", $file]; throw new Lang\Exception("fileUnreadable", $file);
break;
} }
try { try {
// we use output buffering in case the language file is corrupted // we use output buffering in case the language file is corrupted
@ -220,21 +214,11 @@ class Lang {
ob_end_clean(); ob_end_clean();
} }
if (!is_array($arr)) { if (!is_array($arr)) {
$toThrow = ["fileCorrupt", $file]; throw new Lang\Exception("fileCorrupt", $file);
break;
} }
$strings[] = $arr; $this->strings = array_replace_recursive($this->strings, $arr);
$loaded[] = $file; $this->loaded[] = $file;
} $this->locale = $file;
// apply the results and return
$this->strings = array_replace_recursive(...$strings);
$this->loaded = $loaded;
$this->locale = $this->wanted;
$this->synched = true;
$this->formatter = null;
if ($toThrow) {
// if not all requested files could be loaded successfully, throw an exception
throw new Lang\Exception(...$toThrow);
} }
return true; return true;
} }