Skip to content

Commit 448affb

Browse files
author
slicewuff
committed
Add webSPELL class 'Language'
1 parent 9bd80ea commit 448affb

File tree

5 files changed

+248
-36
lines changed

5 files changed

+248
-36
lines changed

languages/de/index.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error_404": "Seite wurde nicht gefunden."
3+
}

languages/uk/index.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error_404": "Seite wurde nicht gefunden."
3+
}

phpunit.xml

+27-36
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
convertErrorsToExceptions="true"
4-
convertWarningsToExceptions="true"
5-
convertNoticesToExceptions="true"
6-
bootstrap="./vendor/autoload.php"
7-
verbose="true"
8-
colors="true">
9-
10-
<php>
11-
<ini name="error_reporting" value="-1"/>
12-
<ini name="memory_limit" value="3G"/>
13-
</php>
14-
15-
<testsuites>
16-
<testsuite name="webspell-ng-data-structures">
17-
<directory>./tests</directory>
18-
</testsuite>
19-
</testsuites>
20-
21-
<filter>
22-
<whitelist>
23-
<directory>./src/</directory>
24-
</whitelist>
25-
</filter>
26-
27-
<logging>
28-
<log type="coverage-html" target="./tests/tmp/report" lowUpperBound="35" highLowerBound="70"/>
29-
<log type="coverage-clover" target="./tests/tmp/coverage.xml"/>
30-
<log type="coverage-php" target="./tests/tmp/coverage.serialized"/>
31-
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
32-
<log type="junit" target="./tests/tmp/logfile.xml"/>
33-
<log type="testdox-html" target="./tests/tmp/testdox.html"/>
34-
<log type="testdox-text" target="./tests/tmp/testdox.txt"/>
35-
</logging>
36-
37-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" convertErrorsToExceptions="true" convertWarningsToExceptions="true" convertNoticesToExceptions="true" bootstrap="./vendor/autoload.php" verbose="true" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./src/</directory>
6+
</include>
7+
<report>
8+
<clover outputFile="./tests/tmp/coverage.xml"/>
9+
<html outputDirectory="./tests/tmp/report" lowUpperBound="35" highLowerBound="70"/>
10+
<php outputFile="./tests/tmp/coverage.serialized"/>
11+
<text outputFile="php://stdout" showUncoveredFiles="false"/>
12+
</report>
13+
</coverage>
14+
<php>
15+
<ini name="error_reporting" value="-1"/>
16+
<ini name="memory_limit" value="3G"/>
17+
</php>
18+
<testsuites>
19+
<testsuite name="webspell-ng-data-structures">
20+
<directory>./tests</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging>
24+
<junit outputFile="./tests/tmp/logfile.xml"/>
25+
<testdoxHtml outputFile="./tests/tmp/testdox.html"/>
26+
<testdoxText outputFile="./tests/tmp/testdox.txt"/>
27+
</logging>
28+
</phpunit>

src/Language.php

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
/*
3+
##########################################################################
4+
# #
5+
# Version 4 / / / #
6+
# -----------__---/__---__------__----__---/---/- #
7+
# | /| / /___) / ) (_ ` / ) /___) / / #
8+
# _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___ #
9+
# Free Content / Management System #
10+
# / #
11+
# #
12+
# #
13+
# Copyright 2005-2015 by webspell.org #
14+
# #
15+
# visit webSPELL.org, webspell.info to get webSPELL for free #
16+
# - Script runs under the GNU GENERAL PUBLIC LICENSE #
17+
# - It's NOT allowed to remove this copyright-tag #
18+
# -- http://www.fsf.org/licensing/licenses/gpl.html #
19+
# #
20+
# Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at), #
21+
# Far Development by Development Team - webspell.org #
22+
# #
23+
# visit webspell.org #
24+
# #
25+
##########################################################################
26+
*/
27+
28+
namespace webspell_ng;
29+
30+
use Noodlehaus\Config;
31+
32+
class Language
33+
{
34+
35+
/**
36+
* @var string $language
37+
*/
38+
public $language = 'en';
39+
40+
/**
41+
* @var string $language_path
42+
*/
43+
private $language_path = 'languages/';
44+
45+
/**
46+
* @var string $language_path
47+
*/
48+
private $default_language_path = __DIR__ . '/../languages/';
49+
50+
/**
51+
* @var array<string> $module
52+
*/
53+
public $module = array();
54+
55+
/**
56+
* @var array<string> $module
57+
*/
58+
private $module_array = array();
59+
60+
public function setLanguage(string $to, bool $admin=false): bool
61+
{
62+
63+
if ($admin) {
64+
$this->language_path = '../'.$this->language_path;
65+
}
66+
67+
if (in_array($to, $this->getLanguages())) {
68+
$this->language = $to;
69+
$this->language_path = 'languages/';
70+
return true;
71+
} else {
72+
return false;
73+
}
74+
75+
}
76+
77+
/**
78+
* @return array<string>
79+
*/
80+
private function getLanguages(): array
81+
{
82+
$languages = array();
83+
foreach (new \DirectoryIterator($this->default_language_path) as $fileInfo) {
84+
if ($fileInfo->isDot() === false && $fileInfo->isDir() === true) {
85+
$languages[] = $fileInfo->getFilename();
86+
}
87+
}
88+
return $languages;
89+
}
90+
91+
public function getRootPath(): string
92+
{
93+
return $this->language_path;
94+
}
95+
96+
public function readModule(string $module, bool $add=false, bool $admin=false, bool $pluginpath=false, bool $installpath=false): bool
97+
{
98+
global $default_language;
99+
100+
$module = str_replace(array('\\', '/', '.'), '', $module);
101+
102+
if ($admin && !$pluginpath) {
103+
$langFolder = '../' . $this->language_path;
104+
$folderPath = '%s%s/admin/%s.php';
105+
} else if ($admin && $pluginpath) {
106+
$langFolder = '../' . $pluginpath . $this->language_path;
107+
$folderPath = '%s%s/admin/%s.php';
108+
} else if ($pluginpath) {
109+
$langFolder = $pluginpath . $this->language_path;
110+
$folderPath = '%s%s/%s.php';
111+
} else if ($installpath) {
112+
$langFolder = '../install/' . $this->language_path;
113+
$folderPath = '%s%s/%s.php';
114+
} else if (!$admin && is_dir('../languages/')) {
115+
$langFolder = '../' . $this->language_path;
116+
$folderPath = '%s%s/%s.php';
117+
} else {
118+
$langFolder = $this->language_path;
119+
$folderPath = '%s%s/%s.php';
120+
}
121+
122+
$languageFallbackTable = array();
123+
if (!empty($this->language)) {
124+
$languageFallbackTable[] = $this->language;
125+
}
126+
if (!empty($default_language)) {
127+
$languageFallbackTable[] = $default_language;
128+
}
129+
if (!in_array('en', $languageFallbackTable)) {
130+
$languageFallbackTable[] = 'en';
131+
}
132+
133+
foreach ($languageFallbackTable as $folder) {
134+
135+
if (empty($folder)) {
136+
continue;
137+
}
138+
139+
$path = sprintf($folderPath, $langFolder, $folder, $module);
140+
if (file_exists($path)) {
141+
$module_file = $path;
142+
break;
143+
}
144+
145+
}
146+
147+
if (!isset($module_file)) {
148+
return false;
149+
}
150+
151+
$this->module_array[] = $module_file;
152+
153+
include($module_file);
154+
155+
if (!$add) {
156+
$this->module = array();
157+
}
158+
159+
foreach ($language_array as $key => $val) {
160+
$this->module[ $key ] = $val;
161+
}
162+
163+
$formvalidation = 'formvalidation';
164+
if (!in_array($formvalidation, $this->module_array) && ($module != $formvalidation)) {
165+
$this->readModule($formvalidation, true, false, false);
166+
}
167+
168+
return true;
169+
}
170+
171+
public function replace(string $template): string
172+
{
173+
foreach ($this->module as $key => $val) {
174+
$template = str_replace('%' . $key . '%', $val, $template);
175+
}
176+
return $template;
177+
}
178+
179+
/**
180+
* @return array<string>
181+
*/
182+
public function getTranslationTable(): array
183+
{
184+
$map = array();
185+
foreach ($this->module as $key => $val) {
186+
$newKey = '%' . $key . '%';
187+
$map[ $newKey ] = $val;
188+
}
189+
return $map;
190+
}
191+
192+
}

tests/LanguageTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
use webspell_ng\Language;
6+
7+
final class LanguageTest extends TestCase
8+
{
9+
10+
public function testIfLanguageCanBeLoaded(): void
11+
{
12+
13+
$language = new Language();
14+
15+
$this->assertFalse($language->setLanguage("unknown"), "Unknown language cannot be set.");
16+
$this->assertTrue($language->setLanguage("de", true), "Language can be set.");
17+
$this->assertEquals("de", $language->language, "Language is set.");
18+
19+
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)