pigs_fallback.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. Copyright (c) 2003,2004,2005,2009 Danilo Segan <danilo@kvota.net>.
  4. Copyright (c) 2005,2006 Steven Armstrong <sa@c-area.ch>
  5. This file is part of PHP-gettext.
  6. PHP-gettext is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. PHP-gettext is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with PHP-gettext; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. error_reporting(E_ALL | E_STRICT);
  19. // define constants
  20. define('PROJECT_DIR', realpath('./'));
  21. define('LOCALE_DIR', PROJECT_DIR .'/locale');
  22. define('DEFAULT_LOCALE', 'en_US');
  23. require_once('../gettext.inc');
  24. $supported_locales = array('en_US', 'sr_CS', 'de_CH');
  25. $encoding = 'UTF-8';
  26. $locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;
  27. // gettext setup
  28. T_setlocale(LC_MESSAGES, $locale);
  29. // Set the text domain as 'messages'
  30. $domain = 'messages';
  31. T_bindtextdomain($domain, LOCALE_DIR);
  32. T_bind_textdomain_codeset($domain, $encoding);
  33. T_textdomain($domain);
  34. header("Content-type: text/html; charset=$encoding");
  35. ?>
  36. <html>
  37. <head>
  38. <title>PHP-gettext fallback example</title>
  39. </head>
  40. <body>
  41. <h1>PHP-gettext as a fallback solution</h1>
  42. <p>Example showing how to use PHP-gettext as a fallback solution if the native gettext library is not available or the system does not support the requested locale.</p>
  43. <?php
  44. print "<p>";
  45. foreach($supported_locales as $l) {
  46. print "[<a href=\"?lang=$l\">$l</a>] ";
  47. }
  48. print "</p>\n";
  49. if (!locale_emulation()) {
  50. print "<p>locale '$locale' is supported by your system, using native gettext implementation.</p>\n";
  51. }
  52. else {
  53. print "<p>locale '$locale' is <strong>not</strong> supported on your system, using custom gettext implementation.</p>\n";
  54. }
  55. ?>
  56. <hr />
  57. <?php
  58. // using PHP-gettext
  59. print "<pre>";
  60. print T_("This is how the story goes.\n\n");
  61. for ($number=6; $number>=0; $number--) {
  62. print sprintf( T_ngettext("%d pig went to the market\n",
  63. "%d pigs went to the market\n", $number),
  64. $number );
  65. }
  66. print "</pre>\n";
  67. ?>
  68. <hr />
  69. <p>&laquo; <a href="./">back</a></p>
  70. </body>
  71. </html>