Root/
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of Plume Framework, a simple PHP Application Framework. # Copyright (C) 2001-2007 Loic d'Anterroches and contributors. # # Plume Framework is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Plume Framework is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * Translation utility. * * This class provides utilities to load and cache translation * strings. The functions using the values are directly available when * loading Pluf. They are __ and _n for simple translations and for * plural dependent translations respectively. * * Based on benchmarking by * localization-is-gettext-fast-enough/ the string id system is really * fast, so here the system is using a .ini file approach with a * string id cache. * * Why reimplementing a gettext system when one is already available? * It is because the PHP gettext extension requires the corresponding * locale to be installed system wide to load the corresponding * translations. If your host has no locales outside English * installed, you can only provide English to your users. Which is not * really nice. * */ class Pluf_Translation { public static $plural_forms = array ( 'fr' => 'plural_2gt1' , 'en' => 'plural_2not1' , // This is the default. 'de' => 'plural_2not1' , ); public static function loadSetLocale( $lang ) { $GLOBALS [ '_PX_current_locale' ] = $lang ; setlocale(LC_TIME, array ( $lang . '.UTF-8' , $lang . '_' . strtoupper ( $lang ). '.UTF-8' )); if (isset( $GLOBALS [ '_PX_locale' ][ $lang ])) { return ; // We consider that it was already loaded. } $GLOBALS [ '_PX_locale' ][ $lang ] = array (); foreach (Pluf::f( 'installed_apps' ) as $app ) { if (false != ( $pofile =Pluf::fileExists( $app . '/locale/' . $lang . '/' . strtolower ( $app ). '.po' ))) { $GLOBALS [ '_PX_locale' ][ $lang ] += Pluf_Translation::readPoFile( $pofile ); } } } public static function getLocale() { return $GLOBALS [ '_PX_current_locale' ]; } /** * Get the plural form for a given locale. */ public static function getPluralForm( $locale ) { if (isset(self:: $plural_forms [ $locale ])) { return self:: $plural_forms [ $locale ]; } if (isset(self:: $plural_forms [ substr ( $locale , 0, 2)])) { return self:: $plural_forms [ substr ( $locale , 0, 2)]; } return 'plural_2not1' ; } /** * Return the "best" accepted language from the list of available * languages. * * Use $_SERVER['HTTP_ACCEPT_LANGUAGE'] if the accepted language * list is empty. The list must be something like: * 'da, en-gb;q=0.8, en;q=0.7' * * @param array Available languages in the system * @param string String of comma separated accepted languages ('') * @return string Language 2 or 5 letter iso code, default is 'en' */ public static function getAcceptedLanguage( $available , $accepted = '' ) { if ( empty ( $accepted )) { if (! empty ( $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ])) { $accepted = $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ]; } else { return 'en' ; } } $acceptedlist = explode ( ',' , $accepted ); foreach ( $acceptedlist as $lang ) { $lang = explode ( ';' , $lang ); $lang = trim( $lang [0]); if (in_array( $lang , $available )) { return $lang ; } // for the xx-XX cases we may have only the "main" language // form like xx is available $lang = substr ( $lang , 0, 2); if (in_array( $lang , $available )) { return $lang ; } } $langs = Pluf::f( 'languages' , array ( 'en' )); return $langs [0]; } /** * Given a key indexed array, do replacement using the %%key%% in * the string. */ public static function sprintf( $string , $words = array ()) { foreach ( $words as $key => $word ) { $string = mb_ereg_replace( '%%' . $key . '%%' , $word , $string , 'm' ); } return $string ; } /** * French, Brazilian Portuguese */ public static function plural_2gt1( $n ) { return (int) ( $n >1); } public static function plural_2not1( $n ) { return (int) ( $n !=1); } /** * Read a .po file. * * Based on the work by Matthias Bauer with some little cosmetic fixes. * * @source http://wordpress-soc-2007.googlecode.com/svn/trunk/moeffju/php-msgfmt/msgfmt-functions.php * @copyright 2007 Matthias Bauer * @author Matthias Bauer * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License 2.1 * @license http://opensource.org/licenses/apache2.0.php Apache License 2.0 */ public static function readPoFile( $file ) { if (false !== ( $hash =self::getCachedFile( $file ))) { return $hash ; } // read .po file $fc = file_get_contents ( $file ); // normalize newlines $fc = str_replace ( array ( "\r\n" , "\r" ), array ( "\n" , "\n" ), $fc ); // results array $hash = array (); // temporary array $temp = array (); // state $state = null; $fuzzy = false; // iterate over lines foreach ( explode ( "\n" , $fc ) as $line ) { $line = trim( $line ); if ( $line === '' ) continue ; if (false === strpos ( $line , ' ' )) { $key = $line ; $data = '' ; } else { list ( $key , $data )= explode ( ' ' , $line , 2); } switch ( $key ) { case '#,' : // flag... $fuzzy = in_array( 'fuzzy' , preg_split( '/,\s*/' , $data )); case '#' : // translator-comments case '#.' : // extracted-comments case '#:' : // reference... case '#|' : // msgid previous-untranslated-string case '#~' : // deprecated translations // start a new entry if (sizeof( $temp ) && array_key_exists ( 'msgid' , $temp ) && array_key_exists ( 'msgstr' , $temp )) { if (! $fuzzy ) $hash []= $temp ; $temp = array (); $state = null; $fuzzy = false; } break ; case 'msgctxt' : // context case 'msgid' : // untranslated-string case 'msgid_plural' : // untranslated-string-plural $state = $key ; $temp [ $state ]= $data ; break ; case 'msgstr' : // translated-string $state = 'msgstr' ; $temp [ $state ][]= $data ; break ; default : if ( strpos ( $key , 'msgstr[' ) !== False) { // translated-string-case-n $state = 'msgstr' ; $temp [ $state ][]= $data ; } else { // continued lines switch ( $state ) { case 'msgctxt' : case 'msgid' : case 'msgid_plural' : $temp [ $state ] .= "\n" . $line ; break ; case 'msgstr' : $temp [ $state ][sizeof( $temp [ $state ]) - 1] .= "\n" . $line ; break ; default : // parse error return False; } } break ; } } // add final entry if ( $state == 'msgstr' ) $hash []= $temp ; // Cleanup data, merge multiline entries, reindex hash for ksort $temp = $hash ; $hash = array (); foreach ( $temp as $entry ) { foreach ( $entry as & $v ) { $v = Pluf_Translation_poCleanHelper( $v ); if ( $v === False) { // parse error return False; } } if (isset( $entry [ 'msgid_plural' ])) { $hash [ $entry [ 'msgid' ]. '#' . $entry [ 'msgid_plural' ]]= $entry [ 'msgstr' ]; } else { $hash [ $entry [ 'msgid' ]]= $entry [ 'msgstr' ]; } } self::cacheFile( $file , $hash ); return $hash ; } /** * Load optimized version of a language file if available. * * @return mixed false or array with value */ public static function getCachedFile( $file ) { $phpfile = Pluf::f( 'tmp_folder' ). '/Pluf_L10n-' .md5( $file ). '.phps' ; if ( file_exists ( $phpfile ) && ( filemtime ( $file ) < filemtime ( $phpfile ))) { return include $phpfile ; } return false; } /** * Cache an optimized version of a language file. * * @param string File * @param array Parsed hash */ public static function cacheFile( $file , $hash ) { $phpfile = Pluf::f( 'tmp_folder' ). '/Pluf_L10n-' .md5( $file ). '.phps' ; file_put_contents ( $phpfile , '<?php return ' .var_export( $hash , true). '; ?>' , LOCK_EX); @ chmod ( $phpfile , 0666); } } function Pluf_Translation_poCleanHelper( $x ) { if ( is_array ( $x )) { foreach ( $x as $k => $v ) { $x [ $k ]= Pluf_Translation_poCleanHelper( $v ); } } else { if ( $x [0] == '"' ) $x = substr ( $x , 1, -1); $x = str_replace ( "\"\n\"" , '' , $x ); $x = str_replace ( '$' , '\\$' , $x ); $x = @ eval ( "return \"$x\";" ); } return $x ; } |
Source at commit 55305a934bac created 10 years 8 months ago. By Nathan Adams, Fixing bug where password would not be hashed in database if user updated password |
---|