summaryrefslogtreecommitdiffstats
path: root/inc/dictionary.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2023-11-14 14:47:55 +0100
committerSimon Rettberg2023-11-14 14:47:55 +0100
commit06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0 (patch)
tree7e5493b102074672d8cfd8fe1a61e49f080edbe8 /inc/dictionary.inc.php
parentUpdate phpstorm config (diff)
downloadslx-admin-06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0.tar.gz
slx-admin-06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0.tar.xz
slx-admin-06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0.zip
Add function param/return types, fix a lot more phpstorm complaints
Diffstat (limited to 'inc/dictionary.inc.php')
-rw-r--r--inc/dictionary.inc.php72
1 files changed, 33 insertions, 39 deletions
diff --git a/inc/dictionary.inc.php b/inc/dictionary.inc.php
index 4c98c63b..ee613d0b 100644
--- a/inc/dictionary.inc.php
+++ b/inc/dictionary.inc.php
@@ -6,16 +6,15 @@ class Dictionary
/**
* @var string[] Array of languages, numeric index, two letter CC as values
*/
- private static $languages = false;
+ private static $languages = [];
/**
- * @var array Array of languages, numeric index, values are ['name' => 'Language Name', 'cc' => 'xx']
+ * @var array{'name': string, 'cc': string}|null Long name of language, and CC
*/
- private static $languagesLong = false;
- private static $stringCache = array();
+ private static $languagesLong = null;
+ private static $stringCache = [];
- public static function init()
+ public static function init(): void
{
- self::$languages = array();
foreach (glob('lang/??', GLOB_ONLYDIR) as $lang) {
if (!file_exists($lang . '/name.txt') && !file_exists($lang . '/flag.png'))
continue;
@@ -93,25 +92,25 @@ class Dictionary
*
* @param string $module Module name
* @param string $file Dictionary name
- * @param string|false $lang Language CC, false === current language
+ * @param ?string $lang Language CC, false === current language
* @return array assoc array mapping language tags to the translated strings
*/
- public static function getArray($module, $file, $lang = false)
+ public static function getArray(string $module, string $file, ?string $lang = null): array
{
- if ($lang === false)
+ if ($lang === null)
$lang = LANG;
$path = Util::safePath("modules/{$module}/lang/{$lang}/{$file}.json");
if (isset(self::$stringCache[$path]))
return self::$stringCache[$path];
if (!file_exists($path))
- return array();
+ return [];
$content = file_get_contents($path);
if ($content === false) { // File does not exist for language
$content = '[]';
}
$json = json_decode($content, true);
if (!is_array($json)) {
- $json = array();
+ $json = [];
}
return self::$stringCache[$path] = $json;
}
@@ -126,7 +125,7 @@ class Dictionary
* @param bool $returnTagOnMissing If true, the tag name enclosed in {{}} will be returned if the tag does not exist
* @return string|false The requested tag's translation, or false if not found and $returnTagOnMissing === false
*/
- public static function translateFileModule($moduleId, $file, $tag, $returnTagOnMissing = false)
+ public static function translateFileModule(string $moduleId, string $file, string $tag, bool $returnTagOnMissing = true)
{
$strings = self::getArray($moduleId, $file);
if (!isset($strings[$tag])) {
@@ -146,7 +145,7 @@ class Dictionary
* @param bool $returnTagOnMissing If true, the tag name enclosed in {{}} will be returned if the tag does not exist
* @return string|false The requested tag's translation, or false if not found and $returnTagOnMissing === false
*/
- public static function translateFile($file, $tag, $returnTagOnMissing = false)
+ public static function translateFile(string $file, string $tag, bool $returnTagOnMissing = true)
{
if (!class_exists('Page') || Page::getModule() === false)
return false; // We have no page - return false for now, as we're most likely running in api or install mode
@@ -160,9 +159,9 @@ class Dictionary
* @param bool $returnTagOnMissing If true, the tag name enclosed in {{}} will be returned if the tag does not exist
* @return string|false The requested tag's translation, or false if not found and $returnTagOnMissing === false
*/
- public static function translate($tag, $returnTagOnMissing = false)
+ public static function translate(string $tag, bool $returnTagOnMissing = true)
{
- $string = self::translateFile('module', $tag);
+ $string = self::translateFile('module', $tag, false);
if ($string !== false)
return $string;
$string = self::translateFileModule('main', 'global-tags', $tag);
@@ -176,9 +175,8 @@ class Dictionary
*
* @param string $module Module the message belongs to
* @param string $id Message id
- * @return string|false
*/
- public static function getMessage($module, $id)
+ public static function getMessage(string $module, string $id): string
{
$string = self::translateFileModule($module, 'messages', $id);
if ($string === false) {
@@ -190,19 +188,18 @@ class Dictionary
/**
* Get translation of the given category.
*
- * @param string $category
+ * @param string $category Menu category to get localized name for
* @return string Category name, or some generic fallback to the given category id
*/
- public static function getCategoryName($category)
+ public static function getCategoryName(string $category): string
{
- if ($category === false) {
- return 'No Category';
- }
- if (!preg_match('/^(\w+)\.(.*)$/', $category, $out)) {
- return 'Invalid Category ID format: ' . $category;
+ if (!empty($category)) {
+ if (!preg_match('/^(\w+)\.(.*)$/', $category, $out)) {
+ return 'Invalid Category ID format: ' . $category;
+ }
+ $string = self::translateFileModule($out[1], 'categories', $out[2]);
}
- $string = self::translateFileModule($out[1], 'categories', $out[2]);
- if ($string === false) {
+ if (empty($category) || $string === false) {
return "!!{$category}!!";
}
return $string;
@@ -215,12 +212,12 @@ class Dictionary
* false = regular array containing only the ccs
* @return array List of languages
*/
- public static function getLanguages($withName = false)
+ public static function getLanguages(bool $withName = false): ?array
{
if (!$withName)
return self::$languages;
- if (self::$languagesLong === false) {
- self::$languagesLong = array();
+ if (self::$languagesLong === null) {
+ self::$languagesLong = [];
foreach (self::$languages as $lang) {
if (file_exists("lang/$lang/name.txt")) {
$name = file_get_contents("lang/$lang/name.txt");
@@ -230,10 +227,10 @@ class Dictionary
if (!isset($name) || $name === false) {
$name = $lang;
}
- self::$languagesLong[] = array(
+ self::$languagesLong[] = [
'cc' => $lang,
- 'name' => $name
- );
+ 'name' => $name,
+ ];
}
}
return self::$languagesLong;
@@ -242,11 +239,8 @@ class Dictionary
/**
* Get name of language matching given language CC.
* Default to the CC if the language isn't known.
- *
- * @param string $langCC
- * @return string
*/
- public static function getLanguageName($langCC)
+ public static function getLanguageName(string $langCC): string
{
if (file_exists("lang/$langCC/name.txt")) {
$name = file_get_contents("lang/$langCC/name.txt");
@@ -264,12 +258,12 @@ class Dictionary
* to the image, otherwise, it is just added as the title attribute.
*
* @param $caption bool with caption next to <img>
- * @param $langCC string Language cc to get flag code for - defaults to current language
+ * @param $langCC ?string Language cc to get flag code for - defaults to current language
* @return string html code of img tag for language
*/
- public static function getFlagHtml($caption = false, $langCC = false)
+ public static function getFlagHtml(bool $caption = false, string $langCC = null): string
{
- if ($langCC === false) {
+ if ($langCC === null) {
$langCC = LANG;
}
$flag = "lang/$langCC/flag.png";