-
Notifications
You must be signed in to change notification settings - Fork 52
DEV: Move translation custom fields into their own tables #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
ce6bdca
6c7e155
069d043
e45e0b3
06c45b5
33aab2b
723402b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| 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("_", "-") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicking, but do we expect more than one
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 | ||
| # |
| 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 | ||
| # |
| 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 | ||
| # |
| 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 | ||
| # |
| 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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating these tables without indexes first. |
||
| end | ||
| end | ||
There was a problem hiding this comment.
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
PostandTopicmodels.