Referencing and initializing localized messages
The process of removing hard-coded references to language-dependent text from an application is known as string externalization, because the process results in language-dependent textual information being stored externally to the application.
Language- and culture-dependent strings in an internationalized VAST application are stored as pool dictionary variables and are accessed directly (without the need for accessor methods) in application source code. Appropriate language-dependent strings are bound to language-independent message identifiers (pool dictionary variables) in response to changes of locale. Thus, the language independent identifiers are used throughout the application source code regardless of the target language.
The following example shows the conversion of the previous example to incorporate an externalized string reference.
displayWarningFor: userName operation: anOperation
"Display a warning message on the Transcript warning the user called
userName that the operation called anOperation cannot be performed."
(MyAppWarningTemplate1 bindWith: Username with: anOperation).
The original template string 'Sorry, %1, I cannot perform %2!' has been replaced by a reference to a pool variable called MyAppWarningTemplate1. This method is now language-independent and can be localized simply by modifying the value to which the pool variable MyAppWarningTemplate1 is bound. Pool variables are most easily rebound using the features provided by NlsMessageCatalog.
The MyAppWarningTemplate1 pool variable must be reflected in the code that declares and initializes the pool dictionaries used by the application. It is the responsibility of each application to declare pool dictionaries and variables, initialize the values of pool variables (typically using an NlsMessageCatalog), and remove the pools when the application is unloaded.
Removing hard-coded strings
Literal arrays containing strings require special attention when strings are being externalized. The manner in which Smalltalk expressions are parsed does not allow strings in literal arrays to be replaced with pool variables because items in literal arrays are themselves assumed to be literals.
"Answer an Array of language-dependent message strings."
^('message1' 'message2' 'message3' 'message4')
Externalizing the strings contained in the method illustrated previously requires that the literal array be replaced with an explicitly created array. The externalized version is presented in the following example. Notice that the string literals have been replace with pool variables (Message1, Message2, etc.) and that the array containing the elements is now created explicitly.
"Answer an Array of language-dependent message strings."
with: Message1
with: Message2
with: Message3
with: Message4
Last modified date: 10/08/2020