diff options
Diffstat (limited to 'src/web/helpers/lang.php')
-rw-r--r-- | src/web/helpers/lang.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/web/helpers/lang.php b/src/web/helpers/lang.php new file mode 100644 index 0000000..e8fa29e --- /dev/null +++ b/src/web/helpers/lang.php @@ -0,0 +1,79 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +$lang = array(); + +function lang($key, $default = NULL, $sub = NULL) { + $lang = $GLOBALS['lang']; + if(array_key_exists($key, $lang)) { + if ($sub) { + return sprintf($lang[$key], ...$sub); + } else { + return $lang[$key]; + } + } else if ($default !== NULL) { + return $default; + } else { + trigger_error('Undefined lang string: ' . $key, E_USER_WARNING); + return $key; + } +} + +function ilang($key, + $class = NULL, + $id = NULL, + $href = NULL, + $click = NULL, + $attrs = array(), + $sub = NULL, + $button = FALSE, + $container = 'span' +) { + $text = ucfirst(lang($key . "_text", FALSE, sub: $sub)); + $tip = lang($key . "_tip", FALSE, sub: $sub); + $icon = lang($key . "_icon", FALSE); + $content = lang($key . "_content", FALSE); + + if ($click || $button) { + echo '<button '; + } else { + echo '<a '; + } + if ($tip) { + echo 'title="' . $tip . '" '; + echo 'aria-label="' . $tip . '" '; + } + if ($class) { + echo 'class="' . $class . '" '; + } + if ($id) { + echo 'id="' . $id . '" '; + } + if ($click) { + echo 'onclick="' . $click . '" '; + } + if ($href) { + echo 'href="' . $href . '" '; + } + foreach ($attrs as $key => $attr) { + echo $key . '="' . $attr . '" '; + } + echo '> '; + if ($icon) { + echo '<i class="' . $icon . '">'; + if ($content) { + echo $content; + } + echo '</i>'; + } + if ($text) { + echo '<' . $container; + if ($icon) { + echo ' class="ml-sm"'; + } + echo '>' . $text . '</' . $container . '>'; + } + if ($click || $button) { + echo '</button>'; + } else { + echo '</a>'; + } +} |