aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/locale.php
blob: 8927af782b1ace7386622d6b50de32273e8ac86d (plain)
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
<?php namespace HashOver;

// Copyright (C) 2010-2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Locale
{
	public $setup;
	public $mode;
	public $text;

	public function __construct (Setup $setup)
	{
		$this->setup = $setup;
		$this->mode = $setup->usage['mode'];

		// Get appropriate locale file
		$locale_file_path = $this->getLocaleFile ();

		// Include the locale file
		$this->includeLocaleFile ($locale_file_path);

		// Prepare locale
		$this->prepareLocale ();
	}

	// Check for PHP locale file
	protected function getLocaleFile ()
	{
		// Locales checklist
		$locales = array ();

		// Lowercase language code
		$language = mb_strtolower ($this->setup->language);

		// Get path to locales directory
		$locales_directory = $this->setup->getAbsolutePath ('backend/locales');

		// Check if we are automatically selecting the locale
		if ($language === 'auto') {
			// If so, get system locale
			$system_locale = mb_strtolower (setlocale (LC_CTYPE, 0));

			// Split the locale into specific parts
			$locale_parts = explode ('.', $system_locale);
			$language_parts = explode ('_', $locale_parts[0]);

			// Add locale in 'en-us' format to checklist
			$full_locale = str_replace ('_', '-', $locale_parts[0]);
			$locales[] = $full_locale;

			// Add front part of locale ('en') to checklist
			$locales[] = $language_parts[0];

			// Add end part of locale ('us') to checklist
			if (!empty ($language_parts[1])) {
				$locales[] = $language_parts[1];
			}
		} else {
			// If not, add configured locale to checklist
			$locales[] = $language;
		}

		foreach ($locales as $locale) {
			// Locale file path
			$locale_file = $locales_directory . '/' . $locale . '.php';

			// Check if a locale file exists for current locale
			if (file_exists ($locale_file)) {
				// If so, return PHP locale file path
				return $locale_file;
			}
		}

		// Otherwise, default to English
		return $locales_directory . '/en.php';
	}

	protected function includeLocaleFile ($file)
	{
		// Check if the locale file can be included
		if (@include ($file)) {
			// If so, set locale to array stored in the file
			$this->text = $locale;
		} else {
			// If not, throw exception
			$language = mb_strtoupper ($this->setup->language);
			$exception = $language . ' locale file could not be included!';

			throw new \Exception ($exception);
		}
	}

	// Injects optionality into a given locale string
	public function optionality ($locale, $choice = 'optional')
	{
		// Optionality locale key (default to optional)
		$key = ($choice === 'required') ? 'required' : 'optional';

		// Optionality locale string
		$optionality = mb_strtolower ($this->text[$key]);

		// Inject optionality into locale string
		$new_locale = sprintf ($locale, $optionality);

		return $new_locale;
	}

	// Adds optionality to any given locale string
	public function optionalize ($key, $choice = 'optional')
	{
		return $this->optionality ($this->text[$key] . ' (%s)', $choice);
	}

	// Prepares locale by modifying them in various ways
	public function prepareLocale ()
	{
		// Add optionality to form field title locales
		foreach ($this->setup->fieldOptions as $field => $option) {
			// Title locale key
			$tooltip_key = $field . '-tip';

			// Title locale string
			$tooltip_locale = $this->text[$tooltip_key];

			// Inject optionality into title locale
			$optionality = $this->optionality ($tooltip_locale, $option);

			// Update the locale
			$this->text[$tooltip_key] = $optionality;
		}

		// Run through each locale string
		foreach ($this->text as $key => $value) {
			switch ($key) {
				// Inject date and time formats into date and time locale
				case 'date-time': {
					$this->text[$key] = sprintf (
						$value,
						$this->setup->dateFormat,
						$this->setup->timeFormat
					);

					break;
				}
			}
		}
	}

	// Return file permissions locale with directory and PHP user
	public function permissionsInfo ($file)
	{
		// PHP user, or www-data
		$php_user = isset ($_SERVER['USER']) ? $_SERVER['USER'] : '';
		$php_user = !empty ($php_user) ? $php_user : 'www-data';

		return sprintf (
			$this->text['permissions-info'],
			$this->setup->getHttpPath ($file),
			$php_user
		);
	}
}