Skip to content

Commit

Permalink
Move lookup for translation path to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Oct 21, 2024
1 parent 4a247f5 commit 68c87c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions include/I18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class I18nClass {
I18nClass();
void init(Scheduler& scheduler);
std::list<LanguageInfo_t> getAvailableLanguages();
String getFilenameByLocale(String& locale) const;

private:
void readLangPacks();
Expand Down
13 changes: 13 additions & 0 deletions src/I18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ std::list<LanguageInfo_t> I18nClass::getAvailableLanguages()
return _availLanguages;
}

String I18nClass::getFilenameByLocale(String& locale) const
{
auto it = std::find_if(_availLanguages.begin(), _availLanguages.end(), [locale](const LanguageInfo_t& elem) {
return elem.code == locale;
});

if (it != _availLanguages.end()) {
return it->filename;
} else {
return String();
}
}

void I18nClass::readLangPacks()
{
auto root = LittleFS.open("/");
Expand Down
11 changes: 4 additions & 7 deletions src/WebApi_i18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ void WebApiI18nClass::onI18nLanguage(AsyncWebServerRequest* request)
if (request->hasParam("code")) {
String code = request->getParam("code")->value();

const auto& languages = I18n.getAvailableLanguages();
auto it = std::find_if(languages.begin(), languages.end(), [code](const LanguageInfo_t& elem) {
return elem.code == code;
});
String filename = I18n.getFilenameByLocale(code);

if (it != languages.end()) {
String md5 = Utils::generateMd5FromFile(it->filename);
if (filename != "") {
String md5 = Utils::generateMd5FromFile(filename);

String expectedEtag;
expectedEtag = "\"";
Expand All @@ -64,7 +61,7 @@ void WebApiI18nClass::onI18nLanguage(AsyncWebServerRequest* request)
if (eTagMatch) {
response = request->beginResponse(304);
} else {
response = request->beginResponse(LittleFS, it->filename, asyncsrv::T_application_json);
response = request->beginResponse(LittleFS, filename, asyncsrv::T_application_json);
}

// HTTP requires cache headers in 200 and 304 to be identical
Expand Down

0 comments on commit 68c87c9

Please sign in to comment.