Strings
String Literals
String literals can be specified using either double (") or single (') quotes. The main reason to use one over the other is to avoid character escapes within the string literal—for example, if the string literal actually contains double quotes.
By enclosing the string in single quotes, you do not have to escape the embedded double quotes. Consider the following two examples, which are both valid:
var quote = "Winston Churchill said: \"Never in the field of human conflict was so much owed by so many to so few.\"" var quote = 'Winston Churchill said: "Never in the field of human conflict was so much owed by so many to so few."'
Expressions can be embedded within the string literal by using curly braces:
var name = "Jim"; // prints My name is Jim println ( "My name is {name}" );
The embedded expression must be a valid JavaFX or Java expression that returns an object. This object will be converted to a string using its toString() method. For instance:
println ( "Today is {java.util.Date{}}" ); var state ="The state is { if(running) "Running" else "Stopped"}"; println(" The state is {getStateStr()}" ); println("The state is { if(checkRunning()) "Running" else "Stopped"}");
Also, a string literal may be split across lines:
var quote = "Winston Churchill said: " "\"Never in the field of human conflict was so much owed " "by so many to so few.\"";
In this example, the strings from both lines are concatenated into one string. Only the string literals within the quotes are used and any white space outside of the quotes is ignored.
Unicode characters can be entered within the string literal using \u + the four digit unicode.
var thanks = "dank\u00eb"; // dankë
Formatting
Embedded expressions within string literals may contain a formatting code that specifies how the embedded expression should be presented. Consider the following:
var totalCountMessage = "The total count is {total}";
Now if total is an integer, the resulting string will show the decimal number; but if total is a Number, the resulting string will show the number formatted according to the local locale.
var total = 1000.0;
produces:
The total count is 1000.0
To format an expression, you need a format code within the embedded expression. This is a percent (%) followed by the format codes. The format code is defined in the java.util.Formatter class. Please refer to its JavaDoc page for more details (http://java.sun.com/javase/6/docs/api/index.html).
println("Total is {%f total}"); // Total is 1000.000000 println("Total is {%.2f total}"); // Total is 1000.00 println("Total is {%5.0f total}"); // Total is 1000 println("Total is {%+5.0f total}"); // Total is +1000 println("Total is {%,5.0f total}"); // Total is 1,000
Internationalization
To internationalize a string, you must use the “Translate Key” syntax within the string declaration. To create a translate key, the String assignment starts with ## (sharp, sharp) combination to indicate that the string is to be translated to the host locale. The ## combination is before the leading double or single quote. Optionally, a key may be specified within square brackets ([]). If a key is not specified, the string itself becomes the key into the locale properties file. For example:
var postalCode = ## "Zip Code: "; var postalCode = ##[postal]"Zip Code: ";
In the preceding example, using the first form, the key is "Zip Code: ", whereas for the second form, the key is "postal". So how does this work?
By default, the localizer searches for a property file for each unique script name. This is the package name plus script filename with a locale and a file type of .fxproperties. So, if your script name is com.mycompany.MyClass, the localizer code would look for a property file named com/mycompany/MyClass_xx.fxproperties on the classpath, where xx is the locale. For example, for English in the United Kingdom, the properties filename would be com/mycompany/MyClass_en_GB.fxproperties, whereas French Canadian would be com/mycompany/MyClass_fr_CA.fxproperties. If your default locale is just English, the properties file would be MyClass_en.fxproperties. The more specific file is searched first, then the least specific file is consulted. For instance, MyClass_en_GB.fxproperties is searched for the key and if it is not found, then MyClass_en.fxproperties would be searched. If the key cannot be found at all, the string itself is used as the default. Here are some examples:
Example #1:
println(##"Thank you"); French – MyClass_fr.fxproperties: "Thank you" = "Merci" German – MyClass_de.fxproperties: "Thank you" = "Danke" Japanese – MyClass_ja.fxproperties: "Thank you" = "Arigato"
Example #2:
println(##[ThankKey] "Thank you"); French – MyClass_fr.fxproperties: "ThankKey" = "Merci" German – MyClass_de.fxproperties: "ThankKey" = "Danke" Japanese – MyClass_ja.fxproperties: "ThankKey" = "Arigato"
When you use a string with an embedded expression, the literal key contains a %s, where the expression is located within the string. For example:
println(##"Hello, my name is {firstname}");
In this case, the key is "Hello, my name is %s". Likewise, if you use more than one expression, the key contains a "%s" for each expression:
println(##"Hello, my name is {firstname} {lastname}");
Now, the key is "Hello, my name is %s %s".
This parameter substitution is also used in the translated strings. For example:
French – MyClass_fr.fxproperties:
"Hello, my name is %s %s" = "Bonjour, je m'appelle %s %s"
Lastly, you can associate another Properties file to the script. This is done using the javafx.util.StringLocalizer class. For example:
StringLocalizer.associate("com.mycompany.resources.MyResources", "com.mycompany");
Now, all translation lookups for scripts in the com.mycompany package will look for the properties file com/mycompany/resources/MyResources_xx.fxproperties, instead of using the default that uses the script name. Again, xx is replaced with the locale abbreviation codes.