From 8bf7b3ee0646ab06c6a4aaeb2764af9ec0723b9b Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Sat, 29 Apr 2023 03:01:13 +0200 Subject: [PATCH] Lang/cleanup * fixed break and trim functions not handling text shorter than break length * add option to output raw text besides html, markup format * decalare return types and parameter types * cleanup --- includes/types/item.class.php | 2 +- localization/lang.class.php | 201 ++++++++++++++++++++-------------- localization/locale_dede.php | 1 + localization/locale_enus.php | 1 + localization/locale_eses.php | 1 + localization/locale_frfr.php | 1 + localization/locale_ruru.php | 1 + localization/locale_zhcn.php | 1 + pages/item.php | 2 +- pages/itemset.php | 2 +- pages/mail.php | 2 +- pages/npc.php | 2 +- pages/object.php | 2 +- pages/quest.php | 4 +- pages/spell.php | 4 +- 15 files changed, 136 insertions(+), 91 deletions(-) diff --git a/includes/types/item.class.php b/includes/types/item.class.php index c20b8bd3..e21e964d 100644 --- a/includes/types/item.class.php +++ b/includes/types/item.class.php @@ -936,7 +936,7 @@ class ItemList extends BaseType $x .= sprintf(Lang::game('requires'), ''.FactionList::getName($reqFac).' - '.Lang::game('rep', $this->curTpl['requiredFactionRank'])).'
'; // locked or openable - if ($locks = Lang::getLocks($this->curTpl['lockId'], $arr, true, true)) + if ($locks = Lang::getLocks($this->curTpl['lockId'], $arr, true)) $x .= ''.Lang::item('locked').'
'.implode('
', array_map(function($x) { return sprintf(Lang::game('requires'), $x); }, $locks)).'

'; else if ($this->curTpl['flags'] & ITEM_FLAG_OPENABLE) $x .= ''.Lang::item('openClick').'
'; diff --git a/localization/lang.class.php b/localization/lang.class.php index 6953b6c9..319953bc 100644 --- a/localization/lang.class.php +++ b/localization/lang.class.php @@ -49,7 +49,11 @@ class Lang LOCALE_RU => 'Русский' ); - public static function load($loc) + public const FMT_RAW = 0; + public const FMT_HTML = 1; + public const FMT_MARKUP = 2; + + public static function load(string $loc) : void { if (!file_exists('localization/locale_'.$loc.'.php')) die('File for localization '.strToUpper($loc).' not found.'); @@ -65,7 +69,7 @@ class Lang self::$main['moreTitles']['privilege'] = self::$privileges['_privileges']; } - public static function __callStatic($prop, $args) + public static function __callStatic(string $prop, array $args) : ?string { if (!isset(self::$$prop)) { @@ -99,7 +103,7 @@ class Lang return self::vspf($var, $vspfArgs); } - public static function concat($args, $useAnd = true, $callback = null) + public static function concat(array $args, bool $useAnd = true, ?callable $callback = null) : string { $b = ''; $i = 0; @@ -114,7 +118,7 @@ class Lang if ($n > 1 && $i < ($n - 2)) $b .= ', '; else if ($n > 1 && $i == $n - 2) - $b .= Lang::main($useAnd ? 'and' : 'or'); + $b .= self::main($useAnd ? 'and' : 'or'); $i++; } @@ -131,26 +135,24 @@ class Lang // limit whitespaces to one at a time $text = preg_replace('/\s+/', ' ', trim($text)); - if ($len > 0 && mb_strlen($text) > $len) - { - $n = 0; - $b = []; - $parts = explode(' ', $text); - while ($n < $len && $parts) - { - $_ = array_shift($parts); - $n += mb_strlen($_); - $b[] = $_; - } + if ($len <= 0 || mb_strlen($text) <= $len) + return $text; - $text = implode(' ', $b).'…'; + $n = 0; + $b = []; + $parts = explode(' ', $text); + while ($n < $len && $parts) + { + $_ = array_shift($parts); + $n += mb_strlen($_); + $b[] = $_; } - return $text; + return implode(' ', $b).'…'; } // add line breaks to string after X chars. If X is inside a word break behind it. - public static function breakTextClean(string $text, int $len = 30, bool $asHTML = true) : string + public static function breakTextClean(string $text, int $len = 30, int $fmt = self::FMT_HTML) : string { // remove line breaks $text = strtr($text, ["\n" => ' ', "\r" => ' ']); @@ -158,51 +160,58 @@ class Lang // limit whitespaces to one at a time $text = preg_replace('/\s+/', ' ', trim($text)); + if ($len <= 0 || mb_strlen($text) <= $len) + return $text; + $row = []; - if ($len > 0 && mb_strlen($text) > $len) + $i = 0; + $n = 0; + foreach (explode(' ', $text) as $p) { - $i = 0; + $row[$i][] = $p; + $n += (mb_strlen($p) + 1); + + if ($n < $len) + continue; + $n = 0; - $parts = explode(' ', $text); - foreach ($parts as $p) - { - $row[$i][] = $p; - $n += (mb_strlen($p) + 1); + $i++; + } + foreach ($row as &$r) + $r = implode(' ', $r); - if ($n < $len) - continue; - - $n = 0; - $i++; - } - foreach ($row as &$r) - $r = implode(' ', $r); + switch ($fmt) + { + case self::FMT_HTML: $separator = '
'; break; + case self::FMT_MARKUP: $separator = '[br]'; break; + case self::FMT_RAW: + default: $separator = "\n"; break; } - return implode($asHTML ? '
' : '[br]', $row); + return implode($separator, $row); } - public static function sort($prop, $group, $method = SORT_NATURAL) + public static function sort(string $prop, string $group, int $method = SORT_NATURAL) : void { if (!isset(self::$$prop)) { trigger_error('Lang::sort - tried to use undefined property Lang::$'.$prop, E_USER_WARNING); - return null; + return; } $var = &self::$$prop; if (!isset($var[$group])) { trigger_error('Lang::sort - tried to use undefined property Lang::$'.$prop.'[\''.$group.'\']', E_USER_WARNING); - return null; + return; } asort($var[$group], $method); } // todo: expand - public static function getInfoBoxForFlags($flags) + public static function getInfoBoxForFlags(int $flags) : array { $tmp = []; @@ -221,7 +230,7 @@ class Lang return $tmp; } - public static function getLocks(int $lockId, ?array &$ids = [], bool $interactive = false, bool $asHTML = false) : array + public static function getLocks(int $lockId, ?array &$ids = [], bool $interactive = false, int $fmt = self::FMT_HTML) : array { $locks = []; $ids = []; @@ -241,13 +250,16 @@ class Lang if (!$name) continue; - if ($interactive && $asHTML) - $name = ''.$name.''; - else if ($interactive && !$asHTML) + if ($fmt == self::FMT_HTML) + $name = $interactive ? ''.$name.'' : ''.$name.''; + else if ($interactive && $fmt == self::FMT_MARKUP) { $name = '[item='.$prop.']'; $ids[Type::ITEM][] = $prop; } + else + $name = $prop; + } else if ($lock['type'.$i] == LOCK_TYPE_SKILL) { @@ -263,15 +275,17 @@ class Lang 2 => SKILL_HERBALISM, 3 => SKILL_MINING, 20 => SKILL_INSCRIPTION - ); + ); - if ($interactive && $asHTML) - $name = ''.$name.''; - else if ($interactive && !$asHTML) + if ($fmt == self::FMT_HTML) + $name = $interactive ? ''.$name.'' : ''.$name.''; + else if ($interactive && $fmt == self::FMT_MARKUP) { $name = '[skill='.$skills[$prop].']'; $ids[Type::SKILL][] = $skills[$prop]; } + else + $name = $skills[$prop]; if ($rank > 0) $name .= ' ('.$rank.')'; @@ -279,13 +293,14 @@ class Lang // Lockpicking else if ($prop == 4) { - if ($interactive && $asHTML) - $name = ''.$name.''; - else if ($interactive && !$asHTML) + if ($fmt == self::FMT_HTML) + $name = $interactive ? ''.$name.'' : ''.$name.''; + else if ($interactive && $fmt == self::FMT_MARKUP) { $name = '[spell=1842]'; $ids[Type::SPELL][] = 1842; } + // else $name = $name } // exclude unusual stuff else if (User::isInGroup(U_GROUP_STAFF)) @@ -305,14 +320,12 @@ class Lang return $locks; } - public static function getReputationLevelForPoints($pts) + public static function getReputationLevelForPoints(int $pts) : string { - $_ = Game::getReputationLevelForPoints($pts); - - return self::game('rep', $_); + return self::game('rep', Game::getReputationLevelForPoints($pts)); } - public static function getRequiredItems($class, $mask, $short = true) + public static function getRequiredItems(int $class, int $mask, bool $short = true) : string { if (!in_array($class, [ITEM_CLASS_MISC, ITEM_CLASS_ARMOR, ITEM_CLASS_WEAPON])) return ''; @@ -354,7 +367,7 @@ class Lang return implode(', ', $tmp); } - public static function getStances($stanceMask) + public static function getStances(int $stanceMask) : string { $stanceMask &= 0xFF37F6FF; // clamp to available stances/forms.. @@ -374,7 +387,7 @@ class Lang return implode(', ', $tmp); } - public static function getMagicSchools($schoolMask) + public static function getMagicSchools(int $schoolMask) : string { $schoolMask &= SPELL_ALL_SCHOOLS; // clamp to available schools.. $tmp = []; @@ -393,17 +406,31 @@ class Lang return implode(', ', $tmp); } - public static function getClassString(int $classMask, array &$ids = [], bool $asHTML = true) : string + public static function getClassString(int $classMask, array &$ids = [], int $fmt = self::FMT_HTML) : string { $classMask &= CLASS_MASK_ALL; // clamp to available classes.. if ($classMask == CLASS_MASK_ALL) // available to all classes - return false; + return ''; $tmp = []; $i = 1; - $base = $asHTML ? '%2$s' : '[class=%d]'; - $br = $asHTML ? '' : '[br]'; + + switch ($fmt) + { + case self::FMT_HTML: + $base = '%2$s'; + $br = ''; + break; + case self::FMT_MARKUP: + $base = '[class=%1$d]'; + $br = '[br]'; + break; + case self::FMT_RAW: + default: + $base = '%2$s'; + $br = ''; + } while ($classMask) { @@ -420,20 +447,34 @@ class Lang return implode(', ', $tmp); } - public static function getRaceString(int $raceMask, array &$ids = [], bool $asHTML = true) : string + public static function getRaceString(int $raceMask, array &$ids = [], int $fmt = self::FMT_HTML) : string { $raceMask &= RACE_MASK_ALL; // clamp to available races.. if ($raceMask == RACE_MASK_ALL) // available to all races (we don't display 'both factions') - return false; + return ''; if (!$raceMask) - return false; + return ''; $tmp = []; $i = 1; - $base = $asHTML ? '%s' : '[race=%d]'; - $br = $asHTML ? '' : '[br]'; + + switch ($fmt) + { + case self::FMT_HTML: + $base = '%2$s'; + $br = ''; + break; + case self::FMT_MARKUP: + $base = '[race=%1$d]'; + $br = '[br]'; + break; + case self::FMT_RAW: + default: + $base = '%2$s'; + $br = ''; + } if ($raceMask == RACE_MASK_HORDE) return self::game('ra', -2); @@ -456,30 +497,28 @@ class Lang return implode(', ', $tmp); } - public static function formatSkillBreakpoints(array $bp, bool $html = false) : string + public static function formatSkillBreakpoints(array $bp, int $fmt = self::FMT_MARKUP) : string { - $tmp = Lang::game('difficulty').Lang::main('colon'); + $tmp = self::game('difficulty').self::main('colon'); + + switch ($fmt) + { + case self::FMT_HTML: $base = '%2$s '; break; + case self::FMT_MARKUP: $base = '[color=r%1$d]%2$s[/color] '; break; + case self::FMT_RAW: + default: $base = '%2$s '; break; + } for ($i = 0; $i < 4; $i++) if (!empty($bp[$i])) - $tmp .= $html ? ''.$bp[$i].' ' : '[color=r'.($i + 1).']'.$bp[$i].'[/color] '; + $tmp .= sprintf($base, $i + 1, $bp[$i]); return trim($tmp); } - public static function nf($number, $decimals = 0, $no1k = false) + public static function nf(float $number, int $decimals = 0, bool $no1k = false) : string { - // [decimal, thousand] - $seps = array( - LOCALE_EN => [',', '.'], - LOCALE_FR => [' ', ','], - LOCALE_DE => ['.', ','], - LOCALE_CN => [',', '.'], - LOCALE_ES => ['.', ','], - LOCALE_RU => [' ', ','] - ); - - return number_format($number, $decimals, $seps[User::$localeId][1], $no1k ? '' : $seps[User::$localeId][0]); + return number_format($number, $decimals, self::main('nfSeparators', 1), $no1k ? '' : self::main('nfSeparators', 0)); } public static function typeName(int $type) : string @@ -519,7 +558,7 @@ class Lang return implode(', ', $result); } - private static function vspf($var, array $args = []) + private static function vspf(/* array|string */ $var, array $args = []) // : array|string { if (is_array($var)) { diff --git a/localization/locale_dede.php b/localization/locale_dede.php index d60ed1e1..85c0fb7c 100644 --- a/localization/locale_dede.php +++ b/localization/locale_dede.php @@ -129,6 +129,7 @@ $lang = array( 'dateFmtShort' => "d.m.Y", 'dateFmtLong' => "d.m.Y \u\m H:i", 'timeAgo' => 'vor %s', + 'nfSeparators' => ['.', ','], // error 'intError' => "Ein interner Fehler ist aufgetreten.", diff --git a/localization/locale_enus.php b/localization/locale_enus.php index 441d4ab2..4b17ae92 100644 --- a/localization/locale_enus.php +++ b/localization/locale_enus.php @@ -129,6 +129,7 @@ $lang = array( 'dateFmtShort' => "Y/m/d", 'dateFmtLong' => "Y/m/d \a\\t H:i A", 'timeAgo' => "%s ago", + 'nfSeparators' => [',', '.'], // error 'intError' => "An internal error has occurred.", diff --git a/localization/locale_eses.php b/localization/locale_eses.php index 70ba0d8a..8792e450 100644 --- a/localization/locale_eses.php +++ b/localization/locale_eses.php @@ -129,6 +129,7 @@ $lang = array( 'dateFmtShort' => "d/m/Y", 'dateFmtLong' => "d/m/Y \a \l\a\s H:i A", 'timeAgo' => 'hace %s', + 'nfSeparators' => ['.', ','], // error 'intError' => "Un error interno ha ocurrido.", diff --git a/localization/locale_frfr.php b/localization/locale_frfr.php index 753705d5..e5db3aa1 100644 --- a/localization/locale_frfr.php +++ b/localization/locale_frfr.php @@ -129,6 +129,7 @@ $lang = array( 'dateFmtShort' => "Y-m-d", 'dateFmtLong' => "Y-m-d à H:i A", 'timeAgo' => 'il y a %s', + 'nfSeparators' => [' ', ','], // error 'intError' => "[An internal error occured.]", diff --git a/localization/locale_ruru.php b/localization/locale_ruru.php index 65aece69..981d78e2 100644 --- a/localization/locale_ruru.php +++ b/localization/locale_ruru.php @@ -129,6 +129,7 @@ $lang = array( 'dateFmtShort' => "Y-m-d", 'dateFmtLong' => "Y-m-d в H:i A", 'timeAgo' => '%s назад', + 'nfSeparators' => [' ', ','], // error 'intError' => "[An internal error occured.]", diff --git a/localization/locale_zhcn.php b/localization/locale_zhcn.php index 575ab8bb..22f0acb8 100644 --- a/localization/locale_zhcn.php +++ b/localization/locale_zhcn.php @@ -129,6 +129,7 @@ $lang = array( 'dateFmtShort' => "Y/m/d", 'dateFmtLong' => "Y/m/d \a\\t H:i A", 'timeAgo' => '%s之前', + 'nfSeparators' => [',', '.'], // error 'intError' => "发生内部错误。", diff --git a/pages/item.php b/pages/item.php index 48c513e3..fd397e9d 100644 --- a/pages/item.php +++ b/pages/item.php @@ -262,7 +262,7 @@ class ItemPage extends genericPage if ($_reqRating) { $text = str_replace('
', ' ', Lang::item('reqRating', $_reqRating[1], [$_reqRating[0]])); - $infobox[] = Lang::breakTextClean($text, 30, false); + $infobox[] = Lang::breakTextClean($text); } } diff --git a/pages/itemset.php b/pages/itemset.php index 70ad0a55..bec8984b 100644 --- a/pages/itemset.php +++ b/pages/itemset.php @@ -96,7 +96,7 @@ class ItemsetPage extends GenericPage // class $jsg = []; - if ($cl = Lang::getClassString($this->subject->getField('classMask'), $jsg, false)) + if ($cl = Lang::getClassString($this->subject->getField('classMask'), $jsg, Lang::FMT_MARKUP)) { $this->extendGlobalIds(Type::CHR_CLASS, ...$jsg); $t = count($jsg)== 1 ? Lang::game('class') : Lang::game('classes'); diff --git a/pages/mail.php b/pages/mail.php index 0a7d2e95..d821dbf9 100644 --- a/pages/mail.php +++ b/pages/mail.php @@ -56,7 +56,7 @@ class MailPage extends GenericPage $infobox[] = Lang::game('level').Lang::main('colon').$mlr['level']; $rIds = []; - if ($r = Lang::getRaceString($mlr['raceMask'], $rIds, false)) + if ($r = Lang::getRaceString($mlr['raceMask'], $rIds, Lang::FMT_MARKUP)) { $infobox[] = Lang::game('races').Lang::main('colon').$r; $this->extendGlobalIds(Type::CHR_RACE, ...$rIds); diff --git a/pages/npc.php b/pages/npc.php index 19095895..0599cd83 100644 --- a/pages/npc.php +++ b/pages/npc.php @@ -740,7 +740,7 @@ class NpcPage extends GenericPage if (!empty($sf['note'])) $tabData['note'] = $sf['note']; else if ($sf[0] == LOOT_SKINNING) - $tabData['note'] = ''.Lang::formatSkillBreakpoints(Game::getBreakpointsForSkill($skinTab[2], $this->subject->getField('maxLevel') * 5), true).''; + $tabData['note'] = ''.Lang::formatSkillBreakpoints(Game::getBreakpointsForSkill($skinTab[2], $this->subject->getField('maxLevel') * 5), Lang::FMT_HTML).''; if ($sf[4]) $tabData['hiddenCols'] = $sf[4]; diff --git a/pages/object.php b/pages/object.php index 0da9d7d9..cb66a47c 100644 --- a/pages/object.php +++ b/pages/object.php @@ -103,7 +103,7 @@ class ObjectPage extends GenericPage break; default: // requires key .. maybe { - $locks = Lang::getLocks($this->subject->getField('lockId'), $ids, true); + $locks = Lang::getLocks($this->subject->getField('lockId'), $ids, true, Lang::FMT_MARKUP); $l = []; foreach ($ids as $type => $typeIds) diff --git a/pages/quest.php b/pages/quest.php index 038b4f8f..82c48be2 100644 --- a/pages/quest.php +++ b/pages/quest.php @@ -161,7 +161,7 @@ class QuestPage extends GenericPage $jsg = []; // races - if ($_ = Lang::getRaceString($this->subject->getField('reqRaceMask'), $jsg, false)) + if ($_ = Lang::getRaceString($this->subject->getField('reqRaceMask'), $jsg, Lang::FMT_MARKUP)) { $this->extendGlobalIds(Type::CHR_RACE, ...$jsg); $t = count($jsg) == 1 ? Lang::game('race') : Lang::game('races'); @@ -169,7 +169,7 @@ class QuestPage extends GenericPage } // classes - if ($_ = Lang::getClassString($this->subject->getField('reqClassMask'), $jsg, false)) + if ($_ = Lang::getClassString($this->subject->getField('reqClassMask'), $jsg, Lang::FMT_MARKUP)) { $this->extendGlobalIds(Type::CHR_CLASS, ...$jsg); $t = count($jsg) == 1 ? Lang::game('class') : Lang::game('classes'); diff --git a/pages/spell.php b/pages/spell.php index 8ad93a93..3724e5e8 100644 --- a/pages/spell.php +++ b/pages/spell.php @@ -163,7 +163,7 @@ class SpellPage extends GenericPage $jsg = []; // races - if ($_ = Lang::getRaceString($this->subject->getField('reqRaceMask'), $jsg, false)) + if ($_ = Lang::getRaceString($this->subject->getField('reqRaceMask'), $jsg, Lang::FMT_MARKUP)) { $this->extendGlobalIds(Type::CHR_RACE, ...$jsg); $t = count($jsg) == 1 ? Lang::game('race') : Lang::game('races'); @@ -171,7 +171,7 @@ class SpellPage extends GenericPage } // classes - if ($_ = Lang::getClassString($this->subject->getField('reqClassMask'), $jsg, false)) + if ($_ = Lang::getClassString($this->subject->getField('reqClassMask'), $jsg, Lang::FMT_MARKUP)) { $this->extendGlobalIds(Type::CHR_CLASS, ...$jsg); $t = count($jsg) == 1 ? Lang::game('class') : Lang::game('classes');