i18n.class.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * scientia
  4. *
  5. * Copyright 2023 - 2024 Johannes Keßler
  6. *
  7. * https://www.bananas-playground.net/projekt/scientia/
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  21. */
  22. class I18n {
  23. /**
  24. * @var string The lang code
  25. */
  26. private string $_defaultLangToUse = 'en';
  27. /**
  28. * @var array The loaded lang information from the file
  29. */
  30. private array $_langData = array();
  31. /**
  32. * i18n constructor.
  33. */
  34. public function __construct() {
  35. $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.$this->_defaultLangToUse.'.ini';
  36. if(defined('FRONTEND_LANGUAGE')) {
  37. $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.FRONTEND_LANGUAGE.'.ini';
  38. if(file_exists($_langFile)) {
  39. $_langData = parse_ini_file($_langFile);
  40. if($_langData !== false) {
  41. $this->_langData = $_langData;
  42. }
  43. }
  44. }
  45. else {
  46. $_langData = parse_ini_file($_langFile);
  47. if($_langData !== false) {
  48. $this->_langData = $_langData;
  49. }
  50. }
  51. }
  52. /**
  53. * Return text for given key for currently loaded lang
  54. *
  55. * @param string $key
  56. * @return string
  57. */
  58. public function t(string $key): string {
  59. $ret = $key;
  60. if(isset($this->_langData[$key])) {
  61. $ret = $this->_langData[$key];
  62. }
  63. return $ret;
  64. }
  65. }