PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.5.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.5.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Language / Language.php
wp-staging / Framework / Language Last commit date
Language.php 5 months ago
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