Skip to content

Commit

Permalink
improve LibreOffice 7.4 integration by extending the output of the /l…
Browse files Browse the repository at this point in the history
…anguages endpoint and by also accepting codes like 'fr-FR' (#7421)
  • Loading branch information
danielnaber committed Nov 13, 2022
1 parent c78efb4 commit 0b500f7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
44 changes: 38 additions & 6 deletions languagetool-core/src/main/java/org/languagetool/Languages.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.languagetool;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.languagetool.noop.NoopLanguage;
import org.languagetool.tools.MultiKeyProperties;
Expand Down Expand Up @@ -217,23 +218,38 @@ public static Language getLanguageForShortCode(String langCode) {
*/
public static Language getLanguageForShortCode(String langCode, List<String> noopLanguageCodes) {
Language language = getLanguageForShortCodeOrNull(langCode);
if (language == null) {
// e.g. 'fr-FR' requested (happens with LibreOffice 7.4):
language = Languages.getLongCodeToLangMapping().get(langCode);
}
if (language == null) {
if (noopLanguageCodes.contains(langCode)) {
return NOOP_LANGUAGE;
} else {
List<String> codes = new ArrayList<>();
for (Language realLanguage : getStaticAndDynamicLanguages()) {
codes.add(realLanguage.getShortCodeWithCountryAndVariant());
}
Collections.sort(codes);
throw new IllegalArgumentException("'" + langCode + "' is not a language code known to LanguageTool." +
" Supported language codes are: " + String.join(", ", codes) + ". The list of languages is read from " + PROPERTIES_PATH +
" Supported language codes are: " + String.join(", ", getLangCodes()) + ". The list of languages is read from " + PROPERTIES_PATH +
" in the Java classpath. See https://dev.languagetool.org/java-api for details.");
}
}
return language;
}

@NotNull
private static List<String> getLangCodes() {
List<String> codes = new ArrayList<>();
for (Language realLanguage : getStaticAndDynamicLanguages()) {
codes.add(realLanguage.getShortCodeWithCountryAndVariant());
}
Map<String, Language> longCodeToLang = getLongCodeToLangMapping();
for (Map.Entry<String, Language> entry : longCodeToLang.entrySet()) {
if (!codes.contains(entry.getKey())) {
codes.add(entry.getKey());
}
}
Collections.sort(codes);
return codes;
}

/**
* Return whether a language with the given language code is supported. Which languages
* are supported depends on the classpath when the {@code Language} object is initialized.
Expand Down Expand Up @@ -270,6 +286,22 @@ public static Language getLanguageForLocale(Locale locale) {
throw new RuntimeException("No appropriate language found, not even en-US. Supported languages: " + get());
}

/**
* <b>For internal use only.</b>
* Returns a mapping from {@code fr-FR} to its language etc. Used to support requests
* from LibreOffice 7.4, which sends these language codes.
*/
public static Map<String,Language> getLongCodeToLangMapping() {
Map<String,Language> map = new HashMap<>();
List<Language> languages = Languages.get();
for (Language language : languages) {
if (language.getCountries().length > 0 && !language.getCountries()[0].isEmpty()) {
map.put(language.getShortCode() + "-" + language.getCountries()[0], language);
}
}
return map;
}

@Nullable
private static Language getLanguageForShortCodeOrNull(String langCode) {
StringTools.assureSet(langCode, "langCode");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,26 @@ String getLanguages() throws IOException {
try (JsonGenerator g = factory.createGenerator(sw)) {
g.writeStartArray();
List<Language> languages = new ArrayList<>(Languages.get());
languages.sort(Comparator.comparing(Language::getName));
Set<String> longCodes = new HashSet<>();
for (Language lang : languages) {
g.writeStartObject();
g.writeStringField("name", lang.getName());
g.writeStringField("code", lang.getShortCode());
g.writeStringField("longCode", lang.getShortCodeWithCountryAndVariant());
longCodes.add(lang.getShortCodeWithCountryAndVariant());
g.writeEndObject();
}
// add mappings like "fr-FR -> fr" because LibreOffice 7.4 needs them:
Map<String, Language> codeMap = Languages.getLongCodeToLangMapping();
for (Map.Entry<String, Language> entry : codeMap.entrySet()) {
if (!longCodes.contains(entry.getKey())) {
g.writeStartObject();
g.writeStringField("name", entry.getValue().getName());
g.writeStringField("code", entry.getValue().getShortCode());
g.writeStringField("longCode", entry.getKey());
g.writeEndObject();
}
}
g.writeEndArray();
}
return sw.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void testLanguages() throws IOException {
assertTrue(json.contains("\"German (Germany)\""));
assertTrue(json.contains("\"de\""));
assertTrue(json.contains("\"de-DE\""));
assertTrue(StringUtils.countMatches(json, "\"name\"") >= 43);
assertTrue(json.contains("\"fr\""));
assertTrue(json.contains("\"fr-FR\""));
assertTrue(StringUtils.countMatches(json, "\"name\"") >= 55);
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions languagetool-standalone/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

...

#### HTTP API / LT server
* The `/languages` endpoint now lists language codes like `fr-FR` and `es-ES` for languages
that actually don't have a variant (e.g. there is no `fr-CA`). These codes can also be used
for the `language` parameter when sending a request. `fr-FR` will internally be mapped
to `fr` etc. (https://github.com/languagetool-org/languagetool/issues/7421)

### General
* The `--api` parameter for the command-line version has been removed. It had
long been deprecated and replaced by `--json`.
Expand Down

0 comments on commit 0b500f7

Please sign in to comment.