Add profile language options

This commit is contained in:
Jay Trees 2022-03-25 10:30:38 +01:00
parent 25024e2548
commit 0a3a6aded6
6 changed files with 51 additions and 25 deletions

View file

@ -8,6 +8,7 @@
define('VERSION', '0.4.0');
define('ROOT', __DIR__);
define('DEFAULT_LOCALE', 'en');
/**
* Include
@ -72,11 +73,19 @@ setcookie(
$session['httponly']
);
/**
* User
*/
if ($options) {
$user = new wishthis\User();
}
/**
* Language
*/
\Locale::setDefault(DEFAULT_LOCALE);
/** Determine Locale */
$userLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locales = array_filter(
array_map(
function ($value) {
@ -87,7 +96,7 @@ $locales = array_filter(
scandir(ROOT . '/translations')
)
);
$locale = \Locale::lookup($locales, $userLocale, false, 'en');
$locale = \Locale::lookup($locales, $user->locale, false, DEFAULT_LOCALE);
/** Load Translation */
$translationFilepath = ROOT . '/translations/' . $locale . '.po';
@ -117,13 +126,6 @@ if (!$options || !$options->getOption('isInstalled')) {
$page = 'install';
}
/**
* User
*/
if ($options) {
$user = new wishthis\User();
}
/**
* Update
*

View file

@ -20,4 +20,6 @@ $(function() {
},
}
});
$('.ui.dropdown').dropdown();
});

View file

@ -100,7 +100,7 @@ class Page
/**
* Non-Static
*/
public string $language = 'en';
public string $language = DEFAULT_LOCALE;
public array $messages = array();
/**

View file

@ -23,6 +23,8 @@ class User
/**
* Non-Static
*/
public int $power = 0;
public function __construct(int $id = -1)
{
if (-1 === $id) {
@ -33,6 +35,8 @@ class User
$this->id = $id;
}
$this->locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!isset($this->id)) {
return null;
}

View file

@ -133,7 +133,8 @@ switch ($step) {
`password_reset_valid_until` DATETIME NOT NULL DEFAULT NOW(),
`last_login` DATETIME NOT NULL DEFAULT NOW(),
`power` INT NOT NULL DEFAULT 0,
`birthdate` DATE NULL DEFAULT NULL
`birthdate` DATE NULL DEFAULT NULL,
`locale` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '"
);');
$database->query('CREATE INDEX `idx_password` ON `users` (`password`);');

View file

@ -31,6 +31,8 @@ if (isset($_POST['user-id'])) {
$set[] = '`birthdate` = NULL';
}
$set[] = '`locale` = "' . $_POST['user-locale'] . '"';
$database
->query('UPDATE `users`
SET ' . implode(',', $set) . '
@ -43,10 +45,11 @@ if (isset($_POST['user-id'])) {
if ($loginRequired) {
session_destroy();
}
header('Location: /?page=profile');
die();
}
}
$page = new Page(__FILE__, __('Profile'));
$page->header();
@ -82,6 +85,7 @@ $page->navigation();
</div>
</div>
<div class="two fields">
<div class="field">
<label><?= __('Birthdate') ?></label>
@ -97,6 +101,19 @@ $page->navigation();
</div>
</div>
<div class="field">
<label><?= __('Language') ?></label>
<select class="ui search dropdown" name="user-locale">
<option value="<?= DEFAULT_LOCALE ?>"><?= \Locale::getDisplayName(DEFAULT_LOCALE, $user->locale) ?></option>
<?php foreach ($locales as $locale) { ?>
<option value="<?= $locale ?>"><?= \Locale::getDisplayName($locale, $user->locale) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="ui error message"></div>
<input class="ui primary button"