Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions app/models/concerns/discourse_translator/translatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module DiscourseTranslator
module Translatable
extend ActiveSupport::Concern
Comment on lines +4 to +5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These concerns are applied in the Post and Topic models.


prepended do
has_many :translations,
class_name: "DiscourseTranslator::#{name}Translation",
dependent: :destroy
has_one :content_locale, class_name: "DiscourseTranslator::#{name}Locale", dependent: :destroy
end

def set_detected_locale(locale)
# locales should be "en-US" instead of "en_US" per https://www.rfc-editor.org/rfc/rfc5646#section-2.1
locale = locale.to_s.gsub("_", "-")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but do we expect more than one _ to - substitution? If no, maybe just .sub is enough?

Copy link
Contributor Author

@nattsw nattsw Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read from https://learn.microsoft.com/en-us/globalization/locale/standard-locale-names (Microsoft is one of our translation providers) that ca-ES-valencia is an example of a locale that can have double dash.

Though the Microsoft API only returns stuff like “tlh-Latn” one dash, I am being a bit over-cautious if other providers (AI?) return such cases.

(content_locale || build_content_locale).update!(detected_locale: locale)
end

def set_translation(locale, text)
locale = locale.to_s.gsub("_", "-")
translations.find_or_initialize_by(locale: locale).update!(translation: text)
end

def translation_for(locale)
translations.find_by(locale: locale)&.translation
end

def detected_locale
content_locale&.detected_locale
end
Comment on lines +14 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of accessing custom fields directly, with the new tables, we can access the related translation metadata from the models themselves.


private

def clear_translations
return if !SiteSetting.translator_enabled

translations.delete_all
content_locale&.destroy
end
end
end
23 changes: 23 additions & 0 deletions app/models/discourse_translator/post_locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module DiscourseTranslator
class PostLocale < ActiveRecord::Base
self.table_name = "discourse_translator_post_locales"

belongs_to :post

validates :post_id, presence: true, uniqueness: true
validates :detected_locale, presence: true
end
end

# == Schema Information
#
# Table name: discourse_translator_post_locales
#
# id :bigint not null, primary key
# post_id :integer not null
# detected_locale :string(20) not null
# created_at :datetime not null
# updated_at :datetime not null
#
34 changes: 34 additions & 0 deletions app/models/discourse_translator/post_translation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module DiscourseTranslator
class PostTranslation < ActiveRecord::Base
self.table_name = "discourse_translator_post_translations"

belongs_to :post

validates :post_id, presence: true
validates :locale, presence: true
validates :translation, presence: true
validates :locale, uniqueness: { scope: :post_id }

def self.translation_for(post_id, locale)
find_by(post_id: post_id, locale: locale)&.translation
end
end
end

# == Schema Information
#
# Table name: discourse_translator_post_translations
#
# id :bigint not null, primary key
# post_id :integer not null
# locale :string not null
# translation :text not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# idx_on_post_id_locale_0cc3d81e5b (post_id,locale) UNIQUE
#
23 changes: 23 additions & 0 deletions app/models/discourse_translator/topic_locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module DiscourseTranslator
class TopicLocale < ActiveRecord::Base
self.table_name = "discourse_translator_topic_locales"

belongs_to :topic

validates :topic_id, presence: true, uniqueness: true
validates :detected_locale, presence: true
end
end

# == Schema Information
#
# Table name: discourse_translator_topic_locales
#
# id :bigint not null, primary key
# topic_id :integer not null
# detected_locale :string(20) not null
# created_at :datetime not null
# updated_at :datetime not null
#
34 changes: 34 additions & 0 deletions app/models/discourse_translator/topic_translation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module DiscourseTranslator
class TopicTranslation < ActiveRecord::Base
self.table_name = "discourse_translator_topic_translations"

belongs_to :topic

validates :topic_id, presence: true
validates :locale, presence: true
validates :translation, presence: true
validates :locale, uniqueness: { scope: :topic_id }

def self.translation_for(topic_id, locale)
find_by(topic_id: topic_id, locale: locale)&.translation
end
end
end

# == Schema Information
#
# Table name: discourse_translator_topic_translations
#
# id :bigint not null, primary key
# topic_id :integer not null
# locale :string not null
# translation :text not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# idx_on_topic_id_locale_70b2f83213 (topic_id,locale) UNIQUE
#
90 changes: 34 additions & 56 deletions app/services/discourse_translator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def self.cache_key
# Returns the stored translation of a post or topic.
# If the translation does not exist yet, it will be translated first via the API then stored.
# If the detected language is the same as the target language, the original text will be returned.
# @param topic_or_post [Post|Topic]
def self.translate(topic_or_post)
return if text_for_translation(topic_or_post).blank?
detected_lang = detect(topic_or_post)
# @param translatable [Post|Topic]
def self.translate(translatable)
return if text_for_translation(translatable).blank?
detected_lang = detect(translatable)

return detected_lang, get_text(topic_or_post) if (detected_lang&.to_s == I18n.locale.to_s)
return detected_lang, get_text(translatable) if (detected_lang&.to_s == I18n.locale.to_s)

existing_translation = get_translation(topic_or_post)
existing_translation = get_translation(translatable)
return detected_lang, existing_translation if existing_translation.present?

unless translate_supported?(detected_lang, I18n.locale)
Expand All @@ -46,27 +46,27 @@ def self.translate(topic_or_post)
),
)
end
[detected_lang, translate!(topic_or_post)]
[detected_lang, translate!(translatable)]
end

# Subclasses must implement this method to translate the text of a post or topic
# then use the save_translation method to store the translated text.
# @param topic_or_post [Post|Topic]
def self.translate!(topic_or_post)
# @param translatable [Post|Topic]
def self.translate!(translatable)
raise "Not Implemented"
end

# Returns the stored detected locale of a post or topic.
# If the locale does not exist yet, it will be detected first via the API then stored.
# @param topic_or_post [Post|Topic]
def self.detect(topic_or_post)
return if text_for_detection(topic_or_post).blank?
get_detected_locale(topic_or_post) || detect!(topic_or_post)
# @param translatable [Post|Topic]
def self.detect(translatable)
return if text_for_detection(translatable).blank?
get_detected_locale(translatable) || detect!(translatable)
end

# Subclasses must implement this method to translate the text of a post or topic
# then use the save_translation method to store the translated text.
# @param topic_or_post [Post|Topic]
# Subclasses must implement this method to detect the text of a post or topic
# then use the save_detected_locale method to store the detected locale.
# @param translatable [Post|Topic]
def self.detect!(post)
raise "Not Implemented"
end
Expand All @@ -75,52 +75,33 @@ def self.access_token
raise "Not Implemented"
end

def self.get_translation(topic_or_post)
translated_custom_field =
topic_or_post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] || {}
translated_custom_field[I18n.locale]
def self.get_translation(translatable)
translatable.translation_for(I18n.locale)
end

def self.save_translation(topic_or_post)
translated_custom_field =
topic_or_post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] || {}
translated_text = translated_custom_field[I18n.locale]

if translated_text.nil?
translated_text = yield

topic_or_post.custom_fields[
DiscourseTranslator::TRANSLATED_CUSTOM_FIELD
] = translated_custom_field.merge(I18n.locale => translated_text)

topic_or_post.save!
end

translated_text
def self.save_translation(translatable)
translation = yield
translatable.set_translation(I18n.locale, translation)
translation
end

def self.get_detected_locale(topic_or_post)
topic_or_post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]
def self.get_detected_locale(translatable)
translatable.detected_locale
end

def self.save_detected_locale(topic_or_post)
def self.save_detected_locale(translatable)
detected_locale = yield
topic_or_post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = detected_locale

if !topic_or_post.custom_fields_clean?
topic_or_post.save_custom_fields
topic_or_post.publish_change_to_clients!(:revised) if topic_or_post.class.name == "Post"
end
translatable.set_detected_locale(detected_locale)

detected_locale
end

def self.get_text(topic_or_post)
case topic_or_post.class.name
def self.get_text(translatable)
case translatable.class.name
when "Post"
topic_or_post.cooked
translatable.cooked
when "Topic"
topic_or_post.title
translatable.title
end
end

Expand All @@ -143,15 +124,12 @@ def self.strip_tags_for_detection(detection_text)
html_doc.to_html
end

def self.text_for_detection(topic_or_post)
strip_tags_for_detection(get_text(topic_or_post)).truncate(
DETECTION_CHAR_LIMIT,
omission: nil,
)
def self.text_for_detection(translatable)
strip_tags_for_detection(get_text(translatable)).truncate(DETECTION_CHAR_LIMIT, omission: nil)
end

def self.text_for_translation(topic_or_post)
get_text(topic_or_post).truncate(SiteSetting.max_characters_per_translation, omission: nil)
def self.text_for_translation(translatable)
get_text(translatable).truncate(SiteSetting.max_characters_per_translation, omission: nil)
end
end
end
31 changes: 31 additions & 0 deletions db/migrate/20250205082400_create_translation_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class CreateTranslationTables < ActiveRecord::Migration[7.2]
def change
create_table :discourse_translator_topic_locales do |t|
t.integer :topic_id, null: false
t.string :detected_locale, limit: 20, null: false
t.timestamps
end

create_table :discourse_translator_topic_translations do |t|
t.integer :topic_id, null: false
t.string :locale, null: false
t.text :translation, null: false
t.timestamps
end

create_table :discourse_translator_post_locales do |t|
t.integer :post_id, null: false
t.string :detected_locale, limit: 20, null: false
t.timestamps
end

create_table :discourse_translator_post_translations do |t|
t.integer :post_id, null: false
t.string :locale, null: false
t.text :translation, null: false
t.timestamps
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating these tables without indexes first.

end
end
Loading
Loading