I am developing a multi-language application which supports English and Japanese. But, as everyone might have known, the language structure of these two languages are quite different. For example, when I want to show "Line 1" on a VF page in Japanese, it'd be "1行目"(meaning the first line).

I created a custom label named 'Line'.

Japanese = 行目 and English = Line.

I'm planning to develop the process mentioned above as follows; (The code might not compile. It's just a concept.)

String showString;
String number = '1';
if(UserInfo.getLanguage() =='ja') showString = number + Label.line;
else showString = Lable.line + number;

But I don't want to put an if in my code for every language.

Is there any better ways to solve this problem?


This is where String.format comes in play. For example, you could write this:

String errorLine = String.format(Label.error_on_line_x, new string[] { String.valueof(linenumber) });

This way, label might be 'Error on line {0}' in English, and '{0}行目' in Japanese, and it will format correctly in both languages (use the label translation system...).

  • Thank you. I just did as you suggested and it works. This is very helpful. :) – lw29 Jul 8 '14 at 6:29

What I would do in this case is create a set of languages that have the number before the label:

Set <String> languagesWithLabelAsSuffix = new Set <String> {'ja', 'XX', 'YY'};

and then check if the user language is one of the above:

String showString;
String number = '1';
if (languagesWithLabelAsSuffix.contains(UserInfo.getLanguage()) == true)
    showString = number + Label.line;
    showString = Lable.line + number;

Also, for easier maintainability, you can create custom settings where you can store semi-colon separated language initials for those languages, parse that string and store the values in the languagesWithLabelAsSuffix set.

  • Thank you for the advice! This is better than my approach. I have never used custom settings before. I'll learn more about it. Must be useful for the future. :] – lw29 Jul 8 '14 at 6:27
  • 1
    -1 This idea is exceedingly limiting - the developer must know all the kinks of the languages he must support. Instead of trying to turn the developer into a linguist, let the translator do this as in @sfdcfox's answer – Charles Koppelman Dec 29 '14 at 17:17

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.