Browser-cache localization strings (+ new API endpoint to get them)

This commit is contained in:
Bernd Bestel 2021-06-29 20:24:02 +02:00
parent d18a8d8b56
commit b856911f0f
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
8 changed files with 52 additions and 10 deletions

View File

@ -65,6 +65,7 @@
- Fixed that the number picker up/down buttons did not work when the input field was empty or contained an invalid number
- Fixed that links and embeds (e.g. YouTube videos) did not work in the text editor
### API fixes
### API improvements/fixes
- Added a new API endpoint `/system/localization-strings` to get the localization strings (gettext JSON representation; in the by the user desired language)
- Fixed that due soon products with `due_type` = "Expiration date" were missing in `due_products` of the `/stock/volatile` endpoint
- Fixed that `PUT/DELETE /objects/{entity}/{objectId}` produced an internal server error when the given object id was invalid (now returns `400 Bad Request`)

View File

@ -17,8 +17,13 @@ class BaseApiController extends BaseController
parent::__construct($container);
}
protected function ApiResponse(\Psr\Http\Message\ResponseInterface $response, $data)
protected function ApiResponse(\Psr\Http\Message\ResponseInterface $response, $data, $cache = false)
{
if ($cache)
{
$response = $response->withHeader('Cache-Control', 'max-age=2592000');
}
$response->getBody()->write(json_encode($data));
return $response;
}

View File

@ -129,7 +129,6 @@ class BaseController
$this->View->set('__n', function ($number, $singularForm, $pluralForm) use ($localizationService) {
return $localizationService->__n($number, $singularForm, $pluralForm);
});
$this->View->set('GettextPo', $localizationService->GetPoAsJsonString());
// TODO: Better handle this generically based on the current language (header in .po file?)
$dir = 'ltr';

View File

@ -85,6 +85,11 @@ class SystemApiController extends BaseApiController
}
}
public function GetLocalizationStrings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
return $this->ApiResponse($response, json_decode($this->getLocalizationService()->GetPoAsJsonString()), true);
}
public function __construct(\DI\Container $container)
{
parent::__construct($container);

View File

@ -184,6 +184,27 @@
}
}
},
"/system/localization-strings": {
"get": {
"summary": "Returns all localization strings (in the by the user desired language)",
"tags": [
"System"
],
"responses": {
"200": {
"description": "The operation was successful",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "A gettext JSON representation"
}
}
}
}
}
}
},
"/system/log-missing-localization": {
"post": {
"summary": "Logs a missing localization string",

View File

@ -226,7 +226,22 @@ Grocy.Api.DeleteFile = function(fileName, group, success, error)
xhr.send();
};
Grocy.Translator = new Translator(Grocy.GettextPo);
U = function(relativePath)
{
return Grocy.BaseUrl.replace(/\/$/, '') + relativePath;
}
Grocy.Translator = new Translator(); // Dummy, real instance is loaded async below
Grocy.Api.Get("system/localization-strings?v=" + Grocy.Version,
function(response)
{
Grocy.Translator = new Translator(response);
},
function(xhr)
{
console.error(xhr);
}
);
__t = function(text, ...placeholderValues)
{
if (Grocy.Mode === "dev")
@ -248,11 +263,6 @@ __n = function(number, singularForm, pluralForm)
return Grocy.Translator.n__(singularForm, pluralForm, number, number)
}
U = function(relativePath)
{
return Grocy.BaseUrl.replace(/\/$/, '') + relativePath;
}
if (!Grocy.ActiveNav.isEmpty())
{
var menuItem = $('#sidebarResponsive').find("[data-nav-for-page='" + Grocy.ActiveNav + "']");

View File

@ -151,6 +151,7 @@ $app->group('/api', function (RouteCollectorProxy $group) {
$group->get('/system/db-changed-time', '\Grocy\Controllers\SystemApiController:GetDbChangedTime');
$group->get('/system/config', '\Grocy\Controllers\SystemApiController:GetConfig');
$group->post('/system/log-missing-localization', '\Grocy\Controllers\SystemApiController:LogMissingLocalization');
$group->get('/system/localization-strings', '\Grocy\Controllers\SystemApiController:GetLocalizationStrings');
// Generic entity interaction
$group->get('/objects/{entity}', '\Grocy\Controllers\GenericEntityApiController:GetObjects');

View File

@ -85,6 +85,7 @@
<script>
var Grocy = { };
Grocy.Components = { };
Grocy.Version = '{{ $version }}';
Grocy.Mode = '{{ GROCY_MODE }}';
Grocy.BaseUrl = '{{ $U('/') }}';
Grocy.CurrentUrlRelative = "/" + window.location.href.split('?')[0].replace(Grocy.BaseUrl, "");
@ -93,7 +94,6 @@
Grocy.Currency = '{{ GROCY_CURRENCY }}';
Grocy.CalendarFirstDayOfWeek = '{{ GROCY_CALENDAR_FIRST_DAY_OF_WEEK }}';
Grocy.CalendarShowWeekNumbers = {{ BoolToString(GROCY_CALENDAR_SHOW_WEEK_OF_YEAR) }};
Grocy.GettextPo = {!! $GettextPo !!};
Grocy.FeatureFlags = {!! json_encode($featureFlags) !!};
Grocy.Webhooks = {
@if(GROCY_FEATURE_FLAG_LABELPRINTER && !GROCY_LABEL_PRINTER_RUN_SERVER)