pigs_fallback.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. } else {
  52. print "<p>locale '$locale' is <strong>not</strong> supported on your system, using custom gettext implementation.</p>\n";
  53. }
  54. ?>
  55. <hr />
  56. <?php
  57. // using PHP-gettext
  58. print "<pre>";
  59. print T_("This is how the story goes.\n\n");
  60. for ($number=6; $number>=0; $number--) {
  61. print sprintf(
  62. T_ngettext(
  63. "%d pig went to the market\n",
  64. "%d pigs went to the market\n",
  65. $number
  66. ),
  67. $number
  68. );
  69. }
  70. print "</pre>\n";
  71. ?>
  72. <hr />
  73. <p>&laquo; <a href="./">back</a></p>
  74. </body>
  75. </html>