Language.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Language; |
| 4 | |
| 5 | use WPStaging\Framework\Facades\Hooks; |
| 6 | |
| 7 | class Language |
| 8 | { |
| 9 | /** @var string */ |
| 10 | const HOOK_LOAD_MO_FILES = 'wpstg.language.load_mo_files'; |
| 11 | |
| 12 | /** @var string */ |
| 13 | const TEXT_DOMAIN = 'wp-staging'; |
| 14 | |
| 15 | const FILTER_PLUGIN_LOCALE = 'plugin_locale'; |
| 16 | |
| 17 | /** |
| 18 | * @return void |
| 19 | */ |
| 20 | public function load() |
| 21 | { |
| 22 | /** @noinspection NullPointerExceptionInspection */ |
| 23 | $pluginLangDirectory = WPSTG_PLUGIN_DIR . 'languages/'; |
| 24 | $wpLangDirectory = $this->getLangDirectory(); |
| 25 | |
| 26 | if (function_exists('get_user_locale')) { |
| 27 | $locale = get_user_locale(); |
| 28 | } else { |
| 29 | $locale = get_locale(); |
| 30 | } |
| 31 | |
| 32 | // Traditional WP plugin locale filter |
| 33 | $locale = apply_filters(self::FILTER_PLUGIN_LOCALE, $locale, self::TEXT_DOMAIN); |
| 34 | $localMoFile = $this->getLocalMoFile($locale); |
| 35 | $globalMoFile = $this->getGlobalMoFile($locale); |
| 36 | // Unfiltered mo file name |
| 37 | $actualMoFile = sprintf('%1$s-%2$s.mo', self::TEXT_DOMAIN, $locale); |
| 38 | |
| 39 | // Setup paths to current locale file |
| 40 | $moFileLocal = $pluginLangDirectory . $localMoFile; |
| 41 | $moFilesGlobal = []; |
| 42 | if ($globalMoFile !== $actualMoFile) { |
| 43 | $moFilesGlobal[] = sprintf('%s/%s/%s', $wpLangDirectory, 'plugins', $actualMoFile); |
| 44 | } |
| 45 | |
| 46 | $moFilesGlobal[] = sprintf('%s/%s/%s', $wpLangDirectory, 'plugins', $globalMoFile); |
| 47 | |
| 48 | // Internal Use Only. Use for loading languages files |
| 49 | Hooks::callInternalHook(self::HOOK_LOAD_MO_FILES, [$locale, $moFileLocal, $moFilesGlobal]); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the language code of the current locale, e.g. de, en, it, etc. |
| 54 | * @return string |
| 55 | */ |
| 56 | public function getLocaleLanguageCode(): string |
| 57 | { |
| 58 | if (function_exists('get_user_locale')) { |
| 59 | $locale = get_user_locale(); |
| 60 | } else { |
| 61 | $locale = get_locale(); |
| 62 | } |
| 63 | return substr($locale, 0, 2); |
| 64 | } |
| 65 | |
| 66 | protected function getLocalMoFile(string $locale): string |
| 67 | { |
| 68 | // Let us assume that the locale is `de` for all `de_` dilects |
| 69 | if (strpos($locale, 'de_') === 0) { |
| 70 | $locale = 'de'; |
| 71 | } |
| 72 | |
| 73 | return sprintf('%1$s-%2$s.mo', self::TEXT_DOMAIN, $locale); |
| 74 | } |
| 75 | |
| 76 | protected function getGlobalMoFile(string $locale): string |
| 77 | { |
| 78 | // Let us assume that the locale is `de` for all `de_` dilects |
| 79 | if (strpos($locale, 'de_') === 0) { |
| 80 | $locale = 'de_DE'; |
| 81 | } |
| 82 | |
| 83 | return sprintf('%1$s-%2$s.mo', self::TEXT_DOMAIN, $locale); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return string |
| 88 | */ |
| 89 | protected function getLangDirectory(): string |
| 90 | { |
| 91 | return WP_LANG_DIR; |
| 92 | } |
| 93 | } |
| 94 |