| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* @since 3.0.0 |
| 7 |
*/ |
| 8 |
class Language |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @since 4.13.0 Added early return if the textdomain is loaded |
| 12 |
* @since 3.0.0 |
| 13 |
*/ |
| 14 |
public static function load() |
| 15 |
{ |
| 16 |
if (is_textdomain_loaded('give')) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$giveRelativePath = self::getRelativePath(); |
| 21 |
|
| 22 |
$locale = is_admin() && !wp_doing_ajax() && function_exists('get_user_locale') ? get_user_locale() : get_locale(); |
| 23 |
$locale = apply_filters('plugin_locale', $locale, 'give'); // Traditional WordPress plugin locale filter. |
| 24 |
|
| 25 |
// Setup paths to current locale file. |
| 26 |
$moFile = sprintf('%1$s-%2$s.mo', 'give', $locale); |
| 27 |
$moFileLocal = trailingslashit(WP_PLUGIN_DIR) . $giveRelativePath . $moFile; |
| 28 |
$moFileGlobal = trailingslashit(WP_LANG_DIR) . 'plugins/' . $moFile; |
| 29 |
|
| 30 |
unload_textdomain('give'); |
| 31 |
if (file_exists($moFileGlobal)) { |
| 32 |
load_textdomain('give', $moFileGlobal); // Look in global /wp-content/languages/plugins folder. |
| 33 |
} elseif (file_exists($moFileLocal)) { |
| 34 |
load_textdomain('give', $moFileLocal); // Look in local /wp-content/plugins/give/languages/ folder. |
| 35 |
} else { |
| 36 |
load_plugin_textdomain('give', false, $giveRelativePath); // Load the default language files. |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @since 3.0.0 |
| 42 |
*/ |
| 43 |
public static function setScriptTranslations($handle) |
| 44 |
{ |
| 45 |
wp_set_script_translations($handle, 'give', trailingslashit(WP_PLUGIN_DIR) . self::getRelativePath()); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Return the plugin language dir relative path, e.g. "give/languages/" |
| 50 |
* |
| 51 |
* @since 3.0.0 |
| 52 |
*/ |
| 53 |
public static function getRelativePath(): string |
| 54 |
{ |
| 55 |
$giveRelativePath = dirname(plugin_basename(GIVE_PLUGIN_FILE)) . '/languages/'; |
| 56 |
$giveRelativePath = ltrim(apply_filters('give_languages_directory', $giveRelativePath), '/\\'); |
| 57 |
|
| 58 |
return trailingslashit($giveRelativePath); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @since 3.22.0 |
| 63 |
*/ |
| 64 |
public static function getLocale() |
| 65 |
{ |
| 66 |
return apply_filters('givewp_locale', get_locale()); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @since 3.22.0 |
| 71 |
*/ |
| 72 |
public static function switchToLocale(string $locale) |
| 73 |
{ |
| 74 |
if (empty($locale) || $locale == get_locale()) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
switch_to_locale($locale); |
| 79 |
} |
| 80 |
} |
| 81 |
|