Return best loaded language code in LoadLanguageCommand. (#2232)

Closes #2227.
This commit is contained in:
Antonin Delpeuch 2019-11-27 15:35:18 +00:00 committed by GitHub
parent 391c728a16
commit cc5498a42a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -95,10 +95,12 @@ public class LoadLanguageCommand extends Command {
} }
ObjectNode translations = null; ObjectNode translations = null;
String bestLang = null;
for (int i = langs.length-1; i >= 0; i--) { for (int i = langs.length-1; i >= 0; i--) {
if (langs[i] == null) continue; if (langs[i] == null) continue;
ObjectNode json = loadLanguage(this.servlet, modname, langs[i]); ObjectNode json = loadLanguage(this.servlet, modname, langs[i]);
if (json != null) { if (json != null) {
bestLang = langs[i];
if (translations == null) { if (translations == null) {
translations = json; translations = json;
} else { } else {
@ -111,7 +113,7 @@ public class LoadLanguageCommand extends Command {
try { try {
ObjectNode node = ParsingUtilities.mapper.createObjectNode(); ObjectNode node = ParsingUtilities.mapper.createObjectNode();
node.put("dictionary", translations); node.put("dictionary", translations);
node.put("lang", new TextNode(langs[0])); node.put("lang", new TextNode(bestLang));
respondJSON(response, node); respondJSON(response, node);
} catch (IOException e) { } catch (IOException e) {
logger.error("Error writing language labels to response stream"); logger.error("Error writing language labels to response stream");

View File

@ -58,9 +58,20 @@ public class LoadLanguageCommandTests extends CommandTestBase {
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class); JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
assertTrue(response.has("dictionary")); assertTrue(response.has("dictionary"));
assertEquals(response.get("lang").asText(), "foobar"); assertEquals(response.get("lang").asText(), "en");
}
@Test
public void testLoadNoLanguage() throws JsonParseException, JsonMappingException, IOException, ServletException {
when(request.getParameter("module")).thenReturn("core");
when(request.getParameter("lang")).thenReturn("");
command.doPost(request, response);
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
assertTrue(response.has("dictionary"));
assertEquals(response.get("lang").asText(), "en");
} }
@Test @Test
public void testLanguageFallback() throws JsonParseException, JsonMappingException, IOException { public void testLanguageFallback() throws JsonParseException, JsonMappingException, IOException {