Localizing prompts that has string interpolated generable objects

I'm working on localizing my prompts to support multiple languages, and in some cases my prompts has String interpolated Generable objects. for example:

"Given the following workout routine: \(routine), suggest one additional exercise to complement it."

In the Strings dictionary, I'm only able to select String, Int or Double parameters using %@ and %lld.

Has anyone found a way to accomplish this?

Answered by _Eric in 851533022

To create a prompt object with a mix of localized strings and Generable objects, you can pass your Generable directly to the Prompt initializer:

Prompt {
    String(localized: "Given the following workout routine, suggest one additional exercise to complement it:")
    routine
}

Instead of localizing the prompt, you can also prompt the model in English, and instruct it to generate a localized result. You may then find it easier to iterate over the prompt like this over relying on external translators to create a localized prompt for you.

// After checking that `locale` is supported by the model:
let languageName = Locale(identifier: "en").localizedString(forLanguageCode: locale.languageCode)
Prompt {
    "Given the following workout routine, suggest one additional exercise to complement it. Respond in \(languageName)."
    routine
}
Accepted Answer

To create a prompt object with a mix of localized strings and Generable objects, you can pass your Generable directly to the Prompt initializer:

Prompt {
    String(localized: "Given the following workout routine, suggest one additional exercise to complement it:")
    routine
}

Instead of localizing the prompt, you can also prompt the model in English, and instruct it to generate a localized result. You may then find it easier to iterate over the prompt like this over relying on external translators to create a localized prompt for you.

// After checking that `locale` is supported by the model:
let languageName = Locale(identifier: "en").localizedString(forLanguageCode: locale.languageCode)
Prompt {
    "Given the following workout routine, suggest one additional exercise to complement it. Respond in \(languageName)."
    routine
}
Localizing prompts that has string interpolated generable objects
 
 
Q