instructure/canvas-lms · warning · Translation::SameLanguageTranslationError

SameLanguageTranslationError

Error message

SameLanguageTranslationError

What it means

Translation#handle_cedar_errors wraps CedarClient calls and translates exception class names into Canvas Translation hierarchy errors. When a Cedar client raises an exception whose class name matches /SameLanguageTranslationError/, it is re-raised as Translation::SameLanguageTranslationError. This is a re-mapping layer: either check_same_language inside the yielded block raised it (and it is re-raised identically), or the Cedar client itself raised a same-language error that gets normalized.

Solutions

  1. Skip the translation call when detected/source language equals the target language
  2. Rescue Translation::SameLanguageTranslationError and fall back to the original content
  3. Constrain target-language pickers to languages different from the detected content language
  4. Normalize language codes before invoking translate_text/translate_html

Example fix

# before
Translation.translate_html(html_string: body, tgt_lang: locale, options: opts)
# after
begin
  Translation.translate_html(html_string: body, tgt_lang: locale, options: opts)
rescue Translation::SameLanguageTranslationError
  body
end
Defensive patterns

Strategy: try-catch

Try / catch

begin
  Translation.translate_html(html_string: html, tgt_lang: locale, options: opts)
rescue Translation::SameLanguageTranslationError
  html # already in target language
end

Prevention

When it happens

Trigger: translate_text/translate_html invoked where the content's detected language equals the target language and a SameLanguageTranslationError-class exception occurs inside the block (from check_same_language or from CedarClient), so handle_cedar_errors re-raises it as Translation::SameLanguageTranslationError.

Common situations: Auto-translate of inbox messages or discussion entries already written in the reader's language; UI allowing translation requests where source and target locales coincide; language detection returning the same code as the requested target.

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


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/a4a3831c48393beb. Report an issue: GitHub.

Appendix: source

Thrown at lib/translation.rb:137

    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 UnsupportedLanguageError
      when /ValidationError/
        raise ValidationError
      else
        raise TranslationError, e.message
      end
    end
  end
end

View on GitHub (pinned to 1c9f0bb801)