discourse/plugins/discourse-ai/lib/translation/topic_locale_detector.rb
Natalie Tay 5af43b64ce
FIX: Improve prompt and check returned value conforms to standard (#35763)
This commit improves the prompt, and also matches the return value
against this:
- https://datatracker.ietf.org/doc/html/rfc5646#section-2.2.1
- **Primary Language Subtag**: ... Two-character primary language
subtags were defined in the IANA registry according to the assignments
found in the standard "ISO 639-1:2002 ...
- **Extended Language Subtags**: ... Extended language subtags consist
solely of three-letter subtags.

This commit also moves examples into its own 

Meta:
https://meta.discourse.org/t/locale-detector-return-value-issues/381852
2025-11-05 17:41:49 +08:00

18 lines
447 B
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Translation
class TopicLocaleDetector
def self.detect_locale(topic)
return if topic.blank?
detected_locale = LanguageDetector.new(topic.title.dup, topic:).detect
return if detected_locale.blank?
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
topic.update_column(:locale, locale)
locale
end
end
end
end