Skip to content

Commit b2ed52f

Browse files
committed
fix(server): permit to register new spellings for other languages than en
1 parent 3a3b2aa commit b2ed52f

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

language_tool_python/server.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ def __init__(
207207
language = get_locale_language()
208208
except ValueError:
209209
language = FAILSAFE_LANGUAGE
210+
self._language = LanguageTag(language, self._get_languages())
210211
if new_spellings:
211212
self._new_spellings = new_spellings
212213
self._register_spellings()
213-
self._language = LanguageTag(language, self._get_languages())
214214
self._mother_tongue = mother_tongue
215215
self._disabled_rules = set()
216216
self._enabled_rules = set()
@@ -736,12 +736,24 @@ def _get_valid_spelling_file_path(self) -> Path:
736736
raise PathError(err)
737737
library_path = self._local_language_tool.get_directory_path()
738738

739+
language = self._language.normalized_tag.split("-")[
740+
0
741+
].lower() # if language is "en-US", we want "en"
742+
743+
if language == "auto":
744+
# Default to English if auto is selected, as the spelling file is needed for new spellings
745+
# The new spellings will only be taken into account if the server detects the language as English
746+
logger.debug(
747+
"Language is set to 'auto'. Defaulting to 'en' for spelling file path."
748+
)
749+
language = "en"
750+
739751
spelling_file_path = (
740752
library_path
741753
/ "org"
742754
/ "languagetool"
743755
/ "resource"
744-
/ "en"
756+
/ language
745757
/ "hunspell"
746758
/ "spelling.txt"
747759
)

tests/test_server_local.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ def test_session_only_new_spellings() -> None:
124124
assert initial_checksum.hexdigest() == subsequent_checksum.hexdigest()
125125

126126

127+
def test_new_spellins_in_es() -> None:
128+
"""
129+
Test that new spellings are recognized in Spanish language.
130+
This test verifies that when new_spellings are added for the Spanish language,
131+
they are correctly recognized by the tool during grammar checking.
132+
(This test is important to ensure that the new spellings functionality works
133+
across different languages and is not limited to English.)
134+
135+
:raises AssertionError: If the new spellings are not recognized in Spanish.
136+
"""
137+
import language_tool_python
138+
139+
with language_tool_python.LanguageTool(
140+
"es", new_spellings=["ejempo"], new_spellings_persist=False
141+
) as tool:
142+
matches = tool.check("Este es un ejempo sencillo.")
143+
assert not matches
144+
145+
127146
def test_uk_typo() -> None:
128147
"""
129148
Test grammar checking and correction with UK English language rules.

0 commit comments

Comments
 (0)