2024.8.23
Loading...
Searching...
No Matches
class.langproc.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class LangProc
12 *
13 * This class handles the language processing for the Bearsampp application.
14 * It loads language files, retrieves language settings, and provides language-specific values.
15 */
17{
18 /**
19 * @var string The current language being used.
20 */
21 private $current;
22
23 /**
24 * @var array The raw language data loaded from the language file.
25 */
26 private $raw;
27
28 /**
29 * LangProc constructor.
30 *
31 * Initializes the LangProc object and loads the language settings.
32 */
33 public function __construct()
34 {
35 $this->load();
36 }
37
38 /**
39 * Loads the current language settings and data.
40 *
41 * This method retrieves the default language from the configuration,
42 * checks if it is available, and then loads the corresponding language file.
43 */
44 public function load()
45 {
47 $this->raw = null;
48
49 $this->current = $bearsamppConfig->getDefaultLang();
50 if (!empty($this->current) && in_array($this->current, $this->getList())) {
51 $this->current = $bearsamppConfig->getLang();
52 }
53
54 $this->raw = parse_ini_file($bearsamppCore->getLangsPath() . '/' . $this->current . '.lang');
55 }
56
57 /**
58 * Gets the current language being used.
59 *
60 * @return string The current language.
61 */
62 public function getCurrent()
63 {
64 return $this->current;
65 }
66
67 /**
68 * Retrieves the list of available languages.
69 *
70 * This method scans the language directory and returns a list of available language files.
71 *
72 * @return array The list of available languages.
73 */
74 public function getList()
75 {
76 global $bearsamppCore;
77 $result = array();
78
79 $handle = @opendir($bearsamppCore->getLangsPath());
80 if (!$handle) {
81 return $result;
82 }
83
84 while (false !== ($file = readdir($handle))) {
85 if ($file != "." && $file != ".." && Util::endWith($file, '.lang')) {
86 $result[] = str_replace('.lang', '', $file);
87 }
88 }
89
90 closedir($handle);
91 return $result;
92 }
93
94 /**
95 * Retrieves the value for a given language key.
96 *
97 * This method returns the value associated with the specified key in the current language.
98 * If the key is not found, it logs an error and returns the key itself.
99 *
100 * @param string $key The language key to retrieve the value for.
101 * @return string The value associated with the key, or the key itself if not found.
102 */
103 public function getValue($key)
104 {
105 global $bearsamppRoot;
106
107 if (!isset($this->raw[$key])) {
108 $content = '[' . date('Y-m-d H:i:s', time()) . '] ';
109 $content .= 'ERROR: Lang var missing ' . $key;
110 $content .= ' for ' . $this->current . ' language.' . PHP_EOL;
111 file_put_contents($bearsamppRoot->getErrorLogFilePath(), $content, FILE_APPEND);
112 return $key;
113 }
114
115 // Special chars not handled by Aestan Tray Menu
116 $replace = array("ő", "Ő", "ű", "Ű");
117 $with = array("o", "O", "u", "U");
118
119 return str_replace($replace, $with, $this->raw[$key]);
120 }
121}
$result
global $bearsamppRoot
global $bearsamppCore
static endWith($string, $search)
global $bearsamppConfig
Definition homepage.php:26