Game server and admin dashboard for TerranQuest.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Strings.lib.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. /**
  8. * Provides translated language strings.
  9. */
  10. class Strings {
  11. private $language = "en";
  12. private $strings = [];
  13. public function __construct($language = "en") {
  14. if (!preg_match("/[a-zA-Z\_\-]+/", $language)) {
  15. throw new Exception("Invalid language code $language");
  16. }
  17. $this->load("en");
  18. if ($language == "en") {
  19. return;
  20. }
  21. if (file_exists(__DIR__ . "/../langs/$language/")) {
  22. $this->language = $language;
  23. $this->load($language);
  24. } else {
  25. trigger_error("Language $language could not be found.", E_USER_WARNING);
  26. }
  27. }
  28. /**
  29. * Load all JSON files for the specified language.
  30. * @param string $language
  31. */
  32. private function load(string $language) {
  33. $files = glob(__DIR__ . "/../langs/$language/*.json");
  34. foreach ($files as $file) {
  35. $strings = json_decode(file_get_contents($file), true);
  36. foreach ($strings as $key => $val) {
  37. if (array_key_exists($key, $this->strings)) {
  38. trigger_error("Language key \"$key\" is defined more than once.", E_USER_WARNING);
  39. }
  40. $this->strings[$key] = $val;
  41. }
  42. }
  43. }
  44. /**
  45. * Add language strings dynamically.
  46. * @param array $strings ["key" => "value", ...]
  47. */
  48. public function addStrings(array $strings) {
  49. foreach ($strings as $key => $val) {
  50. $this->strings[$key] = $val;
  51. }
  52. }
  53. /**
  54. * I18N string getter. If the key isn't found, it outputs the key itself.
  55. * @param string $key
  56. * @param bool $echo True to echo the result, false to return it. Default is true.
  57. * @return string
  58. */
  59. public function get(string $key, bool $echo = true): string {
  60. $str = $key;
  61. if (array_key_exists($key, $this->strings)) {
  62. $str = $this->strings[$key];
  63. } else {
  64. trigger_error("Language key \"$key\" does not exist in " . $this->language, E_USER_WARNING);
  65. }
  66. if ($echo) {
  67. echo $str;
  68. }
  69. return $str;
  70. }
  71. /**
  72. * I18N string getter (with builder). If the key doesn't exist, outputs the key itself.
  73. * @param string $key
  74. * @param array $replace key-value array of replacements.
  75. * If the string value is "hello {abc}" and you give ["abc" => "123"], the
  76. * result will be "hello 123".
  77. * @param bool $echo True to echo the result, false to return it. Default is true.
  78. * @return string
  79. */
  80. public function build(string $key, array $replace, bool $echo = true): string {
  81. $str = $key;
  82. if (array_key_exists($key, $this->strings)) {
  83. $str = $this->strings[$key];
  84. } else {
  85. trigger_error("Language key \"$key\" does not exist in " . $this->language, E_USER_WARNING);
  86. }
  87. foreach ($replace as $find => $repl) {
  88. $str = str_replace("{" . $find . "}", $repl, $str);
  89. }
  90. if ($echo) {
  91. echo $str;
  92. }
  93. return $str;
  94. }
  95. /**
  96. * Builds and returns a JSON key:value string for the supplied array of keys.
  97. * @param array $keys ["key1", "key2", ...]
  98. */
  99. public function getJSON(array $keys): string {
  100. $strings = [];
  101. foreach ($keys as $k) {
  102. $strings[$k] = $this->get($k, false);
  103. }
  104. return json_encode($strings);
  105. }
  106. }