PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Translate/Validate/CoreTranslations.php

https://github.com/CodeYellowBV/piwik
PHP | 101 lines | 58 code | 15 blank | 28 comment | 10 complexity | df75f7ea0a2e120122505f169ecb576f MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Translate\Validate;
  10. use Piwik\Common;
  11. /**
  12. */
  13. class CoreTranslations extends ValidateAbstract
  14. {
  15. /**
  16. * Error States
  17. */
  18. const ERRORSTATE_MINIMUMTRANSLATIONS = 'At least 250 translations required';
  19. const ERRORSTATE_LOCALEREQUIRED = 'Locale required';
  20. const ERRORSTATE_TRANSLATORINFOREQUIRED = 'Translator info required';
  21. const ERRORSTATE_TRANSLATOREMAILREQUIRED = 'Translator email required';
  22. const ERRORSTATE_LAYOUTDIRECTIONINVALID = 'Layout direction must be rtl or ltr';
  23. const ERRORSTATE_LOCALEINVALID = 'Locale is invalid';
  24. const ERRORSTATE_LOCALEINVALIDLANGUAGE = 'Locale is invalid - invalid language code';
  25. const ERRORSTATE_LOCALEINVALIDCOUNTRY = 'Locale is invalid - invalid country code';
  26. protected $baseTranslations = array();
  27. /**
  28. * Sets base translations
  29. *
  30. * @param array $baseTranslations
  31. */
  32. public function __construct($baseTranslations = array())
  33. {
  34. $this->baseTranslations = $baseTranslations;
  35. }
  36. /**
  37. * Validates the given translations
  38. * * There need to be more than 250 translations presen
  39. * * Locale, TranslatorName and TranslatorEmail needs to be set in plugin General
  40. * * LayoutDirection needs to be ltr or rtl if present
  41. * * Locale must be valid (format, language & country)
  42. *
  43. * @param array $translations
  44. *
  45. * @return boolean
  46. */
  47. public function isValid($translations)
  48. {
  49. $this->message = null;
  50. if (250 > count($translations, COUNT_RECURSIVE)) {
  51. $this->message = self::ERRORSTATE_MINIMUMTRANSLATIONS;
  52. return false;
  53. }
  54. if (empty($translations['General']['Locale'])) {
  55. $this->message = self::ERRORSTATE_LOCALEREQUIRED;
  56. return false;
  57. }
  58. if (empty($translations['General']['TranslatorName'])) {
  59. $this->message = self::ERRORSTATE_TRANSLATORINFOREQUIRED;
  60. return false;
  61. }
  62. if (empty($translations['General']['TranslatorEmail'])) {
  63. $this->message = self::ERRORSTATE_TRANSLATOREMAILREQUIRED;
  64. return false;
  65. }
  66. if (!empty($translations['General']['LayoutDirection']) &&
  67. !in_array($translations['General']['LayoutDirection'], array('ltr', 'rtl'))
  68. ) {
  69. $this->message = self::ERRORSTATE_LAYOUTDIRECTIONINVALID;
  70. return false;
  71. }
  72. $allLanguages = Common::getLanguagesList();
  73. $allCountries = Common::getCountriesList();
  74. if (!preg_match('/^([a-z]{2})_([A-Z]{2})\.UTF-8$/', $translations['General']['Locale'], $matches)) {
  75. $this->message = self::ERRORSTATE_LOCALEINVALID;
  76. return false;
  77. } else if (!array_key_exists($matches[1], $allLanguages)) {
  78. $this->message = self::ERRORSTATE_LOCALEINVALIDLANGUAGE;
  79. return false;
  80. } else if (!array_key_exists(strtolower($matches[2]), $allCountries)) {
  81. $this->message = self::ERRORSTATE_LOCALEINVALIDCOUNTRY;
  82. return false;
  83. }
  84. return true;
  85. }
  86. }