ArrayUtil.php
1 year ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
3 years ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
NumberUtil.php
2 years ago
OrderUtil.php
1 year ago
PluginUtil.php
1 year ago
RestApiUtil.php
2 years ago
StringUtil.php
2 years ago
TimeUtil.php
2 years ago
I18nUtil.php
67 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A class of utilities for dealing with internationalization. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Utilities; |
| 7 | |
| 8 | /** |
| 9 | * A class of utilities for dealing with internationalization. |
| 10 | */ |
| 11 | final class I18nUtil { |
| 12 | /** |
| 13 | * A cache for the i18n units data. |
| 14 | * |
| 15 | * @var array $units |
| 16 | */ |
| 17 | private static $units; |
| 18 | |
| 19 | /** |
| 20 | * Get the translated label for a weight unit of measure. |
| 21 | * |
| 22 | * This will return the original input string if it isn't found in the units array. This way a custom unit of |
| 23 | * measure can be used even if it's not getting translated. |
| 24 | * |
| 25 | * @param string $weight_unit The abbreviated weight unit in English, e.g. kg. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public static function get_weight_unit_label( $weight_unit ) { |
| 30 | if ( empty( self::$units ) ) { |
| 31 | self::$units = include WC()->plugin_path() . '/i18n/units.php'; |
| 32 | } |
| 33 | |
| 34 | $label = $weight_unit; |
| 35 | |
| 36 | if ( ! empty( self::$units['weight'][ $weight_unit ] ) ) { |
| 37 | $label = self::$units['weight'][ $weight_unit ]; |
| 38 | } |
| 39 | |
| 40 | return $label; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the translated label for a dimensions unit of measure. |
| 45 | * |
| 46 | * This will return the original input string if it isn't found in the units array. This way a custom unit of |
| 47 | * measure can be used even if it's not getting translated. |
| 48 | * |
| 49 | * @param string $dimensions_unit The abbreviated dimension unit in English, e.g. cm. |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function get_dimensions_unit_label( $dimensions_unit ) { |
| 54 | if ( empty( self::$units ) ) { |
| 55 | self::$units = include WC()->plugin_path() . '/i18n/units.php'; |
| 56 | } |
| 57 | |
| 58 | $label = $dimensions_unit; |
| 59 | |
| 60 | if ( ! empty( self::$units['dimensions'][ $dimensions_unit ] ) ) { |
| 61 | $label = self::$units['dimensions'][ $dimensions_unit ]; |
| 62 | } |
| 63 | |
| 64 | return $label; |
| 65 | } |
| 66 | } |
| 67 |