2

I am currently working on a public Salesforce Site that must be implemented in a couple different languages. I am correctly displaying my objects' custom labels, but I unfortunately cannot seem to get the translated picklist values.

I am using the toLabel() function in my SOQL queries, but since the Apex Controller executes in the Locale of the Guest Site User, this will simply return the picklist values in the default language.

Has anybody found a workaround for this problem? I am trying to avoid hard-coding values into my Apex classes for picklist translations.

Edit Due to Toby's answer, I am updating my question to tell you that I know the language in which I have to load the picklist values because I am retrieving it as a page parameter. Therefore, to clarify, I need some way to access a specific language's translations based upon a predetermined language parameter.

2
  • How many languages do you have? – Sergio Alcocer Nov 7 '17 at 18:17
  • Too many to hard-code a bunch of values. – Jon Duelfer Nov 7 '17 at 18:39
1

I really don't think Salesforce is going to be able to do this for you. In order to auto-translate Salesforce would need something from the user to determine the language. In the event of a public site, the user record as you have determined is inaccurate/not useful and this is the only information I imagine Salesforce will be able to reliably use.

There's only two ways I can think to auto-translate the values and they both require information from the browser. One is to detect the user's location the other is to detect the user's language. I'd go straight to option 2 as it's likely more consistent (and less likely to raise security concerns. have you ever seen the "this website wants to know your location" warning in chrome?).

Detecting user's language

JavaScript translation options - This references the Google Translate API which allows you to translate without hardcoding values. The other options require hardcoding.

For the lazy people out there:

It's up to you to decide if you want to fight the fight but you could also argue that the site should not be public (requires registration) so that you have a user record for each person. Use the language issue as an example. Many stakeholder's/executives don't know Salesforce at a technical level making them easy to manipulate. Considering the fact that option 1 exists I'd avoid this

For the insane people out there:

If you want to get CRAZY you can try to dynamically set the public user's language or something but this will result in some really wacky behavior. You'd have to set it before every call to the database and that is still not guaranteed to work as a few calls could over lap resulting in users seeing the wrong language or a "unable to lock row" error. The latter you could catch in a try and (dear god don't do this) loop until it succeeds. If you have a low traffic site this might not be too crazy, but still... don't do it.

4
  • When I load the controller, I actually know the language I want to translate into because I am sending a parameter through the url. So, in the constructor of Apex, I know the language I need for the picklist values. The Labels work because I am passing a controller property to the language parameter on the Visualforce page. – Jon Duelfer Nov 7 '17 at 18:34
  • Right but you're not talking about the labels. You're talking about the values. SFDC doesn't provide a method for auto-translating the values that allows you to provide the desired language right? The only thing that changes is that you no longer need JS to determine the language, but you still need JS to perform the translation. – gNerb Nov 7 '17 at 18:39
  • 1
    I can't seem to find anything in the documentation. I have translated the values in the Translation Workbench, so I was hoping that there was someway to access the translated Schema.PicklistEntry for example. But I am afraid they are pre-loading the controller with the default user information, and have hidden this layer from us as developers. – Jon Duelfer Nov 7 '17 at 18:43
  • 1
    Aye, Put an idea up on the Salesforce idea community for this something like schema.pickListEntry.getLabel('en'). Until then, use the google translate API. – gNerb Nov 7 '17 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.