l10n.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Determine the current locale desired for the request.
  4. *
  5. * @since 5.0.0
  6. *
  7. * @global string $pagenow
  8. *
  9. * @return string The determined locale.
  10. */
  11. if ( ! function_exists( 'determine_locale' ) ) :
  12. function determine_locale() {
  13. /**
  14. * Filters the locale for the current request prior to the default determination process.
  15. *
  16. * Using this filter allows to override the default logic, effectively short-circuiting the function.
  17. *
  18. * @since 5.0.0
  19. *
  20. * @param string|null The locale to return and short-circuit, or null as default.
  21. */
  22. $determined_locale = apply_filters( 'pre_determine_locale', null );
  23. if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
  24. return $determined_locale;
  25. }
  26. $determined_locale = get_locale();
  27. if ( function_exists( 'get_user_locale' ) && is_admin() ) {
  28. $determined_locale = get_user_locale();
  29. }
  30. // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Copied from WordPress core.
  31. if ( function_exists( 'get_user_locale' ) && isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] ) {
  32. $determined_locale = get_user_locale();
  33. }
  34. if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
  35. $determined_locale = sanitize_text_field( $_GET['wp_lang'] );
  36. }
  37. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  38. /**
  39. * Filters the locale for the current request.
  40. *
  41. * @since 5.0.0
  42. *
  43. * @param string $locale The locale.
  44. */
  45. return apply_filters( 'determine_locale', $determined_locale );
  46. }
  47. endif;
  48. /*
  49. * acf_get_locale
  50. *
  51. * Returns the current locale.
  52. *
  53. * @date 16/12/16
  54. * @since 5.5.0
  55. *
  56. * @param void
  57. * @return string
  58. */
  59. function acf_get_locale() {
  60. // Determine local.
  61. $locale = determine_locale();
  62. // Fallback to parent language for regions without translation.
  63. // https://wpastra.com/docs/complete-list-wordpress-locale-codes/
  64. $langs = array(
  65. 'az_TR' => 'az', // Azerbaijani (Turkey)
  66. 'zh_HK' => 'zh_TW', // Chinese (Hong Kong)
  67. 'fr_BE' => 'fr_FR', // French (Belgium)
  68. 'nn_NO' => 'nb_NO', // Norwegian (Nynorsk)
  69. 'fa_AF' => 'fa_IR', // Persian (Afghanistan)
  70. 'ru_UA' => 'ru_RU', // Russian (Ukraine)
  71. );
  72. if ( isset( $langs[ $locale ] ) ) {
  73. $locale = $langs[ $locale ];
  74. }
  75. /**
  76. * Filters the determined local.
  77. *
  78. * @date 8/1/19
  79. * @since 5.7.10
  80. *
  81. * @param string $locale The local.
  82. */
  83. return apply_filters( 'acf/get_locale', $locale );
  84. }
  85. /**
  86. * acf_load_textdomain
  87. *
  88. * Loads the plugin's translated strings similar to load_plugin_textdomain().
  89. *
  90. * @date 8/1/19
  91. * @since 5.7.10
  92. *
  93. * @param string $locale The plugin's current locale.
  94. * @return void
  95. */
  96. function acf_load_textdomain( $domain = 'acf' ) {
  97. /**
  98. * Filters a plugin's locale.
  99. *
  100. * @date 8/1/19
  101. * @since 5.7.10
  102. *
  103. * @param string $locale The plugin's current locale.
  104. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  105. */
  106. $locale = apply_filters( 'plugin_locale', acf_get_locale(), $domain );
  107. $mofile = $domain . '-' . $locale . '.mo';
  108. // Load from plugin lang folder.
  109. return load_textdomain( $domain, acf_get_path( 'lang/' . $mofile ) );
  110. }
  111. /**
  112. * _acf_apply_language_cache_key
  113. *
  114. * Applies the current language to the cache key.
  115. *
  116. * @date 23/1/19
  117. * @since 5.7.11
  118. *
  119. * @param string $key The cache key.
  120. * @return string
  121. */
  122. function _acf_apply_language_cache_key( $key ) {
  123. // Get current language.
  124. $current_language = acf_get_setting( 'current_language' );
  125. if ( $current_language ) {
  126. $key = "{$key}:{$current_language}";
  127. }
  128. // Return key.
  129. return $key;
  130. }
  131. // Hook into filter.
  132. add_filter( 'acf/get_cache_key', '_acf_apply_language_cache_key' );