Language.php
29.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Language;
defined('JPATH_PLATFORM') or die;
use Joomla\String\StringHelper;
/**
* Languages/translation handler class
*
* @since 1.7.0
*/
class Language
{
/**
* Array of Language objects
*
* @var Language[]
* @since 1.7.0
*/
protected static $languages = array();
/**
* Debug language, If true, highlights if string isn't found.
*
* @var boolean
* @since 1.7.0
*/
protected $debug = false;
/**
* The default language, used when a language file in the requested language does not exist.
*
* @var string
* @since 1.7.0
*/
protected $default = 'en-GB';
/**
* An array of orphaned text.
*
* @var array
* @since 1.7.0
*/
protected $orphans = array();
/**
* Array holding the language metadata.
*
* @var array
* @since 1.7.0
*/
protected $metadata = null;
/**
* Array holding the language locale or boolean null if none.
*
* @var array|boolean
* @since 1.7.0
*/
protected $locale = null;
/**
* The language to load.
*
* @var string
* @since 1.7.0
*/
protected $lang = null;
/**
* A nested array of language files that have been loaded
*
* @var array
* @since 1.7.0
*/
protected $paths = array();
/**
* List of language files that are in error state
*
* @var array
* @since 1.7.0
*/
protected $errorfiles = array();
/**
* Translations
*
* @var array
* @since 1.7.0
*/
protected $strings = array();
/**
* An array of used text, used during debugging.
*
* @var array
* @since 1.7.0
*/
protected $used = array();
/**
* Counter for number of loads.
*
* @var integer
* @since 1.7.0
*/
protected $counter = 0;
/**
* An array used to store overrides.
*
* @var array
* @since 1.7.0
*/
protected $override = array();
/**
* Name of the transliterator function for this language.
*
* @var string
* @since 1.7.0
*/
protected $transliterator = null;
/**
* Name of the pluralSuffixesCallback function for this language.
*
* @var callable
* @since 1.7.0
*/
protected $pluralSuffixesCallback = null;
/**
* Name of the ignoredSearchWordsCallback function for this language.
*
* @var callable
* @since 1.7.0
*/
protected $ignoredSearchWordsCallback = null;
/**
* Name of the lowerLimitSearchWordCallback function for this language.
*
* @var callable
* @since 1.7.0
*/
protected $lowerLimitSearchWordCallback = null;
/**
* Name of the upperLimitSearchWordCallback function for this language.
*
* @var callable
* @since 1.7.0
*/
protected $upperLimitSearchWordCallback = null;
/**
* Name of the searchDisplayedCharactersNumberCallback function for this language.
*
* @var callable
* @since 1.7.0
*/
protected $searchDisplayedCharactersNumberCallback = null;
/**
* Constructor activating the default information of the language.
*
* @param string $lang The language
* @param boolean $debug Indicates if language debugging is enabled.
*
* @since 1.7.0
*/
public function __construct($lang = null, $debug = false)
{
$this->strings = array();
if ($lang == null)
{
$lang = $this->default;
}
$this->lang = $lang;
$this->metadata = LanguageHelper::getMetadata($this->lang);
$this->setDebug($debug);
$this->override = $this->parse(JPATH_BASE . '/language/overrides/' . $lang . '.override.ini');
// Look for a language specific localise class
$class = str_replace('-', '_', $lang . 'Localise');
$paths = array();
if (defined('JPATH_SITE'))
{
// Note: Manual indexing to enforce load order.
$paths[0] = JPATH_SITE . "/language/overrides/$lang.localise.php";
$paths[2] = JPATH_SITE . "/language/$lang/$lang.localise.php";
}
if (defined('JPATH_ADMINISTRATOR'))
{
// Note: Manual indexing to enforce load order.
$paths[1] = JPATH_ADMINISTRATOR . "/language/overrides/$lang.localise.php";
$paths[3] = JPATH_ADMINISTRATOR . "/language/$lang/$lang.localise.php";
}
ksort($paths);
$path = reset($paths);
while (!class_exists($class) && $path)
{
if (file_exists($path))
{
require_once $path;
}
$path = next($paths);
}
if (class_exists($class))
{
/**
* Class exists. Try to find
* -a transliterate method,
* -a getPluralSuffixes method,
* -a getIgnoredSearchWords method
* -a getLowerLimitSearchWord method
* -a getUpperLimitSearchWord method
* -a getSearchDisplayCharactersNumber method
*/
if (method_exists($class, 'transliterate'))
{
$this->transliterator = array($class, 'transliterate');
}
if (method_exists($class, 'getPluralSuffixes'))
{
$this->pluralSuffixesCallback = array($class, 'getPluralSuffixes');
}
if (method_exists($class, 'getIgnoredSearchWords'))
{
$this->ignoredSearchWordsCallback = array($class, 'getIgnoredSearchWords');
}
if (method_exists($class, 'getLowerLimitSearchWord'))
{
$this->lowerLimitSearchWordCallback = array($class, 'getLowerLimitSearchWord');
}
if (method_exists($class, 'getUpperLimitSearchWord'))
{
$this->upperLimitSearchWordCallback = array($class, 'getUpperLimitSearchWord');
}
if (method_exists($class, 'getSearchDisplayedCharactersNumber'))
{
$this->searchDisplayedCharactersNumberCallback = array($class, 'getSearchDisplayedCharactersNumber');
}
}
$this->load();
}
/**
* Returns a language object.
*
* @param string $lang The language to use.
* @param boolean $debug The debug mode.
*
* @return Language The Language object.
*
* @since 1.7.0
*/
public static function getInstance($lang, $debug = false)
{
if (!isset(self::$languages[$lang . $debug]))
{
self::$languages[$lang . $debug] = new Language($lang, $debug);
}
return self::$languages[$lang . $debug];
}
/**
* Translate function, mimics the php gettext (alias _) function.
*
* The function checks if $jsSafe is true, then if $interpretBackslashes is true.
*
* @param string $string The string to translate
* @param boolean $jsSafe Make the result javascript safe
* @param boolean $interpretBackSlashes Interpret \t and \n
*
* @return string The translation of the string
*
* @since 1.7.0
*/
public function _($string, $jsSafe = false, $interpretBackSlashes = true)
{
// Detect empty string
if ($string == '')
{
return '';
}
$key = strtoupper($string);
if (isset($this->strings[$key]))
{
$string = $this->strings[$key];
// Store debug information
if ($this->debug)
{
$value = \JFactory::getApplication()->get('debug_lang_const') == 0 ? $key : $string;
$string = '**' . $value . '**';
$caller = $this->getCallerInfo();
if (!array_key_exists($key, $this->used))
{
$this->used[$key] = array();
}
$this->used[$key][] = $caller;
}
}
else
{
if ($this->debug)
{
$caller = $this->getCallerInfo();
$caller['string'] = $string;
if (!array_key_exists($key, $this->orphans))
{
$this->orphans[$key] = array();
}
$this->orphans[$key][] = $caller;
$string = '??' . $string . '??';
}
}
if ($jsSafe)
{
// Javascript filter
$string = addslashes($string);
}
elseif ($interpretBackSlashes)
{
if (strpos($string, '\\') !== false)
{
// Interpret \n and \t characters
$string = str_replace(array('\\\\', '\t', '\n'), array("\\", "\t", "\n"), $string);
}
}
return $string;
}
/**
* Transliterate function
*
* This method processes a string and replaces all accented UTF-8 characters by unaccented
* ASCII-7 "equivalents".
*
* @param string $string The string to transliterate.
*
* @return string The transliteration of the string.
*
* @since 1.7.0
*/
public function transliterate($string)
{
if ($this->transliterator !== null)
{
return call_user_func($this->transliterator, $string);
}
$string = Transliterate::utf8_latin_to_ascii($string);
$string = StringHelper::strtolower($string);
return $string;
}
/**
* Getter for transliteration function
*
* @return callable The transliterator function
*
* @since 1.7.0
*/
public function getTransliterator()
{
return $this->transliterator;
}
/**
* Set the transliteration function.
*
* @param callable $function Function name or the actual function.
*
* @return callable The previous function.
*
* @since 1.7.0
*/
public function setTransliterator($function)
{
$previous = $this->transliterator;
$this->transliterator = $function;
return $previous;
}
/**
* Returns an array of suffixes for plural rules.
*
* @param integer $count The count number the rule is for.
*
* @return array The array of suffixes.
*
* @since 1.7.0
*/
public function getPluralSuffixes($count)
{
if ($this->pluralSuffixesCallback !== null)
{
return call_user_func($this->pluralSuffixesCallback, $count);
}
else
{
return array((string) $count);
}
}
/**
* Getter for pluralSuffixesCallback function.
*
* @return callable Function name or the actual function.
*
* @since 1.7.0
*/
public function getPluralSuffixesCallback()
{
return $this->pluralSuffixesCallback;
}
/**
* Set the pluralSuffixes function.
*
* @param callable $function Function name or actual function.
*
* @return callable The previous function.
*
* @since 1.7.0
*/
public function setPluralSuffixesCallback($function)
{
$previous = $this->pluralSuffixesCallback;
$this->pluralSuffixesCallback = $function;
return $previous;
}
/**
* Returns an array of ignored search words
*
* @return array The array of ignored search words.
*
* @since 1.7.0
*/
public function getIgnoredSearchWords()
{
if ($this->ignoredSearchWordsCallback !== null)
{
return call_user_func($this->ignoredSearchWordsCallback);
}
else
{
return array();
}
}
/**
* Getter for ignoredSearchWordsCallback function.
*
* @return callable Function name or the actual function.
*
* @since 1.7.0
*/
public function getIgnoredSearchWordsCallback()
{
return $this->ignoredSearchWordsCallback;
}
/**
* Setter for the ignoredSearchWordsCallback function
*
* @param callable $function Function name or actual function.
*
* @return callable The previous function.
*
* @since 1.7.0
*/
public function setIgnoredSearchWordsCallback($function)
{
$previous = $this->ignoredSearchWordsCallback;
$this->ignoredSearchWordsCallback = $function;
return $previous;
}
/**
* Returns a lower limit integer for length of search words
*
* @return integer The lower limit integer for length of search words (3 if no value was set for a specific language).
*
* @since 1.7.0
*/
public function getLowerLimitSearchWord()
{
if ($this->lowerLimitSearchWordCallback !== null)
{
return call_user_func($this->lowerLimitSearchWordCallback);
}
else
{
return 3;
}
}
/**
* Getter for lowerLimitSearchWordCallback function
*
* @return callable Function name or the actual function.
*
* @since 1.7.0
*/
public function getLowerLimitSearchWordCallback()
{
return $this->lowerLimitSearchWordCallback;
}
/**
* Setter for the lowerLimitSearchWordCallback function.
*
* @param callable $function Function name or actual function.
*
* @return callable The previous function.
*
* @since 1.7.0
*/
public function setLowerLimitSearchWordCallback($function)
{
$previous = $this->lowerLimitSearchWordCallback;
$this->lowerLimitSearchWordCallback = $function;
return $previous;
}
/**
* Returns an upper limit integer for length of search words
*
* @return integer The upper limit integer for length of search words (200 if no value was set or if default value is < 200).
*
* @since 1.7.0
*/
public function getUpperLimitSearchWord()
{
if ($this->upperLimitSearchWordCallback !== null && call_user_func($this->upperLimitSearchWordCallback) > 200)
{
return call_user_func($this->upperLimitSearchWordCallback);
}
return 200;
}
/**
* Getter for upperLimitSearchWordCallback function
*
* @return callable Function name or the actual function.
*
* @since 1.7.0
*/
public function getUpperLimitSearchWordCallback()
{
return $this->upperLimitSearchWordCallback;
}
/**
* Setter for the upperLimitSearchWordCallback function
*
* @param callable $function Function name or the actual function.
*
* @return callable The previous function.
*
* @since 1.7.0
*/
public function setUpperLimitSearchWordCallback($function)
{
$previous = $this->upperLimitSearchWordCallback;
$this->upperLimitSearchWordCallback = $function;
return $previous;
}
/**
* Returns the number of characters displayed in search results.
*
* @return integer The number of characters displayed (200 if no value was set for a specific language).
*
* @since 1.7.0
*/
public function getSearchDisplayedCharactersNumber()
{
if ($this->searchDisplayedCharactersNumberCallback !== null)
{
return call_user_func($this->searchDisplayedCharactersNumberCallback);
}
else
{
return 200;
}
}
/**
* Getter for searchDisplayedCharactersNumberCallback function
*
* @return callable Function name or the actual function.
*
* @since 1.7.0
*/
public function getSearchDisplayedCharactersNumberCallback()
{
return $this->searchDisplayedCharactersNumberCallback;
}
/**
* Setter for the searchDisplayedCharactersNumberCallback function.
*
* @param callable $function Function name or the actual function.
*
* @return callable The previous function.
*
* @since 1.7.0
*/
public function setSearchDisplayedCharactersNumberCallback($function)
{
$previous = $this->searchDisplayedCharactersNumberCallback;
$this->searchDisplayedCharactersNumberCallback = $function;
return $previous;
}
/**
* Checks if a language exists.
*
* This is a simple, quick check for the directory that should contain language files for the given user.
*
* @param string $lang Language to check.
* @param string $basePath Optional path to check.
*
* @return boolean True if the language exists.
*
* @since 1.7.0
* @deprecated 3.7.0, use LanguageHelper::exists() instead.
*/
public static function exists($lang, $basePath = JPATH_BASE)
{
\JLog::add(__METHOD__ . '() is deprecated, use LanguageHelper::exists() instead.', \JLog::WARNING, 'deprecated');
return LanguageHelper::exists($lang, $basePath);
}
/**
* Loads a single language file and appends the results to the existing strings
*
* @param string $extension The extension for which a language file should be loaded.
* @param string $basePath The basepath to use.
* @param string $lang The language to load, default null for the current language.
* @param boolean $reload Flag that will force a language to be reloaded if set to true.
* @param boolean $default Flag that force the default language to be loaded if the current does not exist.
*
* @return boolean True if the file has successfully loaded.
*
* @since 1.7.0
*/
public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null, $reload = false, $default = true)
{
// If language is null set as the current language.
if (!$lang)
{
$lang = $this->lang;
}
// Load the default language first if we're not debugging and a non-default language is requested to be loaded
// with $default set to true
if (!$this->debug && ($lang != $this->default) && $default)
{
$this->load($extension, $basePath, $this->default, false, true);
}
$path = LanguageHelper::getLanguagePath($basePath, $lang);
$internal = $extension == 'joomla' || $extension == '';
$filename = $internal ? $lang : $lang . '.' . $extension;
$filename = "$path/$filename.ini";
if (isset($this->paths[$extension][$filename]) && !$reload)
{
// This file has already been tested for loading.
$result = $this->paths[$extension][$filename];
}
else
{
// Load the language file
$result = $this->loadLanguage($filename, $extension);
// Check whether there was a problem with loading the file
if ($result === false && $default)
{
// No strings, so either file doesn't exist or the file is invalid
$oldFilename = $filename;
// Check the standard file name
if (!$this->debug)
{
$path = LanguageHelper::getLanguagePath($basePath, $this->default);
$filename = $internal ? $this->default : $this->default . '.' . $extension;
$filename = "$path/$filename.ini";
// If the one we tried is different than the new name, try again
if ($oldFilename != $filename)
{
$result = $this->loadLanguage($filename, $extension, false);
}
}
}
}
return $result;
}
/**
* Loads a language file.
*
* This method will not note the successful loading of a file - use load() instead.
*
* @param string $fileName The name of the file.
* @param string $extension The name of the extension.
*
* @return boolean True if new strings have been added to the language
*
* @see Language::load()
* @since 1.7.0
*/
protected function loadLanguage($fileName, $extension = 'unknown')
{
$this->counter++;
$result = false;
$strings = $this->parse($fileName);
if ($strings !== array())
{
$this->strings = array_replace($this->strings, $strings, $this->override);
$result = true;
}
// Record the result of loading the extension's file.
if (!isset($this->paths[$extension]))
{
$this->paths[$extension] = array();
}
$this->paths[$extension][$fileName] = $result;
return $result;
}
/**
* Parses a language file.
*
* @param string $fileName The name of the file.
*
* @return array The array of parsed strings.
*
* @since 1.7.0
*/
protected function parse($fileName)
{
$strings = \JLanguageHelper::parseIniFile($fileName, $this->debug);
// Debug the ini file if needed.
if ($this->debug === true && file_exists($fileName))
{
$this->debugFile($fileName);
}
return $strings;
}
/**
* Debugs a language file
*
* @param string $filename Absolute path to the file to debug
*
* @return integer A count of the number of parsing errors
*
* @since 3.6.3
* @throws \InvalidArgumentException
*/
public function debugFile($filename)
{
// Make sure our file actually exists
if (!file_exists($filename))
{
throw new \InvalidArgumentException(
sprintf('Unable to locate file "%s" for debugging', $filename)
);
}
// Initialise variables for manually parsing the file for common errors.
$blacklist = array('YES', 'NO', 'NULL', 'FALSE', 'ON', 'OFF', 'NONE', 'TRUE');
$debug = $this->getDebug();
$this->debug = false;
$errors = array();
$php_errormsg = null;
// Open the file as a stream.
$file = new \SplFileObject($filename);
foreach ($file as $lineNumber => $line)
{
// Avoid BOM error as BOM is OK when using parse_ini.
if ($lineNumber == 0)
{
$line = str_replace("\xEF\xBB\xBF", '', $line);
}
$line = trim($line);
// Ignore comment lines.
if (!strlen($line) || $line['0'] == ';')
{
continue;
}
// Ignore grouping tag lines, like: [group]
if (preg_match('#^\[[^\]]*\](\s*;.*)?$#', $line))
{
continue;
}
// Remove the "_QQ_" from the equation
$line = str_replace('"_QQ_"', '', $line);
// Remove any escaped double quotes \" from the equation
$line = str_replace('\"', '', $line);
$realNumber = $lineNumber + 1;
// Check for any incorrect uses of _QQ_.
if (strpos($line, '_QQ_') !== false)
{
$errors[] = $realNumber;
continue;
}
// Check for odd number of double quotes.
if (substr_count($line, '"') % 2 != 0)
{
$errors[] = $realNumber;
continue;
}
// Check that the line passes the necessary format.
if (!preg_match('#^[A-Z][A-Z0-9_\*\-\.]*\s*=\s*".*"(\s*;.*)?$#', $line))
{
$errors[] = $realNumber;
continue;
}
// Check that the key is not in the blacklist.
$key = strtoupper(trim(substr($line, 0, strpos($line, '='))));
if (in_array($key, $blacklist))
{
$errors[] = $realNumber;
}
}
// Check if we encountered any errors.
if (count($errors))
{
$this->errorfiles[$filename] = $filename . ' : error(s) in line(s) ' . implode(', ', $errors);
}
elseif ($php_errormsg)
{
// We didn't find any errors but there's probably a parse notice.
$this->errorfiles['PHP' . $filename] = 'PHP parser errors :' . $php_errormsg;
}
$this->debug = $debug;
return count($errors);
}
/**
* Get a metadata language property.
*
* @param string $property The name of the property.
* @param mixed $default The default value.
*
* @return mixed The value of the property.
*
* @since 1.7.0
*/
public function get($property, $default = null)
{
if (isset($this->metadata[$property]))
{
return $this->metadata[$property];
}
return $default;
}
/**
* Determine who called Language or JText.
*
* @return array Caller information.
*
* @since 1.7.0
*/
protected function getCallerInfo()
{
// Try to determine the source if none was provided
if (!function_exists('debug_backtrace'))
{
return;
}
$backtrace = debug_backtrace();
$info = array();
// Search through the backtrace to our caller
$continue = true;
while ($continue && next($backtrace))
{
$step = current($backtrace);
$class = @ $step['class'];
// We're looking for something outside of language.php
if ($class != '\\Joomla\\CMS\\Language\\Language' && $class != 'JText')
{
$info['function'] = @ $step['function'];
$info['class'] = $class;
$info['step'] = prev($backtrace);
// Determine the file and name of the file
$info['file'] = @ $step['file'];
$info['line'] = @ $step['line'];
$continue = false;
}
}
return $info;
}
/**
* Getter for Name.
*
* @return string Official name element of the language.
*
* @since 1.7.0
*/
public function getName()
{
return $this->metadata['name'];
}
/**
* Get a list of language files that have been loaded.
*
* @param string $extension An optional extension name.
*
* @return array
*
* @since 1.7.0
*/
public function getPaths($extension = null)
{
if (isset($extension))
{
if (isset($this->paths[$extension]))
{
return $this->paths[$extension];
}
return;
}
else
{
return $this->paths;
}
}
/**
* Get a list of language files that are in error state.
*
* @return array
*
* @since 1.7.0
*/
public function getErrorFiles()
{
return $this->errorfiles;
}
/**
* Getter for the language tag (as defined in RFC 3066)
*
* @return string The language tag.
*
* @since 1.7.0
*/
public function getTag()
{
return $this->metadata['tag'];
}
/**
* Getter for the calendar type
*
* @return string The calendar type.
*
* @since 3.7.0
*/
public function getCalendar()
{
if (isset($this->metadata['calendar']))
{
return $this->metadata['calendar'];
}
else
{
return 'gregorian';
}
}
/**
* Get the RTL property.
*
* @return boolean True is it an RTL language.
*
* @since 1.7.0
*/
public function isRtl()
{
return (bool) $this->metadata['rtl'];
}
/**
* Set the Debug property.
*
* @param boolean $debug The debug setting.
*
* @return boolean Previous value.
*
* @since 1.7.0
*/
public function setDebug($debug)
{
$previous = $this->debug;
$this->debug = (boolean) $debug;
return $previous;
}
/**
* Get the Debug property.
*
* @return boolean True is in debug mode.
*
* @since 1.7.0
*/
public function getDebug()
{
return $this->debug;
}
/**
* Get the default language code.
*
* @return string Language code.
*
* @since 1.7.0
*/
public function getDefault()
{
return $this->default;
}
/**
* Set the default language code.
*
* @param string $lang The language code.
*
* @return string Previous value.
*
* @since 1.7.0
*/
public function setDefault($lang)
{
$previous = $this->default;
$this->default = $lang;
return $previous;
}
/**
* Get the list of orphaned strings if being tracked.
*
* @return array Orphaned text.
*
* @since 1.7.0
*/
public function getOrphans()
{
return $this->orphans;
}
/**
* Get the list of used strings.
*
* Used strings are those strings requested and found either as a string or a constant.
*
* @return array Used strings.
*
* @since 1.7.0
*/
public function getUsed()
{
return $this->used;
}
/**
* Determines is a key exists.
*
* @param string $string The key to check.
*
* @return boolean True, if the key exists.
*
* @since 1.7.0
*/
public function hasKey($string)
{
$key = strtoupper($string);
return isset($this->strings[$key]);
}
/**
* Returns an associative array holding the metadata.
*
* @param string $lang The name of the language.
*
* @return mixed If $lang exists return key/value pair with the language metadata, otherwise return NULL.
*
* @since 1.7.0
* @deprecated 3.7.0, use LanguageHelper::getMetadata() instead.
*/
public static function getMetadata($lang)
{
\JLog::add(__METHOD__ . '() is deprecated, use LanguageHelper::getMetadata() instead.', \JLog::WARNING, 'deprecated');
return LanguageHelper::getMetadata($lang);
}
/**
* Returns a list of known languages for an area
*
* @param string $basePath The basepath to use
*
* @return array key/value pair with the language file and real name.
*
* @since 1.7.0
* @deprecated 3.7.0, use LanguageHelper::getKnownLanguages() instead.
*/
public static function getKnownLanguages($basePath = JPATH_BASE)
{
\JLog::add(__METHOD__ . '() is deprecated, use LanguageHelper::getKnownLanguages() instead.', \JLog::WARNING, 'deprecated');
return LanguageHelper::getKnownLanguages($basePath);
}
/**
* Get the path to a language
*
* @param string $basePath The basepath to use.
* @param string $language The language tag.
*
* @return string language related path or null.
*
* @since 1.7.0
* @deprecated 3.7.0, use LanguageHelper::getLanguagePath() instead.
*/
public static function getLanguagePath($basePath = JPATH_BASE, $language = null)
{
\JLog::add(__METHOD__ . '() is deprecated, use LanguageHelper::getLanguagePath() instead.', \JLog::WARNING, 'deprecated');
return LanguageHelper::getLanguagePath($basePath, $language);
}
/**
* Set the language attributes to the given language.
*
* Once called, the language still needs to be loaded using Language::load().
*
* @param string $lang Language code.
*
* @return string Previous value.
*
* @since 1.7.0
* @deprecated 4.0 (CMS) - Instantiate a new Language object instead
*/
public function setLanguage($lang)
{
\JLog::add(__METHOD__ . ' is deprecated. Instantiate a new Language object instead.', \JLog::WARNING, 'deprecated');
$previous = $this->lang;
$this->lang = $lang;
$this->metadata = LanguageHelper::getMetadata($this->lang);
return $previous;
}
/**
* Get the language locale based on current language.
*
* @return array The locale according to the language.
*
* @since 1.7.0
*/
public function getLocale()
{
if (!isset($this->locale))
{
$locale = str_replace(' ', '', isset($this->metadata['locale']) ? $this->metadata['locale'] : '');
if ($locale)
{
$this->locale = explode(',', $locale);
}
else
{
$this->locale = false;
}
}
return $this->locale;
}
/**
* Get the first day of the week for this language.
*
* @return integer The first day of the week according to the language
*
* @since 1.7.0
*/
public function getFirstDay()
{
return (int) (isset($this->metadata['firstDay']) ? $this->metadata['firstDay'] : 0);
}
/**
* Get the weekends days for this language.
*
* @return string The weekend days of the week separated by a comma according to the language
*
* @since 3.2
*/
public function getWeekEnd()
{
return (isset($this->metadata['weekEnd']) && $this->metadata['weekEnd']) ? $this->metadata['weekEnd'] : '0,6';
}
/**
* Searches for language directories within a certain base dir.
*
* @param string $dir directory of files.
*
* @return array Array holding the found languages as filename => real name pairs.
*
* @since 1.7.0
* @deprecated 3.7.0, use LanguageHelper::parseLanguageFiles() instead.
*/
public static function parseLanguageFiles($dir = null)
{
\JLog::add(__METHOD__ . '() is deprecated, use LanguageHelper::parseLanguageFiles() instead.', \JLog::WARNING, 'deprecated');
return LanguageHelper::parseLanguageFiles($dir);
}
/**
* Parse XML file for language information.
*
* @param string $path Path to the XML files.
*
* @return array Array holding the found metadata as a key => value pair.
*
* @since 1.7.0
* @throws \RuntimeException
* @deprecated 3.7.0, use LanguageHelper::parseXMLLanguageFile() instead.
*/
public static function parseXMLLanguageFile($path)
{
\JLog::add(__METHOD__ . '() is deprecated, use LanguageHelper::parseXMLLanguageFile() instead.', \JLog::WARNING, 'deprecated');
return LanguageHelper::parseXMLLanguageFile($path);
}
}