i18n.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2024 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. class I18n {
  21. /**
  22. * @var string The iso 3 lang code
  23. */
  24. private string $_iso3 = 'eng';
  25. /**
  26. * @var string The iso 2 lang code
  27. */
  28. private string $_iso2 = 'en';
  29. /**
  30. * @var array The loaded lang information from the file
  31. */
  32. private array $_langData = array();
  33. /**
  34. * @var array The fallback lang data.
  35. */
  36. private array $_langDataFallback = array();
  37. /**
  38. * Translation constructor.
  39. */
  40. public function __construct() {
  41. // load fallback
  42. $this->_langDataFallback = parse_ini_file(PATH_ABSOLUTE.'/i18n/eng.ini');
  43. if(!$this->_langDataFallback) {
  44. Summoner::sysLog('[ERROR] Missing fallback language file!');
  45. }
  46. if(defined('FRONTEND_LANGUAGE')) {
  47. $this->_iso3 = FRONTEND_LANGUAGE['iso3'];
  48. $this->_iso2 = FRONTEND_LANGUAGE['iso2'];
  49. $_langFile = PATH_ABSOLUTE.'/i18n/'.$this->_iso3.'.ini';
  50. if(file_exists($_langFile)) {
  51. $_langData = parse_ini_file($_langFile);
  52. if($_langData !== false) {
  53. $this->_langData = $_langData;
  54. }
  55. }
  56. }
  57. else {
  58. $this->_langData = $this->_langDataFallback;
  59. }
  60. }
  61. public function twoCharLang(): string {
  62. return $this->_iso2;
  63. }
  64. /**
  65. * Return text for given key for currently loaded lang
  66. * uses vsprintf or sprintf for placeholders in key
  67. *
  68. * @param string $key
  69. * @param mixed $replace
  70. * @return string
  71. */
  72. public function t(string $key, mixed $replace=''): string {
  73. $ret = $key;
  74. $_langWorkWith = $this->_langData;
  75. if(isset($this->_langData[$key])) {
  76. $ret = $this->_langData[$key];
  77. } elseif(!DEBUG && isset($this->_langDataFallback[$key])) {
  78. $ret = $this->_langDataFallback[$key];
  79. $_langWorkWith = $this->_langDataFallback;
  80. }
  81. // the value is another key
  82. // the parse_ini_file interpolation with ${} does not work with existing values from the file itself
  83. if(str_starts_with($ret, "reuse.")) {
  84. $_ret = str_replace("reuse.","",$ret);
  85. if(isset($_langWorkWith[$_ret])) {
  86. $ret = $_langWorkWith[$_ret];
  87. }
  88. }
  89. if(!empty($replace)) {
  90. if(is_array($replace)) {
  91. $ret = vsprintf($ret, $replace);
  92. } else {
  93. $ret = sprintf($ret, $replace);
  94. }
  95. }
  96. return $ret;
  97. }
  98. }