Skip to content

Commit 3ce8da4

Browse files
"Because I clearly have better things to do than write code, added Language class to make localization slightly less painful. Still hate PHP."
1 parent 14f53c8 commit 3ce8da4

File tree

3 files changed

+222
-3
lines changed

3 files changed

+222
-3
lines changed

public_html/system/engine/init.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
$config->load('default.config');
4444

4545
// Overwrites Default Config for the application specific
46-
$config->load('config.' . $config->get('application'));
46+
$config->load($config->get('application') . 'config');
4747

4848
// Set the application
4949
$config->set('application', $config->get('application'));
@@ -56,7 +56,7 @@
5656
$registry->set('log', $log);
5757

5858
// Clear log on each load if in development mode
59-
if ($config->get('development')) {
59+
if (DEVELOPMENT) {
6060
$log->clearLog();
6161
}
6262

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
/*
3+
* Spindle CMS
4+
* Copyright (c) 2025. All rights reserved.
5+
*
6+
* This file is part of the Spindle CMS project — a lightweight, modular PHP content framework derived from OpenCart.
7+
*
8+
* @license GNU General Public License v3.0 (GPL-3.0-or-later)
9+
* @link https://github.com/RandomCoderTinker/Spindle
10+
*/
11+
12+
namespace Spindle\System\Library\Language;
13+
14+
/**
15+
* Class Language
16+
*/
17+
class Language
18+
{
19+
/**
20+
* @var string
21+
*/
22+
protected string $code;
23+
/**
24+
* @var string
25+
*/
26+
protected string $directory;
27+
/**
28+
* @var array<string, string>
29+
*/
30+
protected array $path = [];
31+
/**
32+
* @var array<string, string>
33+
*/
34+
protected array $data = [];
35+
/**
36+
* @var array<string, array<string, array<string, mixed>>>
37+
*/
38+
protected array $cache = [];
39+
40+
/**
41+
* @var array<string, string>
42+
*/
43+
protected array $replacements;
44+
45+
/**
46+
* Constructor
47+
*
48+
* @param string $code
49+
*/
50+
public function __construct (string $code)
51+
{
52+
$this->code = $code;
53+
$this->replacements = [];
54+
}
55+
56+
/**
57+
* Add Path
58+
*
59+
* @param string $namespace
60+
* @param string $directory
61+
*
62+
* @return void
63+
*/
64+
public function addPath (string $namespace, string $directory = ''): void
65+
{
66+
if (!$directory) {
67+
$this->directory = $namespace;
68+
} else {
69+
$this->path[$namespace] = $directory;
70+
}
71+
}
72+
73+
protected function autoReplace (string $text): string
74+
{
75+
return strtr($text, $this->replacements);
76+
}
77+
78+
/**
79+
*
80+
* Set language text string
81+
*
82+
* @param string $key
83+
* @param string $value
84+
*
85+
* @return void
86+
*/
87+
public function set (string $key, string $value): void
88+
{
89+
$this->data[$key] = $value;
90+
$this->data[$key] = $this->autoReplace($value);
91+
}
92+
93+
/**
94+
* All
95+
*
96+
* @param string $prefix
97+
*
98+
* @return array<string, string>
99+
*/
100+
public function all (string $prefix = ''): array
101+
{
102+
if (!$prefix) {
103+
return $this->data;
104+
}
105+
106+
$_ = [];
107+
108+
$length = strlen($prefix);
109+
110+
foreach ($this->data as $key => $value) {
111+
if (substr($key, 0, $length) == $prefix) {
112+
$_[substr($key, $length + 1)] = $value;
113+
}
114+
}
115+
116+
return $_;
117+
}
118+
119+
/**
120+
* Clear
121+
*
122+
* @return void
123+
*/
124+
public function clear (): void
125+
{
126+
$this->data = [];
127+
}
128+
129+
/**
130+
* Load
131+
*
132+
* @param string $filename
133+
* @param string $prefix
134+
* @param string $code Language code
135+
*
136+
* @return array<string, string>
137+
*/
138+
public function load (string $filename, string $prefix = '', string $code = ''): array
139+
{
140+
if (!$code) {
141+
$code = $this->code;
142+
}
143+
144+
if (!isset($this->cache[$code][$filename])) {
145+
$_ = [];
146+
147+
// Load selected language file to overwrite the default language keys
148+
$file = $this->directory . $code . '/' . $filename . '.php';
149+
150+
$namespace = '';
151+
152+
$parts = explode('/', $filename);
153+
154+
foreach ($parts as $part) {
155+
if (!$namespace) {
156+
$namespace .= $part;
157+
} else {
158+
$namespace .= '/' . $part;
159+
}
160+
161+
if (isset($this->path[$namespace])) {
162+
$file = $this->path[$namespace] . $code . substr($filename, strlen($namespace)) . '.php';
163+
}
164+
}
165+
166+
if (is_file($file)) {
167+
require($file);
168+
}
169+
170+
$this->cache[$code][$filename] = $_;
171+
} else {
172+
$_ = $this->cache[$code][$filename];
173+
}
174+
175+
if ($prefix) {
176+
foreach ($_ as $key => $value) {
177+
$_[$prefix . '_' . $key] = $value;
178+
179+
unset($_[$key]);
180+
}
181+
}
182+
183+
foreach ($_ as $key => $val) {
184+
$_[$key] = $this->autoReplace($val);
185+
}
186+
187+
$this->data = array_merge($this->data, $_);
188+
189+
return $this->data;
190+
}
191+
192+
/**
193+
* Get
194+
*
195+
* Get language text string
196+
*
197+
* @param string $key
198+
*/
199+
public function get (string $key): string
200+
{
201+
return $this->data[$key] ?? $key;
202+
}
203+
204+
/**
205+
* formatText
206+
*
207+
* Formats a text string by replacing placeholders with provided arguments.
208+
*
209+
* @param string $text The text string to format.
210+
* @param mixed ...$args The arguments to replace the placeholders with.
211+
*
212+
* @return string The formatted text string.
213+
*/
214+
public function formatText (string $text, ...$args): string
215+
{
216+
return vsprintf($text, $args);
217+
}
218+
219+
}

public_html/system/Library/loggers/log.php renamed to public_html/system/library/loggers/log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @link https://github.com/RandomCoderTinker/Spindle
1010
*/
1111

12-
namespace Spindle\system\library\loggers;
12+
namespace Spindle\System\Library\Loggers;
1313

1414
/**
1515
* Class Log

0 commit comments

Comments
 (0)