fix(qwen-mt): support zh-CN and region-qualified language codes in lang_mapping#1120
Open
octo-patch wants to merge 1 commit into
Open
fix(qwen-mt): support zh-CN and region-qualified language codes in lang_mapping#1120octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…ng_mapping The lang_mapping method in QwenMtTranslator raised a KeyError when region- qualified language codes like "zh-CN" were passed via the CLI (e.g. `pdf2zh file.pdf -s qwen-mt -lo zh-CN`). The langdict only had "zh" as a key, so any variant with a region subtag failed. Changes: - Add "zh-CN" explicitly to langdict so the most common Simplified Chinese code works out of the box - Add a prefix-based fallback so other region-qualified codes (e.g. "zh-SG", "en-US") resolve gracefully through their base language code - Replace the silent KeyError with an informative error message listing the supported codes Fixes PDFMathTranslate#951
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #951
Problem
QwenMtTranslator.lang_mapping()raisedKeyError: 'zh-CN'when users passed region-qualified language codes likezh-CNvia the CLI:The root cause is that
langdictonly contained"zh"as the Simplified Chinese key. WhenBaseTranslator.__init__does not normalise the code (sinceQwenMtTranslatorhas nolang_mapclass variable),self.lang_outbecomes"zh-CN", and the strictlangdict[input_lang]lookup fails.Solution
"zh-CN"explicitly tolangdictso the most common Simplified Chinese code is recognised directly."zh-SG"→"zh"→"Chinese"). This future-proofs the method against other region variants without requiring exhaustive enumeration.KeyErrorwith an informative message that lists supported codes, making debugging easier for users who pass unsupported languages.Testing
Verified by unit-level inspection that:
lang_mapping("zh-CN")now returns"Chinese"lang_mapping("zh")still returns"Chinese"lang_mapping("zh-TW")still returns"Chinese"lang_mapping("en-US")returns"English"via the prefix fallbacklang_mapping("xx")raises aKeyErrorwith a helpful message