Adobe 27510753 Scripting Guide - Page 69

Variable values in localized strings, Enabling automatic localization, de: Hallo Welt, Hallo Welt

Page 69 highlights

Adobe InDesign CS2 Scripting Guide Using ExtendScript Tools and Features 61 For portions of your user interface that are displayed on the screen, you may want to localize the displayed text. You can localize any string explicitly using the Global localize function, which takes as its argument a localization object containing the localized versions of a string. A localization object is a JavaScript object literal whose property names are locale names, and whose property values are the localized text strings. The locale name is a standard language code with an optional region identifier. For details of the syntax, see the "Locale names" section. In this example, a msg object contains localized text strings for two locales. This object supplies the text for an alert dialog. msg = { en: "Hello, world", de: "Hallo Welt" }; alert (msg); ExtendScript matches the current locale and platform to one of the object's properties and uses the associated string. On a German system, for example, the property de: "Hallo Welt" is converted to the string "Hallo Welt". Variable values in localized strings Some localization strings need to contain additional data whose position and order may change according to the language used. You can include variables in the string values of the localization object, in the form %n. The variables are replaced in the returned string with the results of JavaScript expressions, supplied as additional arguments to the localize function. The variable %1 corresponds to the first additional argument, %2 to the second, and so on. Because the replacement occurs after the localized string is chosen, the variable values are inserted in the correct position. For example: today = { en: "Today is %1/%2.", de: "Heute ist der %2.%1." }; d = new Date(); alert (localize (today, d.getMonth()+1, d.getDate())); Enabling automatic localization ExtendScript offers an automatic localization feature. When it is enabled, you can specify a localization object directly as the value of any property that takes a localizable string, without using the localize function. For example: msg = { en: "Yes", de: "Ja", fr: "Oui" }; alert (msg); To use automatic translation of localization objects, you must enable localization in your script with this statement: $.localize = true; The localize function always performs its translation, regardless of the setting of the $.localize variable. For example: msg = { en: "Yes", de: "Ja", fr: "Oui" }; //Only works if the $.localize=true alert (msg); //Always works, regardless of $.localize value alert ( localize (msg)); If you need to include variables in the localized strings, use the localize function.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

Adobe InDesign CS2 Scripting Guide
Using ExtendScript Tools and Features
61
For portions of your user interface that are displayed on the screen, you may want to localize the displayed
text. You can localize any string explicitly using the
Global localize
function, which takes as its argument
a
localization object
containing the localized versions of a string.
A localization object is a JavaScript object literal whose property names are locale names, and whose
property values are the localized text strings. The locale name is a standard language code with an optional
region identifier. For details of the syntax, see the “Locale names” section.
In this example, a
msg
object contains localized text strings for two locales. This object supplies the text for an
alert dialog.
msg = { en: "Hello, world", de: "Hallo Welt" };
alert (msg);
ExtendScript matches the current locale and platform to one of the object’s properties and uses the
associated string. On a German system, for example, the property
de: “Hallo Welt”
is converted to the
string
“Hallo Welt”
.
Variable values in localized strings
Some localization strings need to contain additional data whose position and order may change according to
the language used.
You can include variables in the string values of the localization object, in the form
%
n
. The variables are
replaced in the returned string with the results of JavaScript expressions, supplied as additional arguments to
the
localize
function. The variable
%1
corresponds to the first additional argument,
%2
to the second, and
so on.
Because the replacement occurs after the localized string is chosen, the variable values are inserted in the
correct position. For example:
today = {
en: "Today is %1/%2.",
de: "Heute ist der %2.%1."
};
d = new Date();
alert (localize (today, d.getMonth()+1, d.getDate()));
Enabling automatic localization
ExtendScript offers an automatic localization feature. When it is enabled, you can specify a localization object
directly as the value of any property that takes a localizable string, without using the
localize
function. For
example:
msg = { en: "Yes", de: "Ja", fr: "Oui" };
alert (msg);
To use automatic translation of localization objects, you must enable localization in your script with this
statement:
$.localize = true;
The
localize
function always performs its translation, regardless of the setting of the
$.localize
variable.
For example:
msg = { en: "Yes", de: "Ja", fr: "Oui" };
//Only works if the $.localize=true
alert (msg);
//Always works, regardless of $.localize value
alert ( localize (msg));
If you need to include variables in the localized strings, use the
localize
function.