pigs_dropin.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. bindtextdomain($domain, LOCALE_DIR);
  32. // bind_textdomain_codeset is supported only in PHP 4.2.0+
  33. if (function_exists('bind_textdomain_codeset')) {
  34. bind_textdomain_codeset($domain, $encoding);
  35. }
  36. textdomain($domain);
  37. header("Content-type: text/html; charset=$encoding");
  38. ?>
  39. <html>
  40. <head>
  41. <title>PHP-gettext dropin example</title>
  42. </head>
  43. <body>
  44. <h1>PHP-gettext as a dropin replacement</h1>
  45. <p>Example showing how to use PHP-gettext as a dropin replacement for the native gettext library.</p>
  46. <?php
  47. print "<p>";
  48. foreach ($supported_locales as $l) {
  49. print "[<a href=\"?lang=$l\">$l</a>] ";
  50. }
  51. print "</p>\n";
  52. if (!locale_emulation()) {
  53. print "<p>locale '$locale' is supported by your system, using native gettext implementation.</p>\n";
  54. } else {
  55. print "<p>locale '$locale' is _not_ supported on your system, using the default locale '". DEFAULT_LOCALE ."'.</p>\n";
  56. }
  57. ?>
  58. <hr />
  59. <?php
  60. // using PHP-gettext
  61. print "<pre>";
  62. print _("This is how the story goes.\n\n");
  63. for ($number=6; $number>=0; $number--) {
  64. print sprintf(
  65. T_ngettext(
  66. "%d pig went to the market\n",
  67. "%d pigs went to the market\n",
  68. $number
  69. ),
  70. $number
  71. );
  72. }
  73. print "</pre>\n";
  74. ?>
  75. <hr />
  76. <p>&laquo; <a href="./">back</a></p>
  77. </body>
  78. </html>