Merge pull request #22227 from mlutfy/standalone
[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
8 public function run() {
9 $this->generateInstallLangs();
10 $this->generateSchemaStructure();
11 }
12
13 public function generateInstallLangs() {
14 // CRM-7161: generate install/langs.php from the languages template
15 // grep it for enabled languages and create a 'xx_YY' => 'Language name' $langs mapping
16 $matches = [];
17 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);
18 $langs = [];
19 for ($i = 0; $i < count($matches[0]); $i++) {
20 $langs[$matches[1][$i]] = $matches[2][$i];
21 }
22 file_put_contents('../install/langs.php', "<?php \$langs = " . var_export($langs, TRUE) . ";");
23 }
24
25 public function generateSchemaStructure() {
26 echo "Generating CRM_Core_I18n_SchemaStructure...\n";
27 $columns = [];
28 $indices = [];
29 $widgets = [];
30 foreach ($this->tables as $table) {
31 if ($table['localizable']) {
32 $columns[$table['name']] = [];
33 $widgets[$table['name']] = [];
34 }
35 else {
36 continue;
37 }
38 foreach ($table['fields'] as $field) {
39 $required = $field['required'] ? ' NOT NULL' : '';
40 $default = $field['default'] ? ' DEFAULT ' . $field['default'] : '';
41 $comment = $field['comment'] ? " COMMENT '" . $field['comment'] . "'" : '';
42 if ($field['localizable']) {
43 $columns[$table['name']][$field['name']] = $field['sqlType'] . $required . $default . $comment;
44 $widgets[$table['name']][$field['name']] = $field['widget'];
45 }
46 }
47 if (isset($table['index'])) {
48 foreach ($table['index'] as $index) {
49 if ($index['localizable']) {
50 $indices[$table['name']][$index['name']] = $index;
51 }
52 }
53 }
54 }
55
56 $template = new CRM_Core_CodeGen_Util_Template('php', FALSE);
57
58 $template->assign('columns', $columns);
59 $template->assign('indices', $indices);
60 $template->assign('widgets', $widgets);
61
62 $template->run('schema_structure.tpl', $this->config->phpCodePath . "/CRM/Core/I18n/SchemaStructure.php");
63 }
64
65 }