PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/I18n/Utility.php

https://github.com/christianjul/FLOW3-Composer
PHP | 142 lines | 61 code | 18 blank | 63 comment | 17 complexity | 9ccba6c1bfff2eb540f5661b76bcbba6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\I18n;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * The Utility class for locale specific actions
  15. *
  16. * @FLOW3\Scope("singleton")
  17. */
  18. class Utility {
  19. /**
  20. * A pattern which matches HTTP Accept-Language Headers
  21. */
  22. const PATTERN_MATCH_ACCEPTLANGUAGE = '/([a-z]{1,8}(-[a-z]{1,8})?|\*)(;q=(1|0(\.[0-9]+)?))?/';
  23. /**
  24. * Parses Accept-Language header and returns array of locale tags (like:
  25. * en-GB, en), or FALSE if no tags were found.
  26. *
  27. * This method only returns tags that conforms ISO 639 for language codes
  28. * and ISO 3166 for region codes. HTTP spec (RFC 2616) defines both of these
  29. * parts as 1*8ALPHA, but this method ignores tags with longer (or shorter)
  30. * codes than defined in ISO mentioned above.
  31. *
  32. * There can be an asterisk "*" in the returned array, which means that
  33. * any language is acceptable.
  34. *
  35. * Warning: This method expects that locale tags are placed in descending
  36. * order by quality in the $header string. I'm not sure if it's always true
  37. * with the web browsers.
  38. *
  39. * @param string $acceptLanguageHeader
  40. * @return mixed The array of locale identifiers or FALSE
  41. */
  42. static public function parseAcceptLanguageHeader($acceptLanguageHeader) {
  43. $acceptLanguageHeader = str_replace(' ', '', $acceptLanguageHeader);
  44. $matchingLanguages = array();
  45. if (preg_match_all(self::PATTERN_MATCH_ACCEPTLANGUAGE, $acceptLanguageHeader, $matches, \PREG_PATTERN_ORDER) !== FALSE) {
  46. foreach ($matches[1] as $localeIdentifier) {
  47. if ($localeIdentifier === '*') {
  48. $matchingLanguages[] = $localeIdentifier;
  49. continue;
  50. }
  51. if (strpos($localeIdentifier, '-') !== FALSE) {
  52. list($language, $region) = explode('-', $localeIdentifier);
  53. } else {
  54. $language = $localeIdentifier;
  55. $region = NULL;
  56. }
  57. if (strlen($language) >= 2 && strlen($language) <= 3) {
  58. if ($region === NULL || strlen($region) >= 2 && strlen($region) <= 3) {
  59. // Note: there are 3 chars in the region code only if they are all digits, but we don't check it above
  60. $matchingLanguages[] = $localeIdentifier;
  61. }
  62. }
  63. }
  64. if (count($matchingLanguages) > 0) {
  65. return $matchingLanguages;
  66. }
  67. }
  68. return FALSE;
  69. }
  70. /**
  71. * Extracts a locale tag (identifier) from the filename given.
  72. *
  73. * Locale tag should be placed just before the extension of the file. For
  74. * example, filename bar.png can be localized as bar.en_GB.png,
  75. * and this method extracts en_GB from the name.
  76. *
  77. * Note: this ignores matches on rss, xml and php and validates the identifier.
  78. *
  79. * @param string $filename Filename to extract locale identifier from
  80. * @return mixed The string with extracted locale identifier of FALSE on failure
  81. */
  82. static public function extractLocaleTagFromFilename($filename) {
  83. if (strpos($filename, '.') === FALSE) {
  84. return FALSE;
  85. }
  86. $filenameParts = explode('.', $filename);
  87. if (in_array($filenameParts[count($filenameParts) - 2], array('php', 'rss', 'xml'))) {
  88. return FALSE;
  89. } elseif (count($filenameParts) === 2 && preg_match(Locale::PATTERN_MATCH_LOCALEIDENTIFIER, $filenameParts[0]) === 1) {
  90. return $filenameParts[0];
  91. } elseif (preg_match(Locale::PATTERN_MATCH_LOCALEIDENTIFIER, $filenameParts[count($filenameParts) - 2]) === 1) {
  92. return $filenameParts[count($filenameParts) - 2];
  93. } else {
  94. return FALSE;
  95. }
  96. }
  97. /**
  98. * Checks if $haystack string begins with $needle string.
  99. *
  100. * @param string $haystack
  101. * @param string $needle
  102. * @return boolean TRUE if $haystack begins with $needle
  103. */
  104. static public function stringBeginsWith($haystack, $needle) {
  105. if (!empty($needle) && strncmp($haystack, $needle, strlen($needle)) === 0) {
  106. return TRUE;
  107. }
  108. return FALSE;
  109. }
  110. /**
  111. * Checks if $haystack string ends with $needle string.
  112. *
  113. * @param string $haystack
  114. * @param string $needle
  115. * @return boolean TRUE if $haystack ends with $needle
  116. */
  117. static public function stringEndsWith($haystack, $needle) {
  118. if (substr($haystack, - strlen($needle)) === $needle) {
  119. return TRUE;
  120. }
  121. return FALSE;
  122. }
  123. }
  124. ?>