Skip to content

Commit 0836516

Browse files
committed
Tags
1 parent 54849f1 commit 0836516

File tree

11 files changed

+141
-16
lines changed

11 files changed

+141
-16
lines changed

app/jobs/scheduled/automatic_translation_backfill.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,24 @@ def translate_records(type, record_ids, target_locale)
8787

8888
def process_batch
8989
records_to_translate = SiteSetting.automatic_translation_backfill_rate
90-
backfill_locales.each_with_index do |target_locale, i|
91-
topic_ids =
92-
fetch_untranslated_model_ids(Topic, "title", records_to_translate, target_locale)
93-
post_ids = fetch_untranslated_model_ids(Post, "raw", records_to_translate, target_locale)
94-
category_ids =
95-
fetch_untranslated_model_ids(Category, "name", records_to_translate, target_locale)
96-
97-
next if topic_ids.empty? && post_ids.empty? && category_ids.empty?
98-
99-
DiscourseTranslator::VerboseLogger.log(
100-
"Translating #{topic_ids.size} topics, #{post_ids.size} posts, #{category_ids.size} categories, to #{target_locale}",
101-
)
102-
103-
translate_records(Topic, topic_ids, target_locale)
104-
translate_records(Post, post_ids, target_locale)
105-
translate_records(Category, category_ids, target_locale)
90+
backfill_locales.each do |target_locale|
91+
[
92+
[Topic, "title"],
93+
[Post, "raw"],
94+
[Category, "name"],
95+
[Tag, "name"],
96+
].each do |model, content_column|
97+
ids =
98+
fetch_untranslated_model_ids(model, content_column, records_to_translate, target_locale)
99+
100+
next if ids.empty?
101+
102+
DiscourseTranslator::VerboseLogger.log(
103+
"Translating #{ids.size} #{model.name} to #{target_locale}",
104+
)
105+
106+
translate_records(model, ids, target_locale)
107+
end
106108
end
107109
end
108110

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
class TagLocale < ActiveRecord::Base
5+
self.table_name = "discourse_translator_tag_locales"
6+
7+
belongs_to :tag
8+
9+
validates :tag_id, presence: true
10+
validates :detected_locale, presence: true
11+
end
12+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
class TagTranslation < ActiveRecord::Base
5+
self.table_name = "discourse_translator_tag_translations"
6+
7+
belongs_to :tag
8+
9+
validates :tag_id, presence: true
10+
validates :locale, presence: true
11+
validates :translation, presence: true
12+
validates :locale, uniqueness: { scope: :tag_id }
13+
end
14+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseAi
4+
class TagTranslator < BaseTranslator
5+
PROMPT_TEMPLATE = <<~TEXT.freeze
6+
You are a translation service specializing in translating forum tags to the asked target_language. Your task is to provide accurate and contextually appropriate translations while adhering to the following guidelines:
7+
8+
1. Translate the tags to target_language asked
9+
2. Keep proper nouns and technical terms in their original language
10+
3. Keep the translated tags short, close to the original length
11+
4. Ensure the translation maintains the original meaning
12+
4. Translated tags will be in lowercase
13+
14+
Provide your translation in the following JSON format:
15+
16+
<output>
17+
{"translation": "your target_language translation here"}
18+
</output>
19+
20+
Here are three examples of correct translation
21+
22+
Original: {"name":"solved", "target_language":"Chinese"}
23+
Correct translation: {"translation": "已解决"}
24+
25+
Original: {"name":"General", "target_language":"French"}
26+
Correct translation: {"translation": "général"}
27+
28+
Original: {"name": "Q&A", "target_language": "Portuguese"}
29+
Correct translation: {"translation": "perguntas e respostas"}
30+
31+
Remember to keep proper nouns like "minecraft" and "toyota" in their original form. Translate the tag now and provide your answer in the specified JSON format.
32+
TEXT
33+
34+
private def prompt_template
35+
PROMPT_TEMPLATE
36+
end
37+
end
38+
end

app/services/discourse_translator/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def self.get_untranslated(translatable, raw: false)
139139
translatable.title
140140
when "Category"
141141
translatable.name
142+
when "Tag"
143+
translatable.name
142144
end
143145
end
144146
end

app/services/discourse_translator/discourse_ai.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def self.translate!(translatable, target_locale_sym = I18n.locale)
4747
text_for_translation(translatable),
4848
language,
4949
).translate
50+
when "Tag"
51+
::DiscourseAi::TagTranslator.new(text_for_translation(translatable), language).translate
5052
end
5153

5254
DiscourseTranslator::TranslatedContentNormalizer.normalize(translatable, translated)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
class CreateTagTranslationTable < ActiveRecord::Migration[7.2]
4+
def change
5+
create_table :discourse_translator_tag_locales do |t|
6+
t.integer :tag_id, null: false
7+
t.string :detected_locale, limit: 20, null: false
8+
t.timestamps
9+
end
10+
11+
create_table :discourse_translator_tag_translations do |t|
12+
t.integer :tag_id, null: false
13+
t.string :locale, null: false
14+
t.text :translation, null: false
15+
t.timestamps
16+
end
17+
18+
add_index :discourse_translator_tag_translations,
19+
%i[tag_id locale],
20+
unique: true,
21+
name: "idx_tag_translations_on_tag_id_and_locale"
22+
end
23+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
module Extensions
5+
module TagExtension
6+
extend ActiveSupport::Concern
7+
prepended { before_update :clear_translations, if: :name_changed? }
8+
include Translatable
9+
end
10+
end
11+
end

lib/discourse_translator/inline_translation.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ def inject(plugin)
8888
serializer.object.translation_for(InlineTranslation.effective_locale).presence
8989
end
9090
end
91+
92+
# tags
93+
94+
plugin.register_modifier(:topic_tags_serializer_name) do |tags, serializer|
95+
# %w[topics tags serializer name]
96+
end
97+
98+
plugin.register_modifier(:sidebar_tag_serializer_name) do |name, serializer|
99+
if !SiteSetting.experimental_inline_translation ||
100+
serializer.object.locale_matches?(InlineTranslation.effective_locale) ||
101+
serializer.scope&.request&.params&.[]("show") == "original"
102+
name
103+
else
104+
serializer.object.translation_for(InlineTranslation.effective_locale).presence
105+
end
106+
end
107+
108+
# plugin.register_modifier(:tag_serializer_name) { |name, serializer| "tag_serializer_name" }
91109
end
92110
end
93111
end

lib/discourse_translator/translated_content_normalizer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def self.normalize(translatable, content)
1010
PrettyText.cleanup(content, {})
1111
when "Category"
1212
content
13+
when "Tag"
14+
content
1315
end
1416
end
1517
end

0 commit comments

Comments
 (0)