| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils; |
| 4 |
|
| 5 |
/** @define "STOREENGINE_ROOT_DIR_PATH" "./../../" */ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* A class of utilities for dealing with internationalization. |
| 13 |
*/ |
| 14 |
final class I18n { |
| 15 |
/** |
| 16 |
* A cache for the i18n units data. |
| 17 |
* |
| 18 |
* @var ?array $units |
| 19 |
*/ |
| 20 |
private static ?array $units = null; |
| 21 |
|
| 22 |
public static function get_units() { |
| 23 |
if ( null === self::$units ) { |
| 24 |
self::$units = apply_filters( 'storeengine/units', include STOREENGINE_ROOT_DIR_PATH . 'i18n/units.php' ); |
| 25 |
} |
| 26 |
|
| 27 |
return self::$units; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the translated label for a weight unit of measure. |
| 32 |
* |
| 33 |
* This will return the original input string if it isn't found in the units array. This way a custom unit of |
| 34 |
* measure can be used even if it's not getting translated. |
| 35 |
* |
| 36 |
* @param string $weight_unit The abbreviated weight unit in English, e.g. kg. |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public static function get_weight_unit_label( string $weight_unit ): string { |
| 41 |
return self::get_units()['weight'][ $weight_unit ] ?? $weight_unit; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the translated label for a dimensions unit of measure. |
| 46 |
* |
| 47 |
* This will return the original input string if it isn't found in the units array. This way a custom unit of |
| 48 |
* measure can be used even if it's not getting translated. |
| 49 |
* |
| 50 |
* @param string $dimensions_unit The abbreviated dimension unit in English, e.g. cm. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public static function get_dimensions_unit_label( string $dimensions_unit ): string { |
| 55 |
return self::get_units()['dimensions'][ $dimensions_unit ] ?? $dimensions_unit; |
| 56 |
} |
| 57 |
} |
| 58 |
|