mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-01 20:12:15 +00:00
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.
20 lines
503 B
Ruby
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
|