I18nData.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. class I18nData {
  3. const REFERENCE_LANGUAGE = 'en';
  4. private $data = array();
  5. private $originalData = array();
  6. public function __construct($data) {
  7. $this->data = $data;
  8. $this->originalData = $data;
  9. }
  10. public function getData() {
  11. return $this->data;
  12. }
  13. /**
  14. * Return the available languages
  15. *
  16. * @return array
  17. */
  18. public function getAvailableLanguages() {
  19. $languages = array_keys($this->data);
  20. sort($languages);
  21. return $languages;
  22. }
  23. /**
  24. * Add a new language. It's a copy of the reference language.
  25. *
  26. * @param string $language
  27. * @throws Exception
  28. */
  29. public function addLanguage($language) {
  30. if (array_key_exists($language, $this->data)) {
  31. throw new Exception('The selected language already exist.');
  32. }
  33. $this->data[$language] = $this->data[static::REFERENCE_LANGUAGE];
  34. }
  35. /**
  36. * Add a key in the reference language
  37. *
  38. * @param string $key
  39. * @param string $value
  40. * @throws Exception
  41. */
  42. public function addKey($key, $value) {
  43. if (array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  44. throw new Exception('The selected key already exist.');
  45. }
  46. $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key] = $value;
  47. }
  48. /**
  49. * Add a value for a key for the selected language.
  50. *
  51. * @param string $key
  52. * @param string $value
  53. * @param string $language
  54. * @throws Exception
  55. */
  56. public function addValue($key, $value, $language) {
  57. if (!in_array($language, $this->getAvailableLanguages())) {
  58. throw new Exception('The selected language does not exist.');
  59. }
  60. if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  61. throw new Exception('The selected key does not exist for the selected language.');
  62. }
  63. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  64. }
  65. /**
  66. * Duplicate a key from the reference language to all other languages
  67. *
  68. * @param string $key
  69. * @throws Exception
  70. */
  71. public function duplicateKey($key) {
  72. if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  73. throw new Exception('The selected key does not exist.');
  74. }
  75. $value = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  76. foreach ($this->getAvailableLanguages() as $language) {
  77. if (static::REFERENCE_LANGUAGE === $language) {
  78. continue;
  79. }
  80. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  81. continue;
  82. }
  83. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  84. }
  85. }
  86. /**
  87. * Remove a key in all languages
  88. *
  89. * @param string $key
  90. * @throws Exception
  91. */
  92. public function removeKey($key) {
  93. if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  94. throw new Exception('The selected key does not exist.');
  95. }
  96. foreach ($this->getAvailableLanguages() as $language) {
  97. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  98. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  99. }
  100. }
  101. }
  102. /**
  103. * Check if the data has changed
  104. *
  105. * @return bool
  106. */
  107. public function hasChanged() {
  108. return $this->data !== $this->originalData;
  109. }
  110. public function getLanguage($language) {
  111. return $this->data[$language];
  112. }
  113. public function getReferenceLanguage() {
  114. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  115. }
  116. /**
  117. * @param string $key
  118. * @return string
  119. */
  120. private function getFilenamePrefix($key) {
  121. return preg_replace('/\..*/', '.php', $key);
  122. }
  123. }