PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.2.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.2.1
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 1 year ago
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