discourse-ai/lib/translation/category_locale_detector.rb
Natalie Tay 8a3a247b11
DEV: Also detect locale of categories and do not translate if already in the locale (#1413)
Previously I had omitted to add `locale` to the category, as categories tended to be just a single word, and I did not find it would be worth to carry locale information.

Due to certain LLMs that do poorer at translation, category descriptions got pretty messy. We added locale support here - https://github.com/discourse/discourse/pull/32962. 

This PR adds the automatic locale detection, and skips translating to the category's locale.
2025-06-06 22:41:48 +08:00

20 lines
503 B
Ruby

# frozen_string_literal: true
module DiscourseAi
module Translation
class CategoryLocaleDetector
def self.detect_locale(category)
return if category.blank?
text = [category.name, category.description].compact.join("\n\n")
return if text.blank?
detected_locale = LanguageDetector.new(text).detect
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
category.update_column(:locale, locale)
locale
end
end
end
end