From 4d6e0f03fd126a91a1aafc9e52146149a23e8ce0 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Fri, 7 Aug 2020 10:18:10 +0200 Subject: [PATCH] Make units for lengths and weights translatable --- l18n | 2 +- src/MDUnitsSet.php | 26 +++++++++++++++++++++++++- src/MDValueSet.php | 29 +++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/l18n b/l18n index 38c989b..11c3524 160000 --- a/l18n +++ b/l18n @@ -1 +1 @@ -Subproject commit 38c989b971471a0b69fe486cedf1d35587640ce8 +Subproject commit 11c3524f1f3a2df9878878df362e2ce8fcfe3f63 diff --git a/src/MDUnitsSet.php b/src/MDUnitsSet.php index 8635fee..4b2ab62 100644 --- a/src/MDUnitsSet.php +++ b/src/MDUnitsSet.php @@ -9,9 +9,33 @@ declare(strict_types = 1); /** * Class containing available units for weights, lengths etc. */ -class MDUnitsSet { +class MDUnitsSet extends MDValueSet { const UNITS_LENGTH = ['', 'm', 'dm', 'cm', 'mm']; const UNITS_WEIGHT = ['', 't', 'kg', 'g']; + /** + * Returns a translated list of length units. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + function getLengthUnitsTLed(MDTlLoader $tlLoader):array { + return parent::getTlUnsortedList($tlLoader, self::UNITS_LENGTH, "units_length_set", "units_length_set"); + + } + + /** + * Returns a translated list of weight units. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + function getWeightUnitsTLed(MDTlLoader $tlLoader):array { + return parent::getTlUnsortedList($tlLoader, self::UNITS_WEIGHT, "units_weight_set", "units_weight_set"); + + } + } diff --git a/src/MDValueSet.php b/src/MDValueSet.php index b9f0ac9..6a981fc 100644 --- a/src/MDValueSet.php +++ b/src/MDValueSet.php @@ -11,6 +11,28 @@ declare(strict_types = 1); */ class MDValueSet { + /** + * Gets an unsorted array based on provided keys and their translations. + * + * @param MDTlLoader $tlLoader Translation loader. + * @param array $keyList List of keys to get translations for. + * @param string $tlFileName Name of the translation file. + * @param string $tlVarName Variable of the translation. + * + * @return array + */ + public static function getTlUnsortedList(MDTlLoader $tlLoader, array $keyList, string $tlFileName, string $tlVarName):array { + + $output = []; + foreach ($keyList as $tID) { + if ($tID === "") $output[$tID] = ""; + else $output[$tID] = $tlLoader->tl($tlFileName, $tlVarName, $tID); + } + + return $output; + + } + /** * Gets a list of entries in a translated version. * @@ -23,12 +45,7 @@ class MDValueSet { */ public static function getTlSortedList(MDTlLoader $tlLoader, array $keyList, string $tlFileName, string $tlVarName):array { - $output = []; - foreach ($keyList as $tID) { - if ($tID === "") $output[$tID] = ""; - else $output[$tID] = $tlLoader->tl($tlFileName, $tlVarName, $tID); - } - + $output = self::getTlUnsortedList($tlLoader, $keyList, $tlFileName, $tlVarName); asort($output); return $output;