ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
PhpCode.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Gettext\Extractors;
4 
7 
11 class PhpCode extends Extractor implements ExtractorInterface
12 {
13  public static $functions = array(
14  'gettext' => '__',
15  '__' => '__',
16  '__e' => '__',
17  'ngettext' => 'n__',
18  'n__' => 'n__',
19  'n__e' => 'n__',
20  'pgettext' => 'p__',
21  'p__' => 'p__',
22  'p__e' => 'p__',
23  'dgettext' => 'd__',
24  'd__' => 'd__',
25  'd__e' => 'd__',
26  'dpgettext' => 'dp__',
27  'dp__' => 'dp__',
28  'dp__e' => 'dp__',
29  'npgettext' => 'np__',
30  'np__' => 'np__',
31  'np__e' => 'np__',
32  'dnpgettext' => 'dnp__',
33  'dnp__' => 'dnp__',
34  'dnp__e' => 'dnp__',
35  );
36 
45  public static $extractComments = false;
46 
50  public static function fromString($string, Translations $translations = null, $file = '')
51  {
52  if ($translations === null) {
53  $translations = new Translations();
54  }
55 
56  $functions = new PhpFunctionsScanner($string);
57  if (self::$extractComments !== false) {
58  $functions->enableCommentsExtraction(self::$extractComments);
59  }
60  $functions->saveGettextFunctions(self::$functions, $translations, $file);
61 
62  return $translations;
63  }
64 
72  public static function convertString($value)
73  {
74  if (strpos($value, '\\') === false) {
75  return substr($value, 1, -1);
76  }
77 
78  if ($value[0] === "'") {
79  return strtr(substr($value, 1, -1), array('\\\\' => '\\', '\\\'' => '\''));
80  }
81 
82  $value = substr($value, 1, -1);
83 
84  return preg_replace_callback('/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/', function ($match) {
85  switch ($match[1][0]) {
86  case 'n':
87  return "\n";
88  case 'r':
89  return "\r";
90  case 't':
91  return "\t";
92  case 'v':
93  return "\v";
94  case 'e':
95  return "\e";
96  case 'f':
97  return "\f";
98  case '$':
99  return '$';
100  case '"':
101  return '"';
102  case '\\':
103  return '\\';
104  case 'x':
105  return chr(hexdec(substr($match[0], 1)));
106  case 'u':
107  return self::unicodeChar(hexdec(substr($match[0], 1)));
108  default:
109  return chr(octdec($match[0]));
110  }
111  }, $value);
112  }
113 
114  //http://php.net/manual/en/function.chr.php#118804
115  private static function unicodeChar($dec)
116  {
117  if ($dec < 0x80) {
118  return chr($dec);
119  }
120 
121  if ($dec < 0x0800) {
122  return chr(0xC0 + ($dec >> 6))
123  .chr(0x80 + ($dec & 0x3f));
124  }
125 
126  if ($dec < 0x010000) {
127  return chr(0xE0 + ($dec >> 12))
128  .chr(0x80 + (($dec >> 6) & 0x3f))
129  .chr(0x80 + ($dec & 0x3f));
130  }
131 
132  if ($dec < 0x200000) {
133  return chr(0xF0 + ($dec >> 18))
134  .chr(0x80 + (($dec >> 12) & 0x3f))
135  .chr(0x80 + (($dec >> 6) & 0x3f))
136  .chr(0x80 + ($dec & 0x3f));
137  }
138  }
139 }
static fromString($string, Translations $translations=null, $file='')
{Parses a string and append the translations found in the Translations instance.The file path to inse...
Definition: PhpCode.php:50
Class to get gettext strings from php files returning arrays.
Definition: PhpCode.php:11
static convertString($value)
Decodes a T_CONSTANT_ENCAPSED_STRING string.
Definition: PhpCode.php:72
static unicodeChar($dec)
Definition: PhpCode.php:115
Class to manage a collection of translations.
Create styles array
The data for the language used.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file