discourse/plugins/discourse-ai/lib/translation/post_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

25 lines
581 B
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Translation
class PostLocaleDetector
def self.detect_locale(post)
return if post.blank?
text = PostDetectionText.get_text(post)
if text.blank?
locale = SiteSetting.default_locale
else
detected_locale = LanguageDetector.new(text, post:).detect
return if detected_locale.blank?
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
end
post.update_column(:locale, locale)
locale
end
end
end
end