An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Strings.lib.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 (file_exists(__DIR__ . "/../langs/$language/")) {
  19. $this->language = $language;
  20. $this->load($language);
  21. } else {
  22. trigger_error("Language $language could not be found.", E_USER_WARNING);
  23. }
  24. }
  25. /**
  26. * Load all JSON files for the specified language.
  27. * @param string $language
  28. */
  29. private function load(string $language) {
  30. $files = glob(__DIR__ . "/../langs/$language/*.json");
  31. foreach ($files as $file) {
  32. $strings = json_decode(file_get_contents($file), true);
  33. foreach ($strings as $key => $val) {
  34. if (array_key_exists($key, $this->strings)) {
  35. trigger_error("Language key \"$key\" is defined more than once.", E_USER_WARNING);
  36. }
  37. $this->strings[$key] = $val;
  38. }
  39. }
  40. }
  41. /**
  42. * Add language strings dynamically.
  43. * @param array $strings ["key" => "value", ...]
  44. */
  45. public function addStrings(array $strings) {
  46. foreach ($strings as $key => $val) {
  47. $this->strings[$key] = $val;
  48. }
  49. }
  50. /**
  51. * I18N string getter. If the key isn't found, it outputs the key itself.
  52. * @param string $key
  53. * @param bool $echo True to echo the result, false to return it. Default is true.
  54. * @return string
  55. */
  56. public function get(string $key, bool $echo = true): string {
  57. $str = $key;
  58. if (array_key_exists($key, $this->strings)) {
  59. $str = $this->strings[$key];
  60. } else {
  61. trigger_error("Language key \"$key\" does not exist in " . $this->language, E_USER_WARNING);
  62. }
  63. if ($echo) {
  64. echo $str;
  65. }
  66. return $str;
  67. }
  68. /**
  69. * I18N string getter (with builder). If the key doesn't exist, outputs the key itself.
  70. * @param string $key
  71. * @param array $replace key-value array of replacements.
  72. * If the string value is "hello {abc}" and you give ["abc" => "123"], the
  73. * result will be "hello 123".
  74. * @param bool $echo True to echo the result, false to return it. Default is true.
  75. * @return string
  76. */
  77. public function build(string $key, array $replace, bool $echo = true): string {
  78. $str = $key;
  79. if (array_key_exists($key, $this->strings)) {
  80. $str = $this->strings[$key];
  81. } else {
  82. trigger_error("Language key \"$key\" does not exist in " . $this->language, E_USER_WARNING);
  83. }
  84. foreach ($replace as $find => $repl) {
  85. $str = str_replace("{" . $find . "}", $repl, $str);
  86. }
  87. if ($echo) {
  88. echo $str;
  89. }
  90. return $str;
  91. }
  92. /**
  93. * Builds and returns a JSON key:value string for the supplied array of keys.
  94. * @param array $keys ["key1", "key2", ...]
  95. */
  96. public function getJSON(array $keys): string {
  97. $strings = [];
  98. foreach ($keys as $k) {
  99. $strings[$k] = $this->get($k, false);
  100. }
  101. return json_encode($strings);
  102. }
  103. }