shopify functions translation

Solved

shopify functions translation

AW1234
Shopify Partner
49 8 16

Hi,

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?

Is there a way to translate or a workaround pls?

when nothing in message field:

AW1234_0-1737032966846.png

when something in message field:

code.png

test.png

Accepted Solution (1)

AW1234
Shopify Partner
49 8 16

This is an accepted solution.

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');
  }
}
 
And I use it like this:
message: chooseTranslationByLanguage(input.localization.language.isoCode).discount.replace('{percent}', disc).replace('{qty}', qty)
 
I will mark this as the solution, but if there is a better method pls let me know.

View solution in original post

Reply 1 (1)

AW1234
Shopify Partner
49 8 16

This is an accepted solution.

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');
  }
}
 
And I use it like this:
message: chooseTranslationByLanguage(input.localization.language.isoCode).discount.replace('{percent}', disc).replace('{qty}', qty)
 
I will mark this as the solution, but if there is a better method pls let me know.