Index: trunk/tools/ToolserverI18N/TsIntuition.php |
— | — | @@ -30,7 +30,7 @@ |
31 | 31 | private $localBaseDir = __DIR__; // to be moved to p_i18n |
32 | 32 | |
33 | 33 | private static $registeredTextdomains = array( |
34 | | - 'General' => 'General.i18n.php', |
| 34 | + 'general' => 'General.i18n.php', |
35 | 35 | 'Getwikiapi' => 'Getwikiapi.i18n.php', |
36 | 36 | 'Jarry' => 'Jarry.i18n.php', |
37 | 37 | 'Svgtranslate' => 'Svgtranslate.i18n.php', |
— | — | @@ -53,7 +53,7 @@ |
54 | 54 | private $paramNames = array( 'userlang' => 'userlang' ); |
55 | 55 | |
56 | 56 | // Here everything will be stored as arrays in arrays |
57 | | - // Such as: $messageBlob['textdomain']['messagename']['langcode'] = 'Message string'; |
| 57 | + // Such as: $messageBlob['Textdomain']['messagename']['langcode'] = 'Message string'; |
58 | 58 | private $messageBlob = array(); |
59 | 59 | |
60 | 60 | // $loadedTextdomains[0] = 'general'; |
— | — | @@ -131,7 +131,7 @@ |
132 | 132 | // Otherwise defaults to 'general'. See also documentation of msg() |
133 | 133 | // First character is case-insensitive |
134 | 134 | if ( isset( $options['domain'] ) ) { |
135 | | - $this->setDomain( trim( lcfirst( $options['domain'] ) ) ); |
| 135 | + $this->setDomain( $options['domain'] ); |
136 | 136 | } |
137 | 137 | |
138 | 138 | // TsIntuition will choose the language based on a cookie. However it |
— | — | @@ -208,7 +208,7 @@ |
209 | 209 | } |
210 | 210 | |
211 | 211 | /** |
212 | | - * DOCME |
| 212 | + * Return the currently selected text domain. |
213 | 213 | * @return string |
214 | 214 | */ |
215 | 215 | public function getDomain(){ |
— | — | @@ -216,11 +216,11 @@ |
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
220 | | - * DOCME |
| 220 | + * Get an array of all registered textd domains. |
221 | 221 | * @return array |
222 | 222 | */ |
223 | | - public function getAllRegisteredDomain(){ |
224 | | - return $this->registeredTextdomains; |
| 223 | + public function getAllRegisteredDomains(){ |
| 224 | + return self::$registeredTextdomains; |
225 | 225 | } |
226 | 226 | |
227 | 227 | /** |
— | — | @@ -228,7 +228,7 @@ |
229 | 229 | * @return true |
230 | 230 | */ |
231 | 231 | public function setDomain( $domain ) { |
232 | | - $this->currentTextdomain = $domain; |
| 232 | + $this->currentTextdomain = ucfirst( strtolower( $domain ) ); |
233 | 233 | return true; |
234 | 234 | } |
235 | 235 | |
— | — | @@ -341,29 +341,29 @@ |
342 | 342 | } |
343 | 343 | |
344 | 344 | // First character of the message-key is case-insensitive. |
345 | | - $key = trim( lcfirst( $key ) ); |
| 345 | + $key = lcfirst( $key ); |
| 346 | + $domain = ucfirst( strtolower( $options['domain'] ) ); |
346 | 347 | |
347 | 348 | // Load if not already loaded |
348 | | - $this->loadTextdomain( $options['domain'] ); |
| 349 | + $this->loadTextdomain( $domain ); |
349 | 350 | |
350 | | - // In case the domainname was invalid or inexistant |
351 | | - if ( !isset( $this->messageBlob[$options['domain']] ) ) { |
| 351 | + // In case the domain name was invalid or inexistant |
| 352 | + if ( !isset( $this->messageBlob[$domain] ) ) { |
352 | 353 | return $this->bracketMsg( $key ); |
353 | 354 | } |
354 | 355 | |
355 | 356 | // Use fallback if this message doesn't exist in the current language |
356 | | - $lang = $this->getLangForTextdomain( $options['lang'], $options['domain'], $key ); |
| 357 | + $lang = $this->getLangForTextdomain( $options['lang'], $domain, $key ); |
357 | 358 | |
358 | 359 | // Just in case, one last check: |
359 | | - if ( isset( $this->messageBlob[$options['domain']][$lang][$key] ) ) { |
360 | | - // We're ok, let's grab it |
361 | | - $msg = $this->messageBlob[$options['domain']][$lang][$key]; |
362 | | - } else { |
| 360 | + $rawMsg = $this->rawMsg( $domain, $lang, $key ); |
| 361 | + if ( is_null( $rawMsg ) ) { |
363 | 362 | // Fall back to a simple [keyname] |
364 | 363 | return $this->bracketMsg( $key ); |
365 | 364 | } |
366 | 365 | |
367 | 366 | /* Now that we've got the message, do post-processing. */ |
| 367 | + $msg = $rawMsg; |
368 | 368 | |
369 | 369 | $escapeDone = false; |
370 | 370 | |
— | — | @@ -391,6 +391,21 @@ |
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
| 395 | + * Access MessageBlob |
| 396 | + * @param $domain |
| 397 | + * @param $lang |
| 398 | + * @param $key |
| 399 | + * @return string value or null. |
| 400 | + */ |
| 401 | + private function rawMsg( $domain, $lang, $key ) { |
| 402 | + if ( isset( $this->messageBlob[$domain][$lang][$key] ) ) { |
| 403 | + return $this->messageBlob[$domain][$lang][$key]; |
| 404 | + } else { |
| 405 | + return null; |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + /** |
395 | 410 | * Don't show [brackets] when suppressing errors. |
396 | 411 | * In that case there could be message files missing and invalid language codes chosen. |
397 | 412 | * Just return a somewhat readable string. |
— | — | @@ -416,6 +431,8 @@ |
417 | 432 | |
418 | 433 | if ( !TsIntuitionUtil::nonEmptyStr( $domain ) ) { |
419 | 434 | $domain = $this->getDomain(); |
| 435 | + } else { |
| 436 | + $domain = ucfirst( strtolower( $domain ) ); |
420 | 437 | } |
421 | 438 | if ( !TsIntuitionUtil::nonEmptyStr( $lang ) ) { |
422 | 439 | $lang = $this->getLang(); |
— | — | @@ -444,8 +461,8 @@ |
445 | 462 | * passed right away, otherwise it looks for a suitable falback |
446 | 463 | * |
447 | 464 | * @param $lang string Preferred language |
448 | | - * @param $domain string Domain to search within (the existance of this domain should be checked |
449 | | - * before calling this function). |
| 465 | + * @param $domain string Domain to search within (the existence of this domain should be checked |
| 466 | + * before calling this function). Note that the domainname should've been sanatized by now. |
450 | 467 | * @param $key string Key of the wanted message |
451 | 468 | * @return string Language code |
452 | 469 | */ |
— | — | @@ -517,24 +534,28 @@ |
518 | 535 | /* Textdomain functions |
519 | 536 | * ------------------------------------------------- */ |
520 | 537 | |
| 538 | + /** |
| 539 | + * Load a textdomain (if not loaded already). |
| 540 | + * |
| 541 | + * @param $domain string Name of the textdomain (case-insensitive) |
| 542 | + */ |
521 | 543 | public function loadTextdomain( $domain ) { |
522 | 544 | |
523 | 545 | // Generally validate input and protect against path traversal |
524 | | - if ( !TsIntuitionUtil::nonEmptyStr( $domain ) || $domain !== trim( strtolower( $domain ) ) || strcspn( $domain, ":/\\\000" ) !== strlen( $domain ) ) { |
| 546 | + if ( !TsIntuitionUtil::nonEmptyStr( $domain ) || strcspn( $domain, ":/\\\000" ) !== strlen( $domain ) ) { |
525 | 547 | $this->errTrigger( "Invalid textdomain \"$domain\"", __METHOD__, E_NOTICE ); |
526 | 548 | return false; |
527 | 549 | } |
| 550 | + // Sanatize domainnames (case-insensitive) |
| 551 | + $domain = ucfirst( strtolower( $domain ) ); |
528 | 552 | |
529 | 553 | // Already loaded ? |
530 | 554 | if ( in_array( $domain, $this->loadedTextdomains ) ) { |
531 | 555 | return false; |
532 | 556 | } |
533 | 557 | |
534 | | - $this->loadedTextdomains[] = $domain; |
535 | | - |
536 | 558 | // File exists ? |
537 | | - $displayname = ucfirst( strtolower( $domain ) ); |
538 | | - $path = $this->localBaseDir . "/language/messages/$displayname.i18n.php"; |
| 559 | + $path = $this->localBaseDir . "/language/messages/$domain.i18n.php"; |
539 | 560 | if ( !file_exists( $path ) ) { |
540 | 561 | $this->errTrigger( "Textdomain file not found for \"$domain\" at $path. Ignoring", __METHOD__, E_NOTICE, __FILE__, __LINE__ ); |
541 | 562 | return false; |
— | — | @@ -576,10 +597,12 @@ |
577 | 598 | // If you need to add or overwrite some messages temporarily, use Itui::setMsg() or Itui::setMsgs() instead |
578 | 599 | foreach ( $data['messages'] as $langcode => $messages ) { |
579 | 600 | $this->availableLanguages[$langcode] = true; |
580 | | - $this->messageBlob[$domain][$langcode] = (array)$messages; |
| 601 | + $this->setMsgs( (array)$messages, $domain, $langcode ); |
581 | 602 | } |
582 | 603 | |
| 604 | + $this->loadedTextdomains[] = $domain; |
583 | 605 | |
| 606 | + |
584 | 607 | return true; |
585 | 608 | } |
586 | 609 | |
— | — | @@ -733,9 +756,9 @@ |
734 | 757 | |
735 | 758 | return true; |
736 | 759 | } |
737 | | - |
738 | 760 | |
739 | 761 | |
| 762 | + |
740 | 763 | /* Other functions |
741 | 764 | * ------------------------------------------------- */ |
742 | 765 | |
Index: trunk/tools/ToolserverI18N/public_html/index.php |
— | — | @@ -30,6 +30,12 @@ |
31 | 31 | ); |
32 | 32 | $I18N = new TsIntuition( $opts ); |
33 | 33 | |
| 34 | +// Load all domains so can get some statistics later on and |
| 35 | +// make sure "getAvailableLangs" is compelte |
| 36 | +foreach ( $I18N->getAllRegisteredDomains() as $domain => $file ) { |
| 37 | + $I18N->loadTextdomain( $domain ); |
| 38 | +} |
| 39 | + |
34 | 40 | /** |
35 | 41 | * Configuration |
36 | 42 | * ------------------------------------------------- |