mirror of
https://github.com/Shadowss/TravianZ.git
synced 2026-06-28 00:24:23 +00:00
Add per-user language selection (issue #166)
The profile already had a language selector (Templates/Profile/preference.tpl)
that saved the choice into users.lang and $_SESSION['lang'], but the saved
preference was never used to actually load the language: every page loads
include("Lang/".LANG.".php") with the server-wide LANG constant, so changing
the language in the profile had no visible effect.
This wires it up:
- Introduce SERVER_LANG (the server default) and make LANG the EFFECTIVE
display language, resolved from the player's saved preference
($_SESSION['lang']) with a fallback to SERVER_LANG
(install/data/constant_format.tpl).
SECURITY: the value is sanitized to [a-z_] and the target Lang/<x>.php
file must exist, otherwise we fall back to the server default. This
prevents Local File Inclusion via include("Lang/".LANG.".php").
- Seed $_SESSION['lang'] from users.lang on login (GameEngine/Session.php,
PopulateVar), so the chosen language applies right after logging in.
- Keep the SERVER default intact when an admin saves settings: the admin
config regenerators and the "Server Settings"/config displays now use
SERVER_LANG (the server default) instead of the per-user LANG, so an
admin browsing in their own language can't accidentally overwrite the
server default (6 Admin/Mods/edit*.php + editServerSet.tpl + config.tpl).
Note: existing installs must also add SERVER_LANG + the LANG resolution to
their generated GameEngine/config.php (and the Admin/Mods/constant_format.tpl
copy) since config is generated at install time.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -59,7 +59,7 @@ $editIcon = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke=
|
||||
<tr><td><?php echo CONF_SERV_NAME ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_NAME_TOOLTIP ?></span></em></td><td><?php echo SERVER_NAME;?></td></tr>
|
||||
<tr><td><?php echo CONF_SERV_STARTED ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_STARTED_TOOLTIP ?></span></em></td><td><?php echo "Date:".START_DATE." Time:".START_TIME;?></td></tr>
|
||||
<tr><td><?php echo CONF_SERV_TIMEZONE ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_TIMEZONE_TOOLTIP ?></span></em></td><td><?php echo TIMEZONE;?></td></tr>
|
||||
<tr><td><?php echo CONF_SERV_LANG ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_LANG_TOOLTIP ?></span></em></td><td><?php if(LANG == 'en') echo "English"; elseif(LANG == 'fr') echo "French"; elseif(LANG == 'es') echo "Spain"; elseif(LANG == 'rs') echo "Serbian"; elseif(LANG == 'ro') echo "Romanian"; elseif(LANG == 'zh_tw') echo "Taiwanese"; ?></td></tr>
|
||||
<tr><td><?php echo CONF_SERV_LANG ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_LANG_TOOLTIP ?></span></em></td><td><?php if((defined('SERVER_LANG') ? SERVER_LANG : LANG) == 'en') echo "English"; elseif((defined('SERVER_LANG') ? SERVER_LANG : LANG) == 'fr') echo "French"; elseif((defined('SERVER_LANG') ? SERVER_LANG : LANG) == 'es') echo "Spain"; elseif((defined('SERVER_LANG') ? SERVER_LANG : LANG) == 'rs') echo "Serbian"; elseif((defined('SERVER_LANG') ? SERVER_LANG : LANG) == 'ro') echo "Romanian"; elseif((defined('SERVER_LANG') ? SERVER_LANG : LANG) == 'zh_tw') echo "Taiwanese"; ?></td></tr>
|
||||
<tr><td><?php echo CONF_SERV_SERVSPEED ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_SERVSPEED_TOOLTIP ?></span></em></td><td><?php echo ''.SPEED.'x';?></td></tr>
|
||||
<tr><td><?php echo CONF_SERV_TROOPSPEED ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_TROOPSPEED_TOOLTIP ?></span></em></td><td><?php echo INCREASE_SPEED;?>x</td></tr>
|
||||
<tr><td><?php echo CONF_SERV_EVASIONSPEED ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_EVASIONSPEED_TOOLTIP ?></span></em></td><td><?php echo EVASION_SPEED;?></td></tr>
|
||||
|
||||
@@ -70,12 +70,12 @@ function refresh(tz) {
|
||||
<td><?php echo CONF_SERV_LANG ?> <em class="tooltip">?<span class="classic"><?php echo CONF_SERV_LANG_TOOLTIP ?></span></em></td>
|
||||
<td>
|
||||
<select name="lang">
|
||||
<option value="en" <?php if (LANG=="en") echo "selected";?>>English</option>
|
||||
<option value="fr" <?php if (LANG=="fr") echo "selected";?>>French</option>
|
||||
<option value="es" <?php if (LANG=="es") echo "selected";?>>Spain</option>
|
||||
<option value="rs" <?php if (LANG=="rs") echo "selected";?>>Serbian</option>
|
||||
<option value="ru" <?php if (LANG=="ro") echo "selected";?>>Romanian</option>
|
||||
<option value="zh_tw" <?php if (LANG=="zh_tw") echo "selected";?>>Taiwanese</option>
|
||||
<option value="en" <?php if ((defined('SERVER_LANG') ? SERVER_LANG : LANG)=="en") echo "selected";?>>English</option>
|
||||
<option value="fr" <?php if ((defined('SERVER_LANG') ? SERVER_LANG : LANG)=="fr") echo "selected";?>>French</option>
|
||||
<option value="es" <?php if ((defined('SERVER_LANG') ? SERVER_LANG : LANG)=="es") echo "selected";?>>Spain</option>
|
||||
<option value="rs" <?php if ((defined('SERVER_LANG') ? SERVER_LANG : LANG)=="rs") echo "selected";?>>Serbian</option>
|
||||
<option value="ru" <?php if ((defined('SERVER_LANG') ? SERVER_LANG : LANG)=="ro") echo "selected";?>>Romanian</option>
|
||||
<option value="zh_tw" <?php if ((defined('SERVER_LANG') ? SERVER_LANG : LANG)=="zh_tw") echo "selected";?>>Taiwanese</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -73,7 +73,7 @@ $fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: GameEngine\con
|
||||
$text = preg_replace("'%STARTTIME%'", COMMENCE, $text);
|
||||
$text = preg_replace("'%SSTARTDATE%'", START_DATE, $text);
|
||||
$text = preg_replace("'%SSTARTTIME%'", START_TIME, $text);
|
||||
$text = preg_replace("'%LANG%'", LANG, $text);
|
||||
$text = preg_replace("'%LANG%'", (defined('SERVER_LANG') ? SERVER_LANG : LANG), $text);
|
||||
$text = preg_replace("'%SPEED%'", SPEED, $text);
|
||||
$text = preg_replace("'%MAX%'", WORLD_MAX, $text);
|
||||
$text = preg_replace("'%GP%'", $GP_ENABLE, $text);
|
||||
|
||||
@@ -72,7 +72,7 @@ $fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: GameEngine\con
|
||||
$text = preg_replace("'%STARTTIME%'", COMMENCE, $text);
|
||||
$text = preg_replace("'%SSTARTDATE%'", START_DATE, $text);
|
||||
$text = preg_replace("'%SSTARTTIME%'", START_TIME, $text);
|
||||
$text = preg_replace("'%LANG%'", LANG, $text);
|
||||
$text = preg_replace("'%LANG%'", (defined('SERVER_LANG') ? SERVER_LANG : LANG), $text);
|
||||
$text = preg_replace("'%SPEED%'", SPEED, $text);
|
||||
$text = preg_replace("'%MAX%'", WORLD_MAX, $text);
|
||||
$text = preg_replace("'%GP%'", GP_ENABLE, $text);
|
||||
|
||||
@@ -68,7 +68,7 @@ $fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: GameEngine\con
|
||||
$text = preg_replace("'%STARTTIME%'", COMMENCE, $text);
|
||||
$text = preg_replace("'%SSTARTDATE%'", START_DATE, $text);
|
||||
$text = preg_replace("'%SSTARTTIME%'", START_TIME, $text);
|
||||
$text = preg_replace("'%LANG%'", LANG, $text);
|
||||
$text = preg_replace("'%LANG%'", (defined('SERVER_LANG') ? SERVER_LANG : LANG), $text);
|
||||
$text = preg_replace("'%SPEED%'", SPEED, $text);
|
||||
$text = preg_replace("'%MAX%'", WORLD_MAX, $text);
|
||||
$text = preg_replace("'%GP%'", $GP_ENABLE, $text);
|
||||
|
||||
@@ -41,7 +41,7 @@ $fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: GameEngine\con
|
||||
$text = preg_replace("'%STARTTIME%'", COMMENCE, $text);
|
||||
$text = preg_replace("'%SSTARTDATE%'", START_DATE, $text);
|
||||
$text = preg_replace("'%SSTARTTIME%'", START_TIME, $text);
|
||||
$text = preg_replace("'%LANG%'", LANG, $text);
|
||||
$text = preg_replace("'%LANG%'", (defined('SERVER_LANG') ? SERVER_LANG : LANG), $text);
|
||||
$text = preg_replace("'%SPEED%'", SPEED, $text);
|
||||
$text = preg_replace("'%MAX%'", WORLD_MAX, $text);
|
||||
$text = preg_replace("'%GP%'", GP_ENABLE, $text);
|
||||
|
||||
@@ -74,7 +74,7 @@ $fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: GameEngine\con
|
||||
$text = preg_replace("'%STARTTIME%'", COMMENCE, $text);
|
||||
$text = preg_replace("'%SSTARTDATE%'", START_DATE, $text);
|
||||
$text = preg_replace("'%SSTARTTIME%'", START_TIME, $text);
|
||||
$text = preg_replace("'%LANG%'", LANG, $text);
|
||||
$text = preg_replace("'%LANG%'", (defined('SERVER_LANG') ? SERVER_LANG : LANG), $text);
|
||||
$text = preg_replace("'%SPEED%'", SPEED, $text);
|
||||
$text = preg_replace("'%MAX%'", WORLD_MAX, $text);
|
||||
$text = preg_replace("'%GP%'", $GP_ENABLE, $text);
|
||||
|
||||
@@ -56,7 +56,7 @@ $fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: GameEngine\con
|
||||
$text = preg_replace("'%STARTTIME%'", COMMENCE, $text);
|
||||
$text = preg_replace("'%SSTARTDATE%'", START_DATE, $text);
|
||||
$text = preg_replace("'%SSTARTTIME%'", START_TIME, $text);
|
||||
$text = preg_replace("'%LANG%'", LANG, $text);
|
||||
$text = preg_replace("'%LANG%'", (defined('SERVER_LANG') ? SERVER_LANG : LANG), $text);
|
||||
$text = preg_replace("'%SPEED%'", SPEED, $text);
|
||||
$text = preg_replace("'%MAX%'", WORLD_MAX, $text);
|
||||
$text = preg_replace("'%GP%'", GP_ENABLE, $text);
|
||||
|
||||
@@ -369,6 +369,15 @@ function __construct() {
|
||||
}
|
||||
$this->userarray = $this->userinfo = $_SESSION[$cacheKeyUser]['data'];
|
||||
|
||||
// Per-user language (issue #166): seed the session language from the
|
||||
// player's saved preference (users.lang) the first time, e.g. right
|
||||
// after login. Once set, the profile "save preferences" page keeps it
|
||||
// up to date, so we don't overwrite it here (also avoids reverting to
|
||||
// a stale value from the 30s user cache above).
|
||||
if (empty($_SESSION['lang']) && !empty($this->userarray['lang'])) {
|
||||
$_SESSION['lang'] = $this->userarray['lang'];
|
||||
}
|
||||
|
||||
$this->username = $this->userarray['username'];
|
||||
$this->uid = $_SESSION['id_user'] = $this->userarray['id'];
|
||||
|
||||
|
||||
@@ -43,8 +43,22 @@ define("START_DATE", "%SSTARTDATE%");
|
||||
define("START_TIME", "%SSTARTTIME%");
|
||||
|
||||
// ***** Language
|
||||
// Choose your server language.
|
||||
define("LANG","%LANG%");
|
||||
// SERVER_LANG is the DEFAULT language of the server (chosen at install / in
|
||||
// the admin "Server Settings"). LANG is the EFFECTIVE display language.
|
||||
//
|
||||
// Per-user language (issue #166): if the logged-in player picked a language
|
||||
// in their profile preferences (stored in users.lang and mirrored into
|
||||
// $_SESSION['lang']), LANG becomes that language; otherwise LANG falls back
|
||||
// to SERVER_LANG.
|
||||
//
|
||||
// SECURITY: LANG is used in include("Lang/".LANG.".php"), so the value is
|
||||
// strictly sanitized to [a-z_] (no path traversal) and the target file MUST
|
||||
// exist, otherwise we fall back to the server default. This prevents Local
|
||||
// File Inclusion via a crafted session value.
|
||||
define("SERVER_LANG", "%LANG%");
|
||||
if (session_status() !== PHP_SESSION_ACTIVE) { @session_start(); }
|
||||
$__user_lang = isset($_SESSION['lang']) ? preg_replace('/[^a-z_]/', '', strtolower((string) $_SESSION['lang'])) : '';
|
||||
define("LANG", ($__user_lang !== '' && is_file(__DIR__ . "/Lang/" . $__user_lang . ".php")) ? $__user_lang : SERVER_LANG);
|
||||
|
||||
// ***** Speed
|
||||
// Choose your server speed. NOTICE: Higher speed, more likely
|
||||
|
||||
Reference in New Issue
Block a user