i18n.class.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * scientia
  4. *
  5. * Copyright 2023 - 2024 Johannes Keßler
  6. *
  7. * https://www.bananas-playground.net/projekt/scientia/
  8. *
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  12. *
  13. * You should have received a copy of the
  14. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  15. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  16. */
  17. class I18n {
  18. /**
  19. * @var string The lang code
  20. */
  21. private string $_defaultLangToUse = 'en';
  22. /**
  23. * @var array The loaded lang information from the file
  24. */
  25. private array $_langData = array();
  26. /**
  27. * i18n constructor.
  28. */
  29. public function __construct() {
  30. $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.$this->_defaultLangToUse.'.ini';
  31. if(defined('FRONTEND_LANGUAGE')) {
  32. $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.FRONTEND_LANGUAGE.'.ini';
  33. if(file_exists($_langFile)) {
  34. $_langData = parse_ini_file($_langFile);
  35. if($_langData !== false) {
  36. $this->_langData = $_langData;
  37. }
  38. }
  39. }
  40. else {
  41. $_langData = parse_ini_file($_langFile);
  42. if($_langData !== false) {
  43. $this->_langData = $_langData;
  44. }
  45. }
  46. }
  47. /**
  48. * Return text for given key for currently loaded lang
  49. *
  50. * @param string $key
  51. * @return string
  52. */
  53. public function t(string $key): string {
  54. $ret = $key;
  55. if(isset($this->_langData[$key])) {
  56. $ret = $this->_langData[$key];
  57. }
  58. return $ret;
  59. }
  60. }