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