I18nData.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. */
  28. public function addLanguage($language) {
  29. if (array_key_exists($language, $this->data)) {
  30. throw new Exception('The selected language already exist.');
  31. }
  32. $this->data[$language] = $this->data[static::REFERENCE_LANGUAGE];
  33. }
  34. /**
  35. * Add a key in the reference language
  36. *
  37. * @param string $key
  38. * @param string $value
  39. */
  40. public function addKey($key, $value) {
  41. if (array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  42. throw new Exception('The selected key already exist.');
  43. }
  44. $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key] = $value;
  45. }
  46. /**
  47. * Duplicate a key from the reference language to all other languages
  48. *
  49. * @param string $key
  50. */
  51. public function duplicateKey($key) {
  52. if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  53. throw new Exception('The selected key does not exist.');
  54. }
  55. $value = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  56. foreach ($this->getAvailableLanguages() as $language) {
  57. if (static::REFERENCE_LANGUAGE === $language) {
  58. continue;
  59. }
  60. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  61. throw new Exception(sprintf('The selected key already exist in %s.', $language));
  62. }
  63. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  64. }
  65. }
  66. /**
  67. * Remove a key in all languages
  68. *
  69. * @param string $key
  70. */
  71. public function removeKey($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. foreach ($this->getAvailableLanguages() as $language) {
  76. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  77. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  78. }
  79. }
  80. }
  81. /**
  82. * Check if the data has changed
  83. *
  84. * @return bool
  85. */
  86. public function hasChanged() {
  87. return $this->data !== $this->originalData;
  88. }
  89. public function getLanguage($language) {
  90. return $this->data[$language];
  91. }
  92. public function getReferenceLanguage() {
  93. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  94. }
  95. /**
  96. * @param string $key
  97. * @return string
  98. */
  99. private function getFilenamePrefix($key) {
  100. return preg_replace('/\..*/', '.php', $key);
  101. }
  102. }