CustomFormats
6 years ago
Locales
6 years ago
FormatsInterface.php
6 years ago
LICENSE.md
6 years ago
Moment.php
6 years ago
MomentException.php
6 years ago
MomentFromVo.php
6 years ago
MomentHelper.php
6 years ago
MomentLocale.php
6 years ago
MomentPeriodVo.php
6 years ago
MomentLocale.php
228 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPressPopularPosts\Moment; |
| 4 | |
| 5 | /** |
| 6 | * MomentLocale |
| 7 | * @package Moment |
| 8 | * @author Tino Ehrich (tino@bigpun.me) |
| 9 | */ |
| 10 | class MomentLocale |
| 11 | { |
| 12 | /** |
| 13 | * @var Moment |
| 14 | */ |
| 15 | private static $moment; |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | private static $locale = 'en_GB'; |
| 21 | |
| 22 | /** |
| 23 | * @var boolean |
| 24 | */ |
| 25 | private static $findSimilar = false; |
| 26 | |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | private static $localeContent = array(); |
| 31 | |
| 32 | /** |
| 33 | * @param Moment $moment |
| 34 | */ |
| 35 | public static function setMoment(Moment $moment) |
| 36 | { |
| 37 | self::$moment = $moment; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string $locale |
| 42 | * @param bool $findSimilar |
| 43 | * |
| 44 | * @return void |
| 45 | * @throws MomentException |
| 46 | */ |
| 47 | public static function setLocale($locale, $findSimilar = false) |
| 48 | { |
| 49 | self::$locale = $locale; |
| 50 | self::$findSimilar = $findSimilar; |
| 51 | self::loadLocaleContent(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return void |
| 56 | * @throws MomentException |
| 57 | */ |
| 58 | public static function loadLocaleContent() |
| 59 | { |
| 60 | $pathFile = self::findLocaleFile(); |
| 61 | |
| 62 | if (!$pathFile) |
| 63 | { |
| 64 | throw new MomentException('Locale does not exist: ' . $pathFile); |
| 65 | } |
| 66 | |
| 67 | /** @noinspection PhpIncludeInspection */ |
| 68 | self::$localeContent = require $pathFile; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return array |
| 73 | */ |
| 74 | public static function getLocaleContent() |
| 75 | { |
| 76 | return self::$localeContent; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param array $keys |
| 81 | * |
| 82 | * @return array|string|\Closure |
| 83 | * @throws MomentException |
| 84 | */ |
| 85 | public static function getLocaleString(array $keys) |
| 86 | { |
| 87 | $string = self::$localeContent; |
| 88 | |
| 89 | foreach ($keys as $key) |
| 90 | { |
| 91 | if (isset($string[$key]) === false) |
| 92 | { |
| 93 | if ($key == 'monthsNominative' && isset($string['months'])) |
| 94 | { |
| 95 | $string = $string['months']; |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | throw new MomentException('Locale string does not exist for key: ' . join(' > ', $keys)); |
| 100 | } |
| 101 | |
| 102 | $string = $string[$key]; |
| 103 | } |
| 104 | |
| 105 | return $string; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param array $localeKeys |
| 110 | * @param array $formatArgs |
| 111 | * |
| 112 | * @return string |
| 113 | * @throws MomentException |
| 114 | */ |
| 115 | public static function renderLocaleString(array $localeKeys, array $formatArgs = array()) |
| 116 | { |
| 117 | // get locale handler |
| 118 | $localeString = self::getLocaleString($localeKeys); |
| 119 | |
| 120 | // handle callback |
| 121 | if ($localeString instanceof \Closure) |
| 122 | { |
| 123 | $localeString = call_user_func_array($localeString, $formatArgs); |
| 124 | } |
| 125 | |
| 126 | return vsprintf($localeString, $formatArgs); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param string $format |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | public static function prepareSpecialLocaleTags($format) |
| 135 | { |
| 136 | $placeholders = array( |
| 137 | // months |
| 138 | '(?<!\\\)F' => 'n__0001', |
| 139 | '(?<!\\\)M' => 'n__0002', |
| 140 | '(?<!\\\)f' => 'n__0005', |
| 141 | // weekdays |
| 142 | '(?<!\\\)l' => 'N__0003', |
| 143 | '(?<!\\\)D' => 'N__0004', |
| 144 | ); |
| 145 | |
| 146 | foreach ($placeholders as $regexp => $tag) |
| 147 | { |
| 148 | $format = preg_replace('/' . $regexp . '/u', $tag, $format); |
| 149 | } |
| 150 | |
| 151 | return $format; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param string $format |
| 156 | * |
| 157 | * @return string |
| 158 | * @throws MomentException |
| 159 | */ |
| 160 | public static function renderSpecialLocaleTags($format) |
| 161 | { |
| 162 | $placeholders = array( |
| 163 | // months |
| 164 | '\d{1,2}__0001' => 'months', |
| 165 | '\d{1,2}__0002' => 'monthsShort', |
| 166 | '\d{1,2}__0005' => 'monthsNominative', |
| 167 | // weekdays |
| 168 | '\d__0003' => 'weekdays', |
| 169 | '\d__0004' => 'weekdaysShort', |
| 170 | ); |
| 171 | |
| 172 | foreach ($placeholders as $regexp => $tag) |
| 173 | { |
| 174 | preg_match_all('/(' . $regexp . ')/', $format, $match); |
| 175 | |
| 176 | if (isset($match[1])) |
| 177 | { |
| 178 | foreach ($match[1] as $date) |
| 179 | { |
| 180 | list($localeIndex, $type) = explode('__', $date); |
| 181 | $localeString = self::renderLocaleString(array($tag, --$localeIndex)); |
| 182 | $format = preg_replace('/' . $date . '/u', $localeString, $format); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return $format; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @return null|string |
| 192 | */ |
| 193 | private static function findLocaleFile() |
| 194 | { |
| 195 | $basePathFile = __DIR__ . '/Locales/' . self::$locale; |
| 196 | $pathFile = $basePathFile . '.php'; |
| 197 | |
| 198 | if (file_exists($pathFile) === false) |
| 199 | { |
| 200 | $pathFile = null; |
| 201 | |
| 202 | if (self::$findSimilar && $similarLocales = self::fetchSimilarLocales($basePathFile)) |
| 203 | { |
| 204 | $pathFile = $similarLocales[0]; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return $pathFile; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @param string $path |
| 213 | * |
| 214 | * @return null|array |
| 215 | */ |
| 216 | private static function fetchSimilarLocales($path) |
| 217 | { |
| 218 | $locales = glob($path . '*.php'); |
| 219 | |
| 220 | if ($locales && !empty($locales)) |
| 221 | { |
| 222 | return $locales; |
| 223 | } |
| 224 | |
| 225 | return null; |
| 226 | } |
| 227 | } |
| 228 |