.NET Tutorial

Externalizing Concatenated Strings

In this lesson, you will learn how to use Globalyzer to handle concatenated display messages, or messages that include hardcoded display text appended to variables. This requires special handling because the order in which the elements of text go together often depends on the translation.

Globalyzer detects string concatenation during scanning, and will set the priority to 'C'.

Within your Globalyzer Embedded String results (Select Embedded Strings in the Results dropdown if needed), the following issues deal with concatenated strings:

It is recommended that you sort your scan results by "File" so that you see these results in the same order as displayed.

To handle these, we will use the static String.Format method within the .NET API. This process involves the placement of a string in the .resx file, with placeholders for the variables that are populated at runtime. We then retrieve the formatted string from the resource file with the GetString call and pass that formatted string into the Format method so the placeholders are replaced with the correct runtime values prior to displaying the string.

  1. First let's change Globalyzer so that it inserts an Externalized String comment each time a string is externalized. Select Window=>Preferences=>Globalyzer to display the Globalyzer Preferences dialog. Check the Automatically Insert Externalized String Comment checkbox as shown here.

    Click OK to save your changes.

  2. Now, let's return to the Scan Results table and double-click on the first row containing the concatenated Hello string. The file should open in the Source Files view, with Hello selected.

  3. We will be editing the following text:

  4. The process of fixing string concatenations involves two steps: First, the .NET String.Format method will be used to convert the code so that the dynamic parts of the concatenated string are passed as parameters to the method and the base string is changed to include the insertion points; and second, the new string is externalized to the resource file.

    Edit the text to resemble the screenshot below. Make sure to leave the word Hello intact, but remove all remaining quotes and operators. Replace each variable with a bracketed number that represents its order in the string being externalized (starting with 0). Your line should mirror the one below:

  5. Save the file using Ctrl-S. This will trigger the Workbench to rescan your code. You should now see the active issue "Hello, {0} {1}".

  6. Click on the "Hello, {0} {1}" result in the scan results window. Click the Externalize selected String button.

  7. The string should be replaced with a GetString call. Also, since we've enabled Automatically Insert Externalized String Comment in the Preferences, Globalyzer will paste a comment above the externalized code.

  8. As you can see in the above example, you've embedded the call to GetString in the call to String.Format, which also takes the runtime variables that the Format method will order according to the specification that comes from the resource file along with the translated text.

  9. Repeat this process for the concatenated string that starts with You currently have. Change the line the issue appears on to:

    Save, wait for Globalyzer to rescan, select the issue and externalize. When finished, your code should look as follows:

  10. Adding String.Format will have created new Locale Sensitive Method issues. You should mark these issues to be ignored, either by setting the Status to Ignore or inserting an Ignore Comment.

You have now externalized the concatenated strings from the C# code. In the next lesson we will handle the regular strings.