translation.class.php 2.0 KB

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