Class: Traductor::PromptBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/traductor/prompt_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_locale:, target_locale:, glossary: nil) ⇒ PromptBuilder

Returns a new instance of PromptBuilder.



5
6
7
8
9
# File 'lib/traductor/prompt_builder.rb', line 5

def initialize(source_locale:, target_locale:, glossary: nil)
  @source_locale = source_locale
  @target_locale = target_locale
  @glossary = glossary
end

Instance Method Details

#system_prompt ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/traductor/prompt_builder.rb', line 11

def system_prompt
  parts = []
  parts << <<~PROMPT.strip
    You are a professional translator specializing in software localization.
    You translate from #{locale_name(@source_locale)} to #{locale_name(@target_locale)}.

    Rules:
    1. Translate ONLY the values, never the keys.
    2. Preserve all placeholders exactly as they appear (e.g., __TVAR0__, __TVAR1__). Do not translate, modify, or reorder placeholders.
    3. Maintain the same tone and formality level as the source text.
    4. For technical terms (e.g., API, URL, HTML), keep them untranslated unless there is a well-established localized term.
    5. Handle pluralization naturally for the target language.
    6. Keep translations concise — UI strings should remain similar in length.
    7. Respond ONLY with valid JSON. No explanations, no markdown fences, no extra text.
  PROMPT

  if @glossary
    glossary_text = @glossary.to_prompt_text(@target_locale)
    if glossary_text
      parts << <<~GLOSSARY.strip

        Glossary — always use these exact translations:
        #{glossary_text}
      GLOSSARY
    end
  end

  parts.join("\n\n")
end

#user_prompt(batch) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/traductor/prompt_builder.rb', line 41

def user_prompt(batch)
  <<~PROMPT.strip
    Translate the following JSON object's values from #{locale_name(@source_locale)} to #{locale_name(@target_locale)}.
    Return a JSON object with the same keys and translated values.

    #{JSON.generate(batch)}
  PROMPT
end