joomla-platform / libraries / joomla / language / language.php

   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
<?php
/**
 * @package     Joomla.Platform
 * @subpackage  Language
 *
 * @copyright   Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

/**
 * Allows for quoting in language .ini files.
 */
define('_QQ_', '"');


// import some libraries
jimport('joomla.filesystem.stream');

/**
 * Languages/translation handler class
 *
 * @package		Joomla.Platform
 * @subpackage	Language
 * @since		11.1
 */
class JLanguage extends JObject
{
	protected static $languages = array();
	/**
	 * Debug language, If true, highlights if string isn't found
	 *
	 * @var		boolean
	 * @since	11.1
	 */
	protected $debug = false;

	/**
	 * The default language
	 *
	 * The default language is used when a language file in the requested language does not exist.
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $default	= 'en-GB';

	/**
	 * An array of orphaned text
	 *
	 * @var		array
	 * @since	11.1
	 */
	protected $orphans = array();

	/**
	 * Array holding the language metadata
	 *
	 * @var		array
	 * @since	11.1
	 */
	protected $metadata = null;

	/**
	 * Array|boolean holding the language locale
	 *
	 * @var		array|boolean
	 * @since	11.1
	 */
	protected $locale = null;

	/**
	 * The language to load
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $lang = null;

	/**
	 * List of language files that have been loaded
	 *
	 * @var		array of arrays
	 * @since	11.1
	 */
	protected $paths = array();

	/**
	 * List of language files that are in error state
	 *
	 * @var		array of string
	 * @since	11.1
	 */
	protected $errorfiles = array();

	/**
	 * Translations
	 *
	 * @var		array
	 * @since	11.1
	 */
	protected $strings = null;

	/**
	 * An array of used text, used during debugging
	 *
	 * @var		array
	 * @since	11.1
	 */
	protected $used = array();

	/**
	 * Counter for number of loads
	 *
	 * @var		integer
	 * @since	11.1
	 */
	protected $counter = 0;

	/**
	 * An array used to store overrides
	 *
	 * @var		array
	 * @since	11.1
	 */
	protected $override = array();

	/**
	 * Name of the transliterator function for this language
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $transliterator = null;

	/**
	 * Name of the pluralSuffixesCallback function for this language
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $pluralSuffixesCallback = null;

	/**
	 * Name of the ignoredSearchWordsCallback function for this language
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $ignoredSearchWordsCallback = null;

	/**
	 * Name of the lowerLimitSearchWordCallback function for this language
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $lowerLimitSearchWordCallback = null;

	/**
	 * Name of the uppperLimitSearchWordCallback function for this language
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $upperLimitSearchWordCallback = null;

	/**
	 * Name of the searchDisplayedCharactersNumberCallback function for this language
	 *
	 * @var		string
	 * @since	11.1
	 */
	protected $searchDisplayedCharactersNumberCallback = null;

	/**
	 * Constructor activating the default information of the language
	 */
	public function __construct($lang = null, $debug = false)
	{
		$this->strings = array ();

		if ($lang == null) {
			$lang = $this->default;
		}

		$this->setLanguage($lang);
		$this->setDebug($debug);

		$filename = JPATH_BASE . "/language/overrides/$lang.override.ini";
		if (file_exists($filename) && $contents = $this->parse($filename)) {
			if (is_array($contents)) {
				$this->override = $contents;
			}
			unset($contents);
		}

		// Look for a language specific localise class
		$class = str_replace('-', '_', $lang . 'Localise');
		if (!class_exists($class) && defined('JPATH_SITE')) {

			// Class does not exist. Try to find it in the Site Language Folder
			$localise = JPATH_SITE . "/language/$lang/$lang.localise.php";
			if (file_exists($localise)) {
				require_once $localise;
			}
		}
		if (!class_exists($class) && defined('JPATH_ADMINISTRATOR')) {

			// Class does not exist. Try to find it in the Administrator Language Folder
			$localise = JPATH_ADMINISTRATOR . "/language/$lang/$lang.localise.php";
			if (file_exists($localise)) {
				require_once $localise;
			}
		}
		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	JLanguage	The Language object.
	 * @since	11.1
	 */
	public static function getInstance($lang, $debug=false)
	{
		if (!isset(self::$languages[$lang.$debug])) {
			self::$languages[$lang.$debug] = new JLanguage($lang, $debug);
		}
		return self::$languages[$lang.$debug];
	}

	/**
	 * Translate function, mimics the php gettext (alias _) function
	 *
	 * @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
	 * @note	The function checks if $jsSafe is true then if $interpretBackslashes is true
	 * @since	11.1
	 */
	public function _($string, $jsSafe = false, $interpretBackSlashes = true)
	{
		$key = strtoupper($string);
		if (isset ($this->strings[$key])) {
			$string = $this->debug ? '**'.$this->strings[$key].'**' : $this->strings[$key];

			// Store debug information
			if ($this->debug) {
				$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) {
			// 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	11.1
	 */
	public function transliterate($string)
	{
		include_once dirname(__FILE__) . '/latin_transliterate.php';

		if ($this->transliterator !== null) {
			return call_user_func($this->transliterator, $string);
		}

		$string = JLanguageTransliterate::utf8_latin_to_ascii($string);
		$string = JString::strtolower($string);

		return $string;
	}

	/**
	 * Getter for transliteration function
	 *
	 * @return	string|function Function name or the actual function for PHP 5.3
	 * @since	11.1
	 */
	public function getTransliterator()
	{
		return $this->transliterator;
	}

	/**
	 * Set the transliteration function
	 *
	 * @return	string|function Function name or the actual function for PHP 5.3
	 * @since	11.1
	 */
	public function setTransliterator($function)
	{
		$previous = $this->transliterator;
		$this->transliterator = $function;
		return $previous;
	}

	/**
	 * pluralSuffixes function
	 *
	 * This method return an array of suffixes for plural rules
	 *
	 * @param	int	$count	The count number
	 * @return	array	The array of suffixes
	 * @since	11.1
	 */
	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      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 * @deprecated
	 */
	public function getPluralSufficesCallback() {
		return $this->getPluralSuffixesCallback();
	}
	/**
	 * Getter for pluralSuffixesCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 * 
	 */
	public function getPluralSuffixesCallback() {
		return $this->pluralSuffixesCallback;
	}	

	/**
	 * Set the pluralSuffixes function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function setPluralSuffixesCallback($function) {
		$previous = $this->pluralSuffixesCallback;
		$this->pluralSuffixesCallback = $function;
		return $previous;
	}

	/**
	 * getIgnoredSearchWords function
	 *
	 * This method returns an array of ignored search words
	 *
	 * @return	array	The array of ignored search words
	 * @since	11.1
	 */
	public function getIgnoredSearchWords() {
		if ($this->ignoredSearchWordsCallback !== null) {
			return call_user_func($this->ignoredSearchWordsCallback);
		}
		else {
			return array();
		}
	}

	/**
	 * Getter for ignoredSearchWordsCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function getIgnoredSearchWordsCallback() {
		return $this->ignoredSearchWordsCallback;
	}

	/**
	 * Setter for the ignoredSearchWordsCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function setIgnoredSearchWordsCallback($function) {
		$previous = $this->ignoredSearchWordsCallback;
		$this->ignoredSearchWordsCallback = $function;
		return $previous;
	}

	/**
	 * getLowerLimitSearchWord function
	 *
	 * This method 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	11.1
	 */
	public function getLowerLimitSearchWord() {
		if ($this->lowerLimitSearchWordCallback !== null) {
			return call_user_func($this->lowerLimitSearchWordCallback);
		}
		else {
			return 3;
		}
	}

	/**
	 * Getter for lowerLimitSearchWordCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function getLowerLimitSearchWordCallback() {
		return $this->lowerLimitSearchWordCallback;
	}

	/**
	 * Setter for the lowerLimitSearchWordCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function setLowerLimitSearchWordCallback($function) {
		$previous = $this->lowerLimitSearchWordCallback;
		$this->lowerLimitSearchWordCallback = $function;
		return $previous;
	}

	/**
	 * getUpperLimitSearchWord function
	 *
	 * This method returns an upper limit integer for length of search words
	 *
	 * @return	integer	The upper limit integer for length of search words (20 if no value was set for a specific language)
	 * @since	11.1
	 */
	public function getUpperLimitSearchWord() {
		if ($this->upperLimitSearchWordCallback !== null) {
			return call_user_func($this->upperLimitSearchWordCallback);
		}
		else {
			return 20;
		}
	}

	/**
	 * Getter for upperLimitSearchWordCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function getUpperLimitSearchWordCallback() {
		return $this->upperLimitSearchWordCallback;
	}

	/**
	 * Setter for the upperLimitSearchWordCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function setUpperLimitSearchWordCallback($function) {
		$previous = $this->upperLimitSearchWordCallback;
		$this->upperLimitSearchWordCallback = $function;
		return $previous;
	}

	/**
	 * getSearchDisplayedCharactersNumber function
	 *
	 * This method returns the number of characters displayed during research
	 *
	 * @return	integer	The number of characters displayed during research (200 if no value was set for a specific language)
	 * @since	11.1
	 */
	public function getSearchDisplayedCharactersNumber() {
		if ($this->searchDisplayedCharactersNumberCallback !== null) {
			return call_user_func($this->searchDisplayedCharactersNumberCallback);
		}
		else {
			return 200;
		}
	}

	/**
	 * Getter for searchDisplayedCharactersNumberCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function getSearchDisplayedCharactersNumberCallback() {
		return $this->searchDisplayedCharactersNumberCallback;
	}

	/**
	 * Setter for the searchDisplayedCharactersNumberCallback function
	 *
	 * @return      string|function Function name or the actual function for PHP 5.3
	 * @since       11.1
	 */
	public function setSearchDisplayedCharactersNumberCallback($function) {
		$previous = $this->searchDisplayedCharactersNumberCallback;
		$this->searchDisplayedCharactersNumberCallback = $function;
		return $previous;
	}

	/**
	 * Check 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	11.1
	 */
	public static function exists($lang, $basePath = JPATH_BASE)
	{
		static	$paths	= array();

		// Return false if no language was specified
		if (! $lang) {
			return false;
		}

		$path	= "$basePath/language/$lang";

		// Return previous check results if it exists
		if (isset($paths[$path]))
		{
			return $paths[$path];
		}

		// Check if the language exists
		jimport('joomla.filesystem.folder');

		$paths[$path]	= JFolder::exists($path);

		return $paths[$path];
	}

	/**
	 * 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	11.1
	 */
	public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null, $reload = false, $default = true)
	{
		if (! $lang) {
			$lang = $this->lang;
		}

		$path = self::getLanguagePath($basePath, $lang);

		$internal = $extension == 'joomla' || $extension == '';
		$filename = $internal ? $lang : $lang . '.' . $extension;
		$filename = "$path/$filename.ini";

		$result = false;
		if (isset($this->paths[$extension][$filename]) && ! $reload) {
			// Strings for this file have already been loaded.
			$result = true;
		} 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
				$path		= self::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 The name of the file
	 * @param	string The name of the extension
	 * 
	 * @return	boolean True if new strings have been added to the language
	 * @see		JLanguage::load()
	 * @since	11.1
	 */
	protected function loadLanguage($filename, $extension = 'unknown', $overwrite = true)
	{

		$this->counter++;

		$result	= false;

		$strings = false;
		if (file_exists($filename)) {
			$strings = $this->parse($filename);
		}

		if ($strings) {
			if (is_array($strings)) {
				$this->strings = array_merge($this->strings, $strings);
			}
			if (is_array($strings) && count($strings)) {
				$this->strings = array_merge($this->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	11.1
	 */
	protected function parse($filename)
	{
		$version = phpversion();

		// Capture hidden PHP errors from the parsing.
		$php_errormsg	= null;
		$track_errors	= ini_get('track_errors');
		ini_set('track_errors', true);

		if ($version >= '5.3.1') {
			$contents = file_get_contents($filename);
			$contents = str_replace('_QQ_','"\""',$contents);
			$strings = @parse_ini_string($contents);
		}
		else {
			$strings = @parse_ini_file($filename);
			if ($version == '5.3.0' && is_array($strings)) {
				foreach($strings as $key => $string) {
					$strings[$key]=str_replace('_QQ_','"',$string);
				}
			}
		}

		// Restore error tracking to what it was before.
		ini_set('track_errors',$track_errors);

		if (!is_array($strings)) {
			$strings = array();
		}

		if ($this->debug) {
			// Initialise variables for manually parsing the file for common errors.
			$blacklist	= array('YES','NO','NULL','FALSE','ON','OFF','NONE','TRUE');
			$regex		= '/^(|(\[[^\]]*\])|([A-Z][A-Z0-9_\-]*\s*=(\s*(("[^"]*")|(_QQ_)))+))\s*(;.*)?$/';
			$this->debug = false;
			$errors		= array();
			$lineNumber	= 0;

			// Open the file as a stream.
			$stream		= new JStream();
			$stream->open($filename);

			while (!$stream->eof())
			{
				$line = $stream->gets();
				$lineNumber++;

				// Check that the key is not in the blacklist and that the line format passes the regex.
				$key = strtoupper(trim(substr($line, 0, strpos($line, '='))));
				if (!preg_match($regex, $line) || in_array($key, $blacklist)) {
					$errors[] = $lineNumber;
				}
			}

			$stream->close();

			// Check if we encountered any errors.
			if (count($errors)) {
				if (basename($filename) != $this->lang.'.ini') {
					$this->errorfiles[$filename] = $filename.JText::sprintf('JERROR_PARSING_LANGUAGE_FILE', implode(', ', $errors));
				}
				else {
					$this->errorfiles[$filename] = $filename . '&#160;: error(s) in line(s) ' . implode(', ', $errors);
				}
			}
			else if ($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 = true;
		}

		return $strings;
	}

	/**
	 * 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	11.1
	 */
	public function get($property, $default = null)
	{
		if (isset ($this->metadata[$property])) {
			return $this->metadata[$property];
		}
		return $default;
	}

	/**
	 * Determine who called JLanguage or JText
	 *
	 * @return	array Caller information
	 * @since	11.1
	 */
	protected function getCallerInfo()
	{
		// Try to determine the source if none was provided
		if (!function_exists('debug_backtrace')) {
			return null;
		}

		$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 != 'JLanguage' && $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	11.1
	 */
	public function getName() {
		return $this->metadata['name'];
	}

	/**
	 * Get a list of language files that have been loaded
	 *
	 * @param	string	$extension	An option extension name
	 * 
	 * @return	array
	 * @since	11.1
	 */
	public function getPaths($extension = null)
	{
		if (isset($extension)) {
			if (isset($this->paths[$extension])) {
				return $this->paths[$extension];
			}

			return null;
		} else {
			return $this->paths;
		}
	}

	/**
	 * Get a list of language files that are in error state
	 *
	 * @return	array
	 * @since	11.1
	 */
	public function getErrorFiles()
	{
		return $this->errorfiles;
	}

	/**
	 * Getter for the language tag (as defined in RFC 3066)
	 *
	 * @return	string The language tag
	 * @since	11.1
	 */
	public function getTag() {
		return $this->metadata['tag'];
	}

	/**
	 * Get the RTL property
	 *
	 * @return	boolean True is it an RTL language
	 * @since	11.1
	 */
	public function isRTL()
	{
		return $this->metadata['rtl'];
	}

	/**
	 * Set the Debug property
	 *
	 * @return	boolean Previous value
	 * @since	11.1
	 */
	public function setDebug($debug)
	{
		$previous	= $this->debug;
		$this->debug = $debug;
		return $previous;
	}

	/**
	 * Get the Debug property
	 *
	 * @return	boolean True is in debug mode
	 * @since	11.1
	 */
	public function getDebug()
	{
		return $this->debug;
	}

	/**
	 * Get the default language code
	 *
	 * @return	string Language code
	 * @since	11.1
	 */
	public function getDefault()
	{
		return $this->default;
	}

	/**
	 * Set the default language code
	 *
	 * @return	string Previous value
	 * @since	11.1
	 */
	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	11.1
	 */
	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	11.1
	 */
	public function getUsed()
	{
		return $this->used;
	}

	/**
	 * Determines is a key exists
	 *
	 * @param	key $key	The key to check
	 * @return	boolean True, if the key exists
	 * @since	11.1
	 */
	function hasKey($string)
	{
		$key = strtoupper($string);
		return isset ($this->strings[$key]);
	}

	/**
	 * Returns a associative array holding the metadata
	 *
	 * @param	string	The name of the language
	 * @return	mixed	If $lang exists return key/value pair with the language metadata,
	 *				otherwise return NULL
	 * @since	11.1
	 */
	public static function getMetadata($lang)
	{
		$path = self::getLanguagePath(JPATH_BASE, $lang);
		$file = "$lang.xml";

		$result = null;
		if (is_file("$path/$file")) {
			$result = self::parseXMLLanguageFile("$path/$file");
		}

		return $result;
	}

	/**
	 * 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	11.1
	 */
	public static function getKnownLanguages($basePath = JPATH_BASE)
	{
		$dir = self::getLanguagePath($basePath);
		$knownLanguages = self::parseLanguageFiles($dir);

		return $knownLanguages;
	}

	/**
	 * 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	11.1
	 */
	public static function getLanguagePath($basePath = JPATH_BASE, $language = null)
	{
		$dir = "$basePath/language";
		if (!empty($language)) {
			$dir .= "/$language";
		}
		return $dir;
	}

	/**
	 * Set the language attributes to the given language
	 *
	 * Once called, the language still needs to be loaded using JLanguage::load()
	 *
	 * @param	string	$lang	Language code
	 * 
	 * @return	string	Previous value
	 * @since	11.1
	 */
	public function setLanguage($lang)
	{
		$previous			= $this->lang;
		$this->lang		= $lang;
		$this->metadata	= $this->getMetadata($this->lang);

		return $previous;
	}

	/**
	 * Get the language locale based on current language
	 *
	 * @return	array|false	The locale according to the language
	 * @since	11.1
	 */
	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	int	The first day of the week according to the language
	 * @since	11.1
	 */
	public function getFirstDay()
	{
		return (int) (isset($this->metadata['firstDay']) ? $this->metadata['firstDay'] : 0);
	}

	/**
	 * 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
	 * @deprecated use parseLanguageFiles instead
	 * @since	11.1
	 */
	public static function _parseLanguageFiles($dir = null)
	{
		return self::parseLanguageFiles($dir);
	}

	/**
	 * 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	11.1
	 */
	public static function parseLanguageFiles($dir = null)
	{
		jimport('joomla.filesystem.folder');

		$languages = array ();

		$subdirs = JFolder::folders($dir);
		foreach ($subdirs as $path) {
			$langs = self::parseXMLLanguageFiles("$dir/$path");
			$languages = array_merge($languages, $langs);
		}

		return $languages;
	}

	/**
	 * Parses XML files for language information
	 *
	 * @param	string	$dir	Directory of files
	 * @return	array	Array holding the found languages as filename => metadata array
	 * @deprecated use parseXMLLanguageFiles instead
	 * @since	11.1
	 */
	public static function _parseXMLLanguageFiles($dir = null)
	{
		return self::parseXMLLanguageFiles($dir);
	}

	/**
	 * Parses XML files for language information
	 *
	 * @param	string	$dir	Directory of files
	 * @return	array	Array holding the found languages as filename => metadata array
	 * @since	11.1
	 */
	public static function parseXMLLanguageFiles($dir = null)
	{
		if ($dir == null) {
			return null;
		}

		$languages = array ();
		jimport('joomla.filesystem.folder');
		$files = JFolder::files($dir, '^([-_A-Za-z]*)\.xml$');
		foreach ($files as $file) {
			if ($content = file_get_contents("$dir/$file")) {
				if ($metadata = self::parseXMLLanguageFile("$dir/$file")) {
					$lang = str_replace('.xml', '', $file);
					$languages[$lang] = $metadata;
				}
			}
		}
		return $languages;
	}

	/**
	 * 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
	 * @deprecated use parseXMLLanguageFile instead
	 * @since	11.1
	 */
	public static function _parseXMLLanguageFile($path)
	{
		return self::parseXMLLanguageFile($path);
	}

	/**
	 * 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	11.1
	 */
	public static function parseXMLLanguageFile($path)
	{
		// Try to load the file
		if (!$xml = JFactory::getXML($path)) {
			return null;
		}

		// Check that it's a metadata file
		if ((string)$xml->getName() != 'metafile') {
			return null;
		}

		$metadata = array();

		foreach ($xml->metadata->children() as $child) {
			$metadata[$child->getName()] = (string) $child;
		}

		return $metadata;
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.