INFRA-132 - CRM/Core - phpcbf
[civicrm-core.git] / CRM / Core / CodeGen / I18n.php
1 <?php
2
3 /**
4 * Generate language files and classes
5 */
6 class CRM_Core_CodeGen_I18n extends CRM_Core_CodeGen_BaseTask {
7 public function run() {
8 $this->generateInstallLangs();
9 $this->generateSchemaStructure();
10 }
11
12 public function generateInstallLangs() {
13 // CRM-7161: generate install/langs.php from the languages template
14 // grep it for enabled languages and create a 'xx_YY' => 'Language name' $langs mapping
15 $matches = array();
16 preg_match_all('/, 1, \'([a-z][a-z]_[A-Z][A-Z])\', \'..\', \{localize\}\'\{ts escape="sql"\}(.+)\{\/ts\}\'\{\/localize\}, /', file_get_contents('templates/languages.tpl'), $matches);
17 $langs = array();
18 for ($i = 0; $i < count($matches[0]); $i++) {
19 $langs[$matches[1][$i]] = $matches[2][$i];
20 }
21 file_put_contents('../install/langs.php', "<?php \$langs = " . var_export($langs, TRUE) . ";");
22 }
23
24 public function generateSchemaStructure() {
25 echo "Generating CRM_Core_I18n_SchemaStructure...\n";
26 $columns = array();
27 $indices = array();
28 foreach ($this->tables as $table) {
29 if ($table['localizable']) {
30 $columns[$table['name']] = array();
31 }
32 else {
33 continue;
34 }
35 foreach ($table['fields'] as $field) {
36 if ($field['localizable']) {
37 $columns[$table['name']][$field['name']] = $field['sqlType'];
38 }
39 }
40 if (isset($table['index'])) {
41 foreach ($table['index'] as $index) {
42 if ($index['localizable']) {
43 $indices[$table['name']][$index['name']] = $index;
44 }
45 }
46 }
47 }
48
49 $template = new CRM_Core_CodeGen_Util_Template('php');
50
51 $template->assign('columns', $columns);
52 $template->assign('indices', $indices);
53
54 $template->run('schema_structure.tpl', $this->config->phpCodePath . "/CRM/Core/I18n/SchemaStructure.php");
55 }
56 }