The library supports several `levels' of localisation; a sequence of string databases, ranging from most specific to least specific, can be consulted for a particular string, and the first string found will be displayed. For instance, if the user specifies hir language as `en.us.chs.jody', then each string will first be looked up in a database with that name (which would allow Jody to override a few particular strings in the application), then (if the string was not found in the en.us.chs.jody database) it would be looked up in the en.us.chs database (which might override certain strings on an institutionwide basis), then if necessary it would be looked up in the en.us database, then in the en database, and if it were still not found, an applicationsupplied default would be used. That way, the application author can supply (for instance) databases en (for English of some variety or another) and fr (for French of some variety or another), and a local installer can augment them with databases en.uk or fr.ch (for U.K. English, or Swiss French, respectively), overriding only those strings which are distinctive in those varieties of the language, and the user can further customise the strings displayed by the application.
(Actually, the description above is a simplification; an application reads all the databases in when it starts up, from least specific to most specific, rather than looking up each string on use in a series of databases, from most specific to least specific, but the net effect is the same.)
The jldb.tcl library doesn't depend on any of the other jstools libraries, so you can use it independently. However, you'll need to make sure the global variable J_PREFS(language) is set to the user's preferred language database, and the global variable JNLS_ROOT is set appropriately. (See j:ldb:read_database for more information on the latter.) Also, some of the features of the language database are designed to support the jcommand.tcl and jmenu.tcl libraries.
This document describes version 4.1/4.2 of the jldb.tcl library. I don't anticipate any nonbackwardscompatible changes to the jldb.tcl library (although I expect changes in how other parts of the jstools libraries use it). So you can use jldb.tcl as described in this document, and future versions won't break your code.
The jldb.tcl library is distributed as part of the jstools package.
The argument app is normally the name of the application, such as `jdoc' or `jedit', but it can be any identifier; it is used in constructing the path to the language database file that will be loaded.
If you call j:jstools_init, then you don't need to call this procedure explicitly; it's called for you.
You should make sure your application provides access to the jstools Global Preferences panel (via j:global_pref_panel), or provide some other mechanism for setting J_PREFS(language), so that your users have an opportunity to choose a language.
.menu.fichier.m add command \
-label [j:ldb Sauvegarder...] \
-command sauvegarder
button .b -command cmd:print \
-text [j:ldb aliases:print "Drucken"]
j:alert -text \
[j:ldb file_not_writable {File $file is not writable.}]
Backslash, variable, and command substitution, triggered by `\', `$', and `[...]', respectively, are performed on the returned string, whether it was found in the database or provided as default, as with Tcl 7.4's subst(n) command (although this is done under Tcl 7.3 as well). It follows that if you wish to use any of these special characters literally in default (or in a database entry) you must escape it with a backslash; it's not enough just to quote default with braces.
If your keys are humanreadable, then you can probably get by without specifying a default; if you use keys that aren't appropriate for display to the user, you should specify a default value for each key, either by using the default argument each time you call j:ldb, or by calling j:ldb:set_defaults with a set of default strings before calling j:ldb:init, in case no language databases are installed.
If language is specified (and nonnull), it is used as a language database name for looking up strings instead of the currentlyactive language databases. (The only practical use I can think of this would be a panel listing localised terms along with their corresponding equivalents in another language, perhaps the application author's, but it was easy to implement.)
The fact that variable (and command) substitution is performed on the returned strings makes it possible to generate complex strings including variables. You can't easily do this with straight string lookup, because word order varies from language to language. (However, it also means that if you change the name of a variable in a procedure, you may also need to change it in string databases; it's unfortunate that strings in the database can depend on details of the implementation of the procedure they're used in.)
Also, note that you can use the returned string in any way you like. For instance, you might want to use different colours for a certain interface element, depending on the user's language. You could look the colour up in the naturallanguage string database; it would be in English in all the databases (because the X Window System colour names are English), but it might be a different colour in different databases.
The intention is that keys in the database with the prefix `SHORT-' will be shorter versions of the corresponding messages without that prefix, which are suitable for use in contexts where space is at a premium, such as buttons or status labels. The plain keys specify fuller versions of the messages which are used when conservation of space isn't so important, as in menu entries or toplevel notification panels.
The procedure returns -1 if no underline position is specified in the current naturallanguage database for the given key. You can safely use the return value of j:ldb:underline as the argument to a -underline widget option without knowing whether there's an underline position specified in the current database, because the value -1 causes Tk to draw no underline.
There's currently no way to provide a default underline position to be used if none is found in the current database. That means that if no language database is installed for your application, and your application doesn't explicitly set defaults (with j:ldb:set_strings or j:ldb:set_defaults), j:ldb:underline won't return a valid underline position (it'll return -1), which means that unless you explicitly check for that underlines won't be drawn in your application.
This procedure is used by the jmenu.tcl library to underline shortcut keys in menubutton names and menu entries.
The procedure returns {} (the null string) if no event specification is set in the current naturallanguage database for the given key. (It works fine to give a null string to the -accelerator option to a menu entry, though, so your code doesn't need to know whether there's an accelerator entry in the database for a particular key or not.)
There's currently no way to provide a default accelerator event specification to be used if none is found in the current database. That means that if no language database is installed for your application, and your application doesn't explicitly set defaults (with j:ldb:set_strings or j:ldb:set_defaults), j:ldb:accelerator won't return a valid accelerator (it'll return {}).
This procedure is used by the j:command:bind procedure in the jcommand.tcl library to bind accelerator keystrokes for user commands.
If there's no humanreadable accelerator label for key in the current database, this procedure returns the result of j:ldb:binding, which normally produces the actual Tk event specification, as described in Tk's bind(n) manual page.
The procedure returns {} (the null string) if no binding is set in the current naturallanguage database for the given key. (It works fine to give a null string to the -accelerator option to a menu entry, though, so your code doesn't usually need to know whether there's an accelerator entry in the database for a particular key or not.)
This procedure is used by the j:menu:commands procedure in the jmenu.tcl library to indicate accelerator bindings visually in menus.
The argument app is normally the name of the application, such as `jdoc' or `jedit', but it can be any identifier; it is used in constructing the path to the language database file that will be loaded.
Note that this only reads in a single database; if you issue `j:ldb:read_database coffeemaker en.uk', strings will be read in from the en.uk database (United Kingdom English) if it exists, but not from en (generic English). It's more typical to call j:ldb:read_database_recursively. Moreover, you don't normally need to call either of these procedures yourself; if you call j:ldb:init, the appropriate databases for the user's preferred language will be read in.
The path used to look for the database is as follows:
$JNLS_ROOT/app/db
$JNLS_ROOT/default/db
~/.tk/jldb/app/db
~/.tk/jldb/default/db
where JNLS_ROOT is a Tcl global variable. (The first database found in that list is used.) If JNLS_ROOT is undefined when the jldb.tcl library is loaded, it defaults to $jstools_library/jldb, or /usr/local/jstools/jldb if jstools_library is undefined.
The argument app is normally the name of the application, such as `jdoc' or `jedit', but it can be any identifier; it is used in constructing the path to the language database file that will be loaded.
You don't normally need to call this yourself; if you call
j:ldb:init, the appropriate databases for the user's preferred language
will be read in (using this procedure).
In actual code, you would normally use j:ldb:set_defaults instead.
* The mechanism for supplying parameters (doing variable and command substitution before a string is returned by j:ldb) has the advantage of simplicity and power, but it requires information about the internal structure of the calling procedure and/or application (specifically, what variables and procedures are available) to be embedded in the naturallanguage database. This is unfortunate, but I couldn't think of a sufficiently flexible alternative. The positionbased substitution provided by POSIX' printf(3) isn't sufficiently flexible, because in some languages you might need to inflect words as they're inserted.
* It would be nice if the preferred database and the systemwide root of the database directory tree could be specified as environment variables, especially for people not using other jstools libraries.