6

Scenario with Custom Lables

I've defined tons of custom labels in Salesforce setup in order to cleanly support translations for localized strings. This work great if the labels are used for UI translation purposes, when we need the strings in the language set as the current logged-in user's language.

Now I need to implement a language selection on an object, let's say on an invoice. Assume we need to render an invoice in the language the recipient prefers which often differs from the UI-language the user who is generating the invoice.

E.g. the I've set my Salesforce user to English but a have to generate a invoice-PDF in German.

Assume further I'm generating the invoices with visualforce using renderAs=PDF and ALL the relevant label are only used in APEX to compose some markup passed as one big string to Visualforce which does the PDF-rendering.

I've found here so far, that we can get the label in APEX using

 System.Label.Label_name  

But this brings poorly back the label in the current user's language.

Is there really no way to get the translation of a label in any language regardless of what is set as the user's language?

ExternalString

During my research, I wasn't able to dig anything useful out of the docs, but I've found this: https://developer.salesforce.com/forums/?id=906F00000008xGvIAI

ExternalString[] extStrArr = [ Select Id,Value,Name FROM ExternalString ]
ExternalStringLocalization[] eslArr = [Select ExternalStringId,Value,Language FROM ExternalStringLocalization ]

But as posted there, this does not work and both objects ExternalString and ExternalStringLocalization simple are not available in APEX. Neither I could find them in the docs. But this would be exactly(!) what I need.

I want to avoid custom translation methods

Sure I could create my own Translations-Object. But I use Salesforce because I want to do all I18n and L10n with standard platform mechanisms and NOT custom stuff to fiddle around. And I want all translations in one place and not spread over multiple tools, objects and places making it difficult for external translators to do their job. So custom label seems to me the natural place to store and handle my texts and translations.

Possible Workarounds?

If this is not supported, has anyone found a workaround for this requirement. I'm sure this is pretty common for processes in multilingual environments to need translations in any language at any time.

I thought about this dirty trick:

temporarily switch the current users language just as long as it takes to generate the PDF, grab the labels and immediately switch the user's language back.

Is this feasible? Even if it could work it feels bad.

Can we do it better?

3
  • In apex:page there is a attribute 'language'. Did you try setting it to the language you want and check if the VF pdf is getting translated to german or any language you want?
    – RedDevil
    Jul 16, 2017 at 10:09
  • @RedDevil At this point I did not try to set the language attribute of apex:page - but I will try. Will it also affect what I get by System.Label.Label_Name in an APEX controller, or only in VF using $Label ?
    – Uwe Heim
    Jul 16, 2017 at 10:13
  • I think its both. Both for the VF page and the class used by the page. In one of my old implementations i set this language attribute from the constructor of the class and it worked both for the page and the class
    – RedDevil
    Jul 16, 2017 at 10:15

1 Answer 1

4

In apex:page there is an attribute 'language' which you can set as the language of your choice/requirement to override the users language.

Check this link

language

The language used to display labels that have associated translations in Salesforce. This value overrides the language of the user viewing the page. Possible values for this attribute include any language keys for languages supported by Salesforce, for example, "en" or "en-US".

From documentation seems it should be possible. I haven't tried it out though.

1
  • 1
    This is also available in components, so you can wrap a particular label in a component if you want to display the translations for just one or some labels. May 9, 2018 at 4:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .