PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/functions/i18n.php

https://bitbucket.org/lgorence/quickpress
PHP | 209 lines | 60 code | 33 blank | 116 comment | 18 complexity | b6c1e4df4f66ded396375026a6ae073d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Internationalization and translation functions. Because Hybrid Core is a framework made up of various
  4. * extensions with different textdomains, it must filter 'gettext' so that a single translation file can
  5. * handle all translations.
  6. *
  7. * @package HybridCore
  8. * @subpackage Functions
  9. * @author Justin Tadlock <justin@justintadlock.com>
  10. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  11. * @link http://themehybrid.com/hybrid-core
  12. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. */
  14. /**
  15. * Checks if a textdomain's translation files have been loaded. This function behaves differently from
  16. * WordPress core's is_textdomain_loaded(), which will return true after any translation function is run over
  17. * a text string with the given domain. The purpose of this function is to simply check if the translation files
  18. * are loaded.
  19. *
  20. * @since 1.3.0
  21. * @access private This is only used internally by the framework for checking translations.
  22. * @param string $domain The textdomain to check translations for.
  23. */
  24. function hybrid_is_textdomain_loaded( $domain ) {
  25. global $hybrid;
  26. return ( isset( $hybrid->textdomain_loaded[$domain] ) && true === $hybrid->textdomain_loaded[$domain] ) ? true : false;
  27. }
  28. /**
  29. * Loads the framework's translation files. The function first checks if the parent theme or child theme
  30. * has the translation files housed in their '/languages' folder. If not, it sets the translation file the the
  31. * framework '/languages' folder.
  32. *
  33. * @since 1.3.0
  34. * @access private
  35. * @uses load_textdomain() Loads an MO file into the domain for the framework.
  36. * @param string $domain The name of the framework's textdomain.
  37. * @return true|false Whether the MO file was loaded.
  38. */
  39. function hybrid_load_framework_textdomain( $domain ) {
  40. /* Get the WordPress installation's locale set by the user. */
  41. $locale = get_locale();
  42. /* Check if the mofile is located in parent/child theme /languages folder. */
  43. $mofile = locate_template( array( "languages/{$domain}-{$locale}.mo" ) );
  44. /* If no mofile was found in the parent/child theme, set it to the framework's mofile. */
  45. if ( empty( $mofile ) )
  46. $mofile = trailingslashit( HYBRID_LANGUAGES ) . "{$domain}-{$locale}.mo";
  47. return load_textdomain( $domain, $mofile );
  48. }
  49. /**
  50. * @since 0.7.0
  51. * @deprecated 1.3.0
  52. */
  53. function hybrid_get_textdomain() {
  54. _deprecated_function( __FUNCTION__, '1.3.0', 'hybrid_get_parent_textdomain' );
  55. return hybrid_get_parent_textdomain();
  56. }
  57. /**
  58. * Gets the parent theme textdomain. This allows the framework to recognize the proper textdomain of the
  59. * parent theme.
  60. *
  61. * Important! Do not use this for translation functions in your theme. Hardcode your textdomain string. Your
  62. * theme's textdomain should match your theme's folder name.
  63. *
  64. * @since 1.3.0
  65. * @access private
  66. * @uses get_template() Defines the theme textdomain based on the template directory.
  67. * @global object $hybrid The global Hybrid object.
  68. * @return string $hybrid->textdomain The textdomain of the theme.
  69. */
  70. function hybrid_get_parent_textdomain() {
  71. global $hybrid;
  72. /* If the global textdomain isn't set, define it. Plugin/theme authors may also define a custom textdomain. */
  73. if ( empty( $hybrid->parent_textdomain ) )
  74. $hybrid->parent_textdomain = sanitize_key( apply_filters( hybrid_get_prefix() . '_parent_textdomain', get_template() ) );
  75. /* Return the expected textdomain of the parent theme. */
  76. return $hybrid->parent_textdomain;
  77. }
  78. /**
  79. * Gets the child theme textdomain. This allows the framework to recognize the proper textdomain of the
  80. * child theme.
  81. *
  82. * Important! Do not use this for translation functions in your theme. Hardcode your textdomain string. Your
  83. * theme's textdomain should match your theme's folder name.
  84. *
  85. * @since 1.2.0
  86. * @access private
  87. * @uses get_stylesheet() Defines the child theme textdomain based on the stylesheet directory.
  88. * @global object $hybrid The global Hybrid object.
  89. * @return string $hybrid->child_theme_textdomain The textdomain of the child theme.
  90. */
  91. function hybrid_get_child_textdomain() {
  92. global $hybrid;
  93. /* If a child theme isn't active, return an empty string. */
  94. if ( !is_child_theme() )
  95. return '';
  96. /* If the global textdomain isn't set, define it. Plugin/theme authors may also define a custom textdomain. */
  97. if ( empty( $hybrid->child_textdomain ) )
  98. $hybrid->child_textdomain = sanitize_key( apply_filters( hybrid_get_prefix() . '_child_textdomain', get_stylesheet() ) );
  99. /* Return the expected textdomain of the child theme. */
  100. return $hybrid->child_textdomain;
  101. }
  102. /**
  103. * Filters the 'load_textdomain_mofile' filter hook so that we can change the directory and file name
  104. * of the mofile for translations. This allows child themes to have a folder called /languages with translations
  105. * of their parent theme so that the translations aren't lost on a parent theme upgrade.
  106. *
  107. * @since 1.3.0
  108. * @access private
  109. * @param string $mofile File name of the .mo file.
  110. * @param string $domain The textdomain currently being filtered.
  111. * @return $mofile
  112. */
  113. function hybrid_load_textdomain_mofile( $mofile, $domain ) {
  114. /* If the $domain is for the parent or child theme, search for a $domain-$locale.mo file. */
  115. if ( $domain == hybrid_get_parent_textdomain() || $domain == hybrid_get_child_textdomain() ) {
  116. /* Check for a $domain-$locale.mo file in the parent and child theme root and /languages folder. */
  117. $locale = get_locale();
  118. $locate_mofile = locate_template( array( "languages/{$domain}-{$locale}.mo", "{$domain}-{$locale}.mo" ) );
  119. /* If a mofile was found based on the given format, set $mofile to that file name. */
  120. if ( !empty( $locate_mofile ) )
  121. $mofile = $locate_mofile;
  122. }
  123. /* Return the $mofile string. */
  124. return $mofile;
  125. }
  126. /**
  127. * Filters 'gettext' to change the translations used for the 'hybrid-core' textdomain. This filter makes it possible
  128. * for the theme's MO file to translate the framework's text strings.
  129. *
  130. * @since 1.3.0
  131. * @access private
  132. * @param string $translated The translated text.
  133. * @param string $text The original, untranslated text.
  134. * @param string $domain The textdomain for the text.
  135. * @return string $translated
  136. */
  137. function hybrid_gettext( $translated, $text, $domain ) {
  138. /* Check if 'hybrid-core' is the current textdomain, there's no mofile for it, and the theme has a mofile. */
  139. if ( 'hybrid-core' == $domain && !hybrid_is_textdomain_loaded( 'hybrid-core' ) && hybrid_is_textdomain_loaded( hybrid_get_parent_textdomain() ) ) {
  140. /* Get the translations for the theme. */
  141. $translations = &get_translations_for_domain( hybrid_get_parent_textdomain() );
  142. /* Translate the text using the theme's translation. */
  143. $translated = $translations->translate( $text );
  144. }
  145. return $translated;
  146. }
  147. /**
  148. * Filters 'gettext' to change the translations used for the each of the extensions' textdomains. This filter
  149. * makes it possible for the theme's MO file to translate the framework's extensions.
  150. *
  151. * @since 1.3.0
  152. * @access private
  153. * @param string $translated The translated text.
  154. * @param string $text The original, untranslated text.
  155. * @param string $domain The textdomain for the text.
  156. * @return string $translated
  157. */
  158. function hybrid_extensions_gettext( $translated, $text, $domain ) {
  159. /* Check if the current textdomain matches one of the framework extensions. */
  160. if ( in_array( $domain, array( 'breadcrumb-trail', 'custom-field-series', 'post-stylesheets', 'theme-layouts' ) ) ) {
  161. /* If the theme supports the extension, switch the translations. */
  162. if ( current_theme_supports( $domain ) ) {
  163. /* If the framework mofile is loaded, use its translations. */
  164. if ( hybrid_is_textdomain_loaded( 'hybrid-core' ) )
  165. $translations = &get_translations_for_domain( 'hybrid-core' );
  166. /* If the theme mofile is loaded, use its translations. */
  167. elseif ( hybrid_is_textdomain_loaded( hybrid_get_parent_textdomain() ) )
  168. $translations = &get_translations_for_domain( hybrid_get_parent_textdomain() );
  169. /* If translations were found, translate the text. */
  170. if ( !empty( $translations ) )
  171. $translated = $translations->translate( $text );
  172. }
  173. }
  174. return $translated;
  175. }
  176. ?>