项目作者: steelywing

项目描述 :
PHP Translator / i18n / l10n
高级语言: PHP
项目地址: git://github.com/steelywing/Translation.git
创建时间: 2017-04-07T06:11:40Z
项目社区:https://github.com/steelywing/Translation

开源协议:MIT License

下载


Translation

PHP Translator, Simple, Lightweight

Installation

Composer

Run composer require steelywing/translation

Manually

Clone this repo, run composer dump-autoload on root directory to
generate autoload.php.

Feature

  • Support CSV, and PHP Array
  • Support fallback language
  1. use SteelyWing\Translation\Dictionary\DictionaryArray;
  2. use SteelyWing\Translation\Dictionary\DictionaryCSV;
  3. use SteelyWing\Translation\Translator;
  4. $translator = new Translator();
  5. // Use array as dictionary
  6. //$dict = new DictionaryArray(include('dictionary.php'));
  7. // Use CSV as dictionary
  8. $dict = new DictionaryCSV('dictionary.csv');
  9. $translator->addDictionary($dict);
  10. $translator->setLocale('en');
  11. // Hello World!
  12. echo $translator->translate('hello world');
  13. echo "\n<br>\n";
  14. // Hello, Steely Wing!
  15. echo $translator->translate('hello', [':user' => 'Steely Wing']);
  16. echo "\n<br>\n";
  17. // 你好,Steely Wing!
  18. echo $translator->translate('hello', [':user' => 'Steely Wing'], 'zh_tw');
  19. echo "\n<br>\n";
  20. // I love programming!
  21. echo $translator->translate('i love programming');
  22. echo "\n<br>\n";
  23. // 我喜爱编程!
  24. echo $translator->translate('i love programming', [], 'zh_cn');
  25. echo "\n<br>\n";
  26. // 世界,你好!
  27. echo $translator->translate('hello world', [], 'zh_tw');
  28. echo "\n<br>\n";
  29. // Echo empty, no fallback locale set
  30. echo $translator->translate('english only', [], 'zh_tw');
  31. echo "\n<br>\n";
  32. // Return the key if key not found
  33. $translator->setFallbackToKey(true);
  34. echo $translator->translate('english only', [], 'zh_tw');
  35. echo "\n<br>\n";
  36. // Fallback to "en", echo "English Only"
  37. $translator->setFallback('en');
  38. echo $translator->translate('english only', [], 'zh_tw');
  39. echo "\n<br>\n";

Chinese conversion 繁簡轉換

  1. use SteelyWing\Translation\Dictionary\DictionaryChinese;
  2. $chinese = new DictionaryChinese($dict);
  3. $translator->addDictionary($chinese);
  4. // Auto translate "繁體轉簡體中文測試" to "繁体转简体中文測試"
  5. echo $translator->translate('simplified chinese testing', [], 'zh_cn');
  6. echo "\n<br>\n";
  7. // Auto translate "简体转繁体中文测试" to "簡體轉繁體中文測試"
  8. echo $translator->translate('traditional chinese testing', [], 'zh_tw');
  9. echo "\n<br>\n";