Skip to content

Commit

Permalink
Fix for storing language
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Aug 15, 2024
1 parent b4bfcd3 commit 83a06bf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/Core/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public function getMiddleware(): array
return self::dispatch_filter('http_middleware', [
Middleware\TrustProxies::class,
Middleware\InitialHeaders::class,
Middleware\StartSession::class,
Middleware\Installed::class,
Middleware\Updated::class,
Middleware\StartSession::class,
Middleware\RequestRateLimiter::class,
$this->app->make(IncomingRequest::class) instanceof ApiRequest
? Middleware\ApiAuth::class
Expand Down
4 changes: 4 additions & 0 deletions app/Core/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,9 @@ public function fullUrl()
return $this->getFullUrl();
}

public function isApiOrCronRequest(): bool
{
return str_starts_with($_SERVER['REQUEST_URI'], "/api") || str_starts_with($_SERVER['REQUEST_URI'], "/cron");
}

}
35 changes: 26 additions & 9 deletions app/Core/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,12 @@ public function __construct(
//Get list of available languages
$this->langlist = $this->getLanguageList();

//Get language that was set in middleware
$language = session("usersettings.language") ?? $this->config->language;
$lang = $this->getCurrentLanguage();
$this->readIni();

//Start checking if the user has a language set
if (!$this->setLanguage($language)) {
$this->setLanguage("en-US");
}
}


/**
* Set the language for the application.
*
Expand All @@ -120,15 +117,18 @@ public function setLanguage(string $lang): bool

session(["usersettings.language" => $lang]);

if (! isset($_COOKIE['language']) || $_COOKIE['language'] !== $lang) {
if ((!isset($_COOKIE['language']) || $_COOKIE['language'] !== $lang) && !request()->isApiOrCronRequest()) {

$isAPIRequest = request()->isApiOrCronRequest();

Events::add_filter_listener(
'leantime.core.httpkernel.handle.beforeSendResponse',
fn ($response) => tap($response, fn (Response $response) => $response->headers->setCookie(
Cookie::create('language')
->withValue($lang)
->withExpires(time() + 60 * 60 * 24 * 30)
->withPath(Str::finish($this->config->appDir, '/'))
->withSameSite('Strict')
->withSameSite('Lax')
))
);
}
Expand All @@ -145,6 +145,23 @@ public function setLanguage(string $lang): bool
*/
public function getCurrentLanguage(): string
{

if (session()->has("usersettings.language")) {
$this->language = session("usersettings.language");
return $this->language;
}

if (isset($_COOKIE['language'])) {
$this->language = $_COOKIE['language'];
return $this->language;
}

if(session("companysettings.language")){
$this->language = session("companysettings.language");
return $this->language;
}

$this->language = $this->config->language;
return $this->language;
}

Expand All @@ -169,7 +186,7 @@ public function isValidLanguage(string $langCode): bool
public function readIni(): array
{
if (Cache::store('installation')->has('cache.language_resources_' . $this->language) && $this->config->debug == 0) {

$this->ini_array = self::dispatch_filter(
'language_resources',
Cache::store('installation')->get('cache.language_resources_' . $this->language),
Expand Down
9 changes: 0 additions & 9 deletions app/Domain/Setting/Repositories/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public function __construct(DbCore $db)
$this->db = $db;
}

/**
* @param $type
* @return false|mixed
*/
/**
* @param $type
* @return false|mixed
Expand Down Expand Up @@ -67,11 +63,6 @@ public function getSetting($type): mixed
return false;
}

/**
* @param $type
* @param $value
* @return bool
*/
/**
* @param $type
* @param $value
Expand Down
4 changes: 4 additions & 0 deletions logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

0 comments on commit 83a06bf

Please sign in to comment.