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