NFC - Short array syntax - auto-convert Civi dir
[civicrm-core.git] / Civi / Core / LocalizationInitializer.php
CommitLineData
ece6501c
ML
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
ece6501c 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
ece6501c
ML
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28namespace Civi\Core;
29
30use Civi;
31use Civi\Core\Event\SystemInstallEvent;
32
33/**
34 * Class LocalizationInitializer
35 * @package Civi\Core
36 */
37class LocalizationInitializer {
38
39 /**
40 * Load the locale settings based on the installation language
41 *
42 * @param \Civi\Core\Event\SystemInstallEvent $event
43 * @throws \CRM_Core_Exception
44 */
45 public static function initialize(SystemInstallEvent $event) {
46
47 // get the current installation language
48 global $tsLocale;
49 $seedLanguage = $tsLocale;
50 if (!$seedLanguage) {
51 return;
52 }
53
54 // get the corresponding settings file if any
55 $localeDir = \CRM_Core_I18n::getResourceDir();
56 $fileName = $localeDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.default.json';
57
58 // initalization
c64f69d9 59 $settingsParams = [];
ece6501c
ML
60
61 if (file_exists($fileName)) {
62
63 // load the file and parse it
64 $json = file_get_contents($fileName);
65 $settings = json_decode($json, TRUE);
66
67 if (!empty($settings)) {
68 // get all valid settings
c64f69d9 69 $results = civicrm_api3('Setting', 'getfields', []);
ece6501c
ML
70 $validSettings = array_keys($results['values']);
71 // add valid settings to params to send to api
72 foreach ($settings as $setting => $value) {
73 if (in_array($setting, $validSettings)) {
74 $settingsParams[$setting] = $value;
75 }
76
77 }
78
79 // ensure we don't mess with multilingual
80 unset($settingsParams['languageLimit']);
81
82 // support for enabled languages (option group)
83 if (isset($settings['languagesOption']) && count($settings['languagesOption']) > 0) {
84 \CRM_Core_BAO_OptionGroup::setActiveValues('languages', $settings['languagesOption']);
85 }
86
87 // set default currency in currencies_enabled (option group)
88 if (isset($settings['defaultCurrency'])) {
c64f69d9 89 \CRM_Admin_Form_Setting_Localization::updateEnabledCurrencies([$settings['defaultCurrency']], $settings['defaultCurrency']);
ece6501c
ML
90 }
91
92 }
93
94 }
95
96 // in any case, enforce the seedLanguage as the default language
97 $settingsParams['lcMessages'] = $seedLanguage;
98
99 // apply the config
100 civicrm_api3('Setting', 'create', $settingsParams);
101
102 }
103
104}