File modules/language/LanguageCompiler.class.php

Last commit: Sun Nov 1 00:36:50 2020 +0100	Jan Dankert	Refactoring: Only using the configuration object.
1 <?php 2 3 namespace language; 4 5 use LogicException; 6 7 use util\YAML; 8 9 class LanguageCompiler 10 { 11 private $srcFile; 12 13 private $fallback = 'en'; 14 const DO_NOT_CHANGE = '/* DO NOT CHANGE THIS GENERATED FILE */'; 15 16 public function __construct() 17 { 18 $this->languageFolder = __DIR__ . '/'; 19 $this->srcFile = __DIR__ . '/language.yml'; 20 $this->listFile = __DIR__ . '/LanguageList.class.php'; 21 $this->keyFile = __DIR__ . '/Messages.class.php'; 22 } 23 24 /** 25 * Compiles language YAML source to native language php files. 26 * Only, if the YAML source file has changed. 27 */ 28 private function compile() 29 { 30 $this->updateProduction(); 31 } 32 33 34 private function getLanguageSource() 35 { 36 return YAML::parse( file_get_contents($this->srcFile) ); 37 } 38 39 40 /** 41 * Creates the production environment. 42 */ 43 public function updateProduction() 44 { 45 $lang = $this->getLanguageSource(); 46 47 $this->updateMessages($lang); 48 $this->updateLanguageFiles($lang); 49 } 50 51 /** 52 * Creates the production environment. 53 * @param $lang 54 */ 55 private function updateMessages( $lang ) 56 { 57 $success = file_put_contents($this->keyFile, '<?php namespace '.__NAMESPACE__."; ".self::DO_NOT_CHANGE."\nclass Messages {\n"); 58 if ($success) 59 ; 60 else 61 throw new LogicException("File is not writable: '$this->keyFile'\n"); 62 63 file_put_contents($this->keyFile, ' public static $AVAILABLE_LANGUAGES = ['.implode(',',array_map(function ($language) { 64 return '\''.$language.'\''; 65 },$this->getIsoList($lang))).'];'."\n",FILE_APPEND); 66 67 68 foreach ($lang as $key => $values) 69 file_put_contents($this->keyFile, ' const '.strtoupper($key).' = \''.strtoupper($key).'\';'."\n",FILE_APPEND); 70 71 file_put_contents($this->keyFile, '}',FILE_APPEND); 72 echo 'Success: Updated file '.$this->keyFile."\n"; 73 } 74 75 76 77 78 private function getIsoList( $lang ) 79 { 80 $isoList = array(); 81 82 foreach ($lang as $key => $values) { 83 foreach ($values as $isocode => $value) { 84 if (!in_array($isocode, $isoList)) 85 $isoList[] = $isocode; 86 } 87 88 } 89 90 return $isoList; 91 } 92 93 94 95 96 /** 97 * Creates the production environment. 98 * @param $lang 99 */ 100 private function updateLanguageFiles( $lang ) 101 { 102 // creating a list of all language iso codes. 103 104 foreach ($this->getIsoList($lang) as $iso) { 105 $outputFilename = $this->getOutputLanguageFile($iso); 106 107 $success = file_put_contents($outputFilename, "<?php ".self::DO_NOT_CHANGE."\n"); 108 109 if ($success) 110 ; 111 else 112 throw new LogicException("File is not writable: '$outputFilename'\n"); 113 114 file_put_contents($outputFilename, "namespace ".__NAMESPACE__.'; class '.$this->getOutputLanguageClass($iso)." {\npublic function get() { return [\n", FILE_APPEND); 115 foreach ($lang as $key => $value) { 116 if (isset($value[$iso])) 117 $t = $value[$iso]; 118 elseif (isset($value[$this->fallback])) 119 $t = $value[$this->fallback]; // Fallback language 120 else 121 echo "WARNING: ".'language key '.$key.' has no value for '.$iso.' and '.$this->fallback.'.'."\n"; 122 $t = str_replace('\'', '\\\'', $t); // escaping 123 file_put_contents($outputFilename, "'$key'=>'$t',\n", FILE_APPEND); 124 } 125 file_put_contents($outputFilename, "];}\n}", FILE_APPEND); 126 127 echo 'Success: Updated file '.$outputFilename."\n"; 128 } 129 } 130 131 /** 132 * Returns the native php language file for the selected iso code. 133 * @param $iso string ISO-Code 134 * @param null string $fallbackiso Fallback to this ISO-Code, if the file does not exist. 135 * @return string filename 136 */ 137 private function getOutputLanguageClass( $iso ) 138 { 139 return 'Language_' . strtoupper($iso); 140 } 141 142 /** 143 * Returns the native php language file for the selected iso code. 144 * @param $iso string ISO-Code 145 * @param null string $fallbackiso Fallback to this ISO-Code, if the file does not exist. 146 * @return string filename 147 */ 148 private function getOutputLanguageFile( $iso ) 149 { 150 return __DIR__.'/'.$this->getOutputLanguageClass($iso).'.class.php'; 151 } 152 153 }
Download modules/language/LanguageCompiler.class.php
History Sun, 1 Nov 2020 00:36:50 +0100 Jan Dankert Refactoring: Only using the configuration object. Sat, 24 Oct 2020 00:22:18 +0200 Jan Dankert Refactoring: Language files as classes Thu, 10 Sep 2020 17:47:47 +0200 Jan Dankert Language Compiler is generating a Messages class which contains all language keys. Applicaton code should use this constants where possible. Fri, 21 Aug 2020 00:22:13 +0200 Jan Dankert Refactoring: Collect all frontend compiler scripts in update.php. Compiling of CSS and JS was extracted to a new TemplateCompiler. JS and CSS is now collected in a new openrat.[min.][js|css].