| | 1 | <?PHP |
| | 2 | |
| | 3 | /** |
| | 4 | * Native GetText-Support for WordPress |
| | 5 | * ------------------------------------ |
| | 6 | * The Patch enhanced WordPress with native support for gettext. |
| | 7 | * Actually WP is shipped with an own implementation called "PoMo" |
| | 8 | * that uses a lot of resources like CPU-Time and Memory. |
| | 9 | * Using gettext turned out to be much faster and efficient. |
| | 10 | * |
| | 11 | * Copyright (C) 2012 Bernd Holzmueller <bernd@quarxconnect.de> |
| | 12 | * |
| | 13 | * This program is free software: you can redistribute it and/or modify |
| | 14 | * it under the terms of the GNU General Public License as published by |
| | 15 | * the Free Software Foundation, either version 3 of the License, or |
| | 16 | * (at your option) any later version. |
| | 17 | * |
| | 18 | * This program is distributed in the hope that it will be useful, |
| | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 21 | * GNU General Public License for more details. |
| | 22 | * |
| | 23 | * You should have received a copy of the GNU General Public License |
| | 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| | 25 | * |
| | 26 | * @revision 02 |
| | 27 | * @author Bernd Holzmueller <bernd@tiggerswelt.net> |
| | 28 | * @url http://oss.tiggerswelt.net/wordpress/3.3.1/ |
| | 29 | **/ |
| | 30 | |
| | 31 | // Check if gettext-support is available |
| | 32 | if (!extension_loaded ('gettext')) |
| | 33 | return; |
| | 34 | |
| | 35 | class Translate_GetText_Native { |
| | 36 | // Our default domain |
| | 37 | private $Domain = null; |
| | 38 | |
| | 39 | // Merged domains |
| | 40 | private $pOthers = array (); |
| | 41 | private $sOthers = array (); |
| | 42 | |
| | 43 | // Some Dummy-Function just to be API-compatible |
| | 44 | function add_entry ($entry) { return false; } |
| | 45 | function add_entry_or_merge ($entry) { return false; } |
| | 46 | function set_header ($header, $value) { return false; } |
| | 47 | function set_headers (&$headers) { return false; } |
| | 48 | function get_header ($header) { return false; } |
| | 49 | function translate_entry (&$entry) { return false; } |
| | 50 | |
| | 51 | // {{{ select_plural_form |
| | 52 | /** |
| | 53 | * Given the number of items, returns the 0-based index of the plural form to use |
| | 54 | * |
| | 55 | * Here, in the base Translations class, the common logic for English is implemented: |
| | 56 | * 0 if there is one element, 1 otherwise |
| | 57 | * |
| | 58 | * This function should be overrided by the sub-classes. For example MO/PO can derive the logic |
| | 59 | * from their headers. |
| | 60 | * |
| | 61 | * @param integer $count number of items |
| | 62 | **/ |
| | 63 | function select_plural_form ($count) { |
| | 64 | return (1 == $count? 0 : 1); |
| | 65 | } |
| | 66 | // }}} |
| | 67 | |
| | 68 | function get_plural_forms_count () { return 2; } |
| | 69 | |
| | 70 | // {{{ merge_with |
| | 71 | /** |
| | 72 | * Merge this translation with another one, the other one takes precedence |
| | 73 | * |
| | 74 | * @param object $other |
| | 75 | * |
| | 76 | * @access public |
| | 77 | * @return void |
| | 78 | **/ |
| | 79 | function merge_with (&$other) { |
| | 80 | $this->pOthers [] = $other; |
| | 81 | } |
| | 82 | // }}} |
| | 83 | |
| | 84 | // {{{ merge_originals_with |
| | 85 | /** |
| | 86 | * Merge this translation with another one, this one takes precedence |
| | 87 | * |
| | 88 | * @param object $other |
| | 89 | * |
| | 90 | * @access public |
| | 91 | * @return void |
| | 92 | **/ |
| | 93 | function merge_originals_with (&$other) { |
| | 94 | $this->sOthers [] = $Other; |
| | 95 | } |
| | 96 | // }}} |
| | 97 | |
| | 98 | // {{{ translate |
| | 99 | /** |
| | 100 | * Try to translate a given string |
| | 101 | * |
| | 102 | * @param string $singular |
| | 103 | * @param string $context (optional) |
| | 104 | * |
| | 105 | * @access public |
| | 106 | * @return string |
| | 107 | **/ |
| | 108 | function translate ($singular, $context = null) { |
| | 109 | // Check for an empty string |
| | 110 | if (strlen ($singular) == 0) |
| | 111 | return $singular; |
| | 112 | |
| | 113 | // Check other domains that take precedence |
| | 114 | foreach ($this->pOthers as $o) |
| | 115 | if (($t = $o->translate ($singular, $context)) != $singular) |
| | 116 | return $t; |
| | 117 | |
| | 118 | // Make sure we have a domain assigned |
| | 119 | if ($this->Domain === null) |
| | 120 | return $singular; |
| | 121 | |
| | 122 | // Translate without a context |
| | 123 | if ($context === null) { |
| | 124 | if (($t = dgettext ($this->Domain, $singular)) != $singular) |
| | 125 | return $t; |
| | 126 | |
| | 127 | // Translate with a given context |
| | 128 | } else { |
| | 129 | $T = $context . "\x04" . $singular; |
| | 130 | $t = dgettext ($this->Domain, $T); |
| | 131 | |
| | 132 | if ($T != $t) |
| | 133 | return $t; |
| | 134 | } |
| | 135 | |
| | 136 | // Check for other domains |
| | 137 | foreach ($this->sOthers as $o) |
| | 138 | if (($t = $o->translate ($singular, $context)) != $singular) |
| | 139 | return $t; |
| | 140 | |
| | 141 | return $singular; |
| | 142 | } |
| | 143 | // }}} |
| | 144 | |
| | 145 | // {{{ translate_plural |
| | 146 | /** |
| | 147 | * Try to translate a plural string |
| | 148 | * |
| | 149 | * @param string $singular Singular version |
| | 150 | * @param string $plural Plural version |
| | 151 | * @param int $count Number of "items" |
| | 152 | * @param string $context (optional) |
| | 153 | * |
| | 154 | * @access public |
| | 155 | * @return string |
| | 156 | **/ |
| | 157 | function translate_plural ($singular, $plural, $count, $context = null) { |
| | 158 | // Check for an empty string |
| | 159 | if (strlen ($singular) == 0) |
| | 160 | return $singular; |
| | 161 | |
| | 162 | // Get the "default" return-value |
| | 163 | $default = ($count == 1 ? $singular : $plural); |
| | 164 | |
| | 165 | // Check other domains that take precedence |
| | 166 | foreach ($this->pOthers as $o) |
| | 167 | if (($t = $o->translate_plural ($singular, $plural, $count, $context)) != $default) |
| | 168 | return $t; |
| | 169 | |
| | 170 | // Make sure we have a domain assigned |
| | 171 | if ($this->Domain === null) |
| | 172 | return $default; |
| | 173 | |
| | 174 | // Translate without context |
| | 175 | if ($context === null) { |
| | 176 | $t = dngettext ($this->Domain, $singular, $plural, $count); |
| | 177 | |
| | 178 | if (($t != $singular) && ($t != $plural)) |
| | 179 | return $t; |
| | 180 | |
| | 181 | // Translate using a given context |
| | 182 | } else { |
| | 183 | $T = $context . "\x04" . $singular; |
| | 184 | $t = dngettext ($this->Domain, $T, $plural, $count); |
| | 185 | |
| | 186 | if (($T != $t) && ($t != $plural)) |
| | 187 | return $t; |
| | 188 | } |
| | 189 | |
| | 190 | // Check other domains |
| | 191 | foreach ($this->sOthers as $o) |
| | 192 | if (($t = $o->translate_plural ($singular, $plural, $count, $context)) != $default) |
| | 193 | return $t; |
| | 194 | |
| | 195 | return $default; |
| | 196 | } |
| | 197 | // }}} |
| | 198 | |
| | 199 | // {{{ import_from_file |
| | 200 | /** |
| | 201 | * Fills up with the entries from MO file $filename |
| | 202 | * |
| | 203 | * @param string $filename MO file to load |
| | 204 | **/ |
| | 205 | function import_from_file ($filename) { |
| | 206 | // Make sure that the locale is set correctly in environment |
| | 207 | global $locale; |
| | 208 | |
| | 209 | putenv ('LC_ALL=' . $locale); |
| | 210 | setlocale (LC_ALL, $locale); |
| | 211 | |
| | 212 | // Retrive MD5-hash of the file |
| | 213 | # DIRTY! But there is no other way at the moment to make this work |
| | 214 | if (!($Domain = md5_file ($filename))) |
| | 215 | return false; |
| | 216 | |
| | 217 | // Make sure that the language-directory exists |
| | 218 | $Path = './wp-lang/' . $locale . '/LC_MESSAGES'; |
| | 219 | |
| | 220 | if (!wp_mkdir_p ($Path)) |
| | 221 | return false; |
| | 222 | |
| | 223 | // Make sure that the MO-File is existant at the destination |
| | 224 | $fn = $Path . '/' . $Domain . '.mo'; |
| | 225 | |
| | 226 | if (!is_file ($fn) && !@copy ($filename, $fn)) |
| | 227 | return false; |
| | 228 | |
| | 229 | // Setup the "domain" for gettext |
| | 230 | bindtextdomain ($Domain, './wp-lang/'); |
| | 231 | bind_textdomain_codeset ($Domain, 'UTF-8'); |
| | 232 | |
| | 233 | // Do the final stuff and return success |
| | 234 | $this->Domain = $Domain; |
| | 235 | |
| | 236 | return true; |
| | 237 | } |
| | 238 | // }}} |
| | 239 | } |
| | 240 | |
| | 241 | if (function_exists ('class_alias')) |
| | 242 | class_alias ('Translate_GetText_Native', 'MO'); |
| | 243 | else { |
| | 244 | class MO extends Translate_GetText_Native { } |
| | 245 | } |
| | 246 | |
| | 247 | ?> |
| | 248 | No newline at end of file |