PHP gettext with fallback for native not supported languages


/ Published in: PHP
Save to your folder(s)

Works, even if a locale is not avaliable on the system.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $locale = "fa";
  4.  
  5. // Define location and domain of translations
  6. $directory = realpath('./') .'/translations/LOCALES';
  7. $domain = 'php-addressbook';
  8. $default_locale = $locale;
  9. $supported_locales = array($locale);
  10.  
  11.  
  12. // Prepare "php-gettext"
  13. require_once('gettext/gettext.inc');
  14.  
  15.  
  16. // Prepare "native gettext" setup
  17. T_setlocale(LC_ALL, $locale);
  18. T_bindtextdomain($domain, $directory);
  19. T_textdomain($domain);
  20. T_bind_textdomain_codeset($domain, 'UTF-8');
  21.  
  22. echo T_gettext('ADDRESS');
  23.  
  24. if (!locale_emulation()) {
  25. print "<p>locale '$locale' is supported by your system, using native gettext implementation.</p>\n";
  26. }
  27. else {
  28. print "<p>locale '$locale' is _not_ supported on your system, using the default locale '". $default_locale ."'.</p>\n";
  29. }
  30.  
  31. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.