[SalesForce] APEX: get a Custom Label translation in a certain language which is not equal to the current user’s language

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?

Best Answer

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.