instructure/canvas-lms · warning · Translation::SameLanguageTranslationError
Translation::SameLanguageTranslationError
Error message
Translation::SameLanguageTranslationError
What it means
After a Cedar translation completes, Translation#check_same_language compares the detected source language with the requested target language and raises Translation::SameLanguageTranslationError when they are identical — translating text to the language it is already in is rejected as a no-op. Stats are incremented under translation.errors before raising.
Solutions
- Compare the detected source language to tgt_lang before requesting translation and skip when equal
- In UIs, disable the translate action when the target language matches the content language
- Rescue Translation::SameLanguageTranslationError and return the original text untranslated
- Normalize language codes (region variants) consistently on both sides before comparison
Example fix
# before translated = Translation.translate_text(text: msg, tgt_lang: 'en', options: opts) # after begin translated = Translation.translate_text(text: msg, tgt_lang: tgt, options: opts) rescue Translation::SameLanguageTranslationError translated = msg # already in target language end
Defensive patterns
Strategy: try-catch
Validate before calling
# only call when target differs from known/detected source language next if detected_source_language.to_s == tgt_lang.to_s
Try / catch
begin translated = Translation.translate_text(text: text, tgt_lang: tgt_lang, options: opts) rescue Translation::SameLanguageTranslationError translated = text end
Prevention
- Detect content language before offering translation
- Disable the translate action for matching locales
- Normalize language-code formats on both sides
- Return original content as a graceful fallback
When it happens
Trigger: Calling Translation.translate_text or translate_html where Cedar's detected source_language equals tgt_lang — e.g. translating an English message with tgt_lang:'en', or user-selected target language matching auto-detected content language (including exact string equality like 'pt-BR' vs 'pt-BR'; a variant mismatch such as 'en' vs 'en-US' would not trigger).
Common situations: Users asking to 'translate' a message already in their language; auto-translate features detecting language after the UI already committed a target; seeded/default tgt_lang matching the content locale; case or code-format mismatches that resolve to equal language IDs.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SameLanguageTranslationError
- TextTooLongError
- UnsupportedLanguageError
- Cedar rate limit exceeded for #
- e.message
AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15).
Data as JSON: /api/errors/80d9c149d69431cb.
Report an issue: GitHub.
Appendix: source
Thrown at lib/translation.rb:123
content: html_string,
target_language: tgt_lang,
feature_slug:,
root_account_uuid:,
current_user:
)
check_same_language(translation_response.source_language, tgt_lang)
collect_translation_stats(src_lang: translation_response.source_language, tgt_lang:, type: feature_slug)
translation_response.translation
end
end
private
def check_same_language(source, target)
if source == target
InstStatsd::Statsd.distributed_increment("translation.errors", tags: ["error:same_language"])
raise Translation::SameLanguageTranslationError
end
end
def collect_translation_stats(src_lang:, tgt_lang:, type:)
tags = %W[type:#{type} source_language:#{src_lang} dest_language:#{tgt_lang}]
InstStatsd::Statsd.distributed_increment("translation.invocations", tags:)
end
def handle_cedar_errors
yield
rescue => e
case e.class.name
when /SameLanguageTranslationError/
raise SameLanguageTranslationError
when /ContentTooLongError/
raise TextTooLongError
when /UnsupportedLanguageError/
raise UnsupportedLanguageErrorView on GitHub (pinned to 1c9f0bb801)