I am making a shopify function to give discounts on products depending on a metafield, my question will be about the message field in FunctionRunResult.Discount, I can put a string to be shown instead of the title of the discount, I would like to tell the customer that if he buys 1 more he gets a better discount and for that I need to translate that.
My researches gave nothing, the “assistant” AI in the docs uses variables that doesn’t exist and chatgpt says there is no way to translate in shopify functions. But why is there locales folder then?
I found a way, don’t know if it is THE way.
So first I found there was a localization field in RunInput:
in run.graphql:
localization{
language{
isoCode
}
}
then I used this to select a json file in my locales folder:
function chooseTranslationByLanguage(isoCode) {
switch (isoCode) {
case ‘FR’:
return require(‘../locales/fr.json’);
case ‘NL’:
return require(‘../locales/nl.json’);
case ‘DE’:
return require(‘../locales/de.json’);
case ‘EN’:
default:
return require(‘../locales/en.default.json’);
}
}