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 institution-wide 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 application-supplied 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 customize 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 package 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. [Support for JNLS_ROOT isn't yet implemented in my code.] Also, some of the features of the language database are designed to support the jcommand.tcl and jmenu.tcl libraries.
The ::jldb::init procedure handles initializing the in-memory national language database based on the user's preferred language (stored in the Tcl global variable J_PREFS(language)). It searches a sequence of directories for language database files, and depending on the user's language preference, it may read in more than one file in a given directory (en.uk as well as en, for instance). See The Language Database Mechanism for a more detailed description of location and order of the files read when loading in a language database, and how users can customize the strings your application uses.
The jldb package does not rely on Tk, and although some of its features are aimed at localizing GUIs, it can be used in pure-Tcl applications as well.
This document describes version 0.2 of the jldb package.
package require jldband then invoke ::jldb::init with appropriate arguments to tell the package the name of your application and what language to use.
If you wish, you can execute
namespace import ::jldb::*and then use the names of jldb procedures without the leading `::jldb::'. Because the fully-qualified names of the jldb procedures are long, and they're likely to be used very frequently in your code, you may instead (or as well) wish to execute
namespace import ::jldb::shortcuts::*which gives you very short (one-character) synonyms for some of the jldb procedures.
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.
The language parameter is a language specifier as described above under Introduction; for example, a two-letter ISO language code such as `en' for English or `fr' for French. If language is not given, a default language is used. (As the package is distrubuted, this is English, but it can be changed by the installer.) Generally, this should be provided by the user via some sort of preferences mechanism.
The db_root parameter is the pathname to the directory tree containing the language database files - this is where jldb will look for site-wide language database files. (See The Language Database Mechanism for a description of the structure of this tree.) This is generally not needed; if it is omitted, an installation-specific directory is used.
(If you use the jstools libraries and call j:jstools_init, then you don't need to call this procedure explicitly; it's called for you.)
This procedure can be called more than once, which lets you re-read a database that has changed on disk or switch languages while an application is running. Also, you can load in databases for more than one app; for instance, the jstools initialization procedure used by all the jstools applications loads in a database for jstools as well as a database for the particular application (like jedit or jdoc). That way some natural-language strings can be shared across applications, and others can be application-specific.
namespace import ::jldb::shortcuts::*
button .b -command cmd:print \
-text [= aliases:print "Drucken"]
namespace import ::jldb::shortcuts::*
puts [= {Please pick a number between one and ten.}]
No substitutions are performed before the string is returned; if changable information such as the name of a file or a number must be part of the string, you may wish to use the Tcl subst command to expand variable names or Tcl commands (in square brackets) in the resulting string.
If your keys are human-readable, 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 ::jldb::long_text, or by calling ::jldb::set_defaults with a set of default strings before calling ::jldb::init, in case no language databases are installed.
You can use the returned string in any way you like; it doesn't have to be presented to the user directly. 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 natural-language string database; it would probably be in English in all the databases (because the system colour names are English), but it might be a different colour in different databases. Alternatively, the value could be the filename of an image.
(I expect that most people will want to use the short form `=' which you can use after executing `namespace import ::jldb::shortcuts::*' rather than the fully spelled out version.)
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.
(I expect that most people will want to use the short form `-' which you can use after executing `namespace import ::jldb::shortcuts::*' rather than the fully spelled out version.)
The procedure returns -1 if no underline position is specified in the current natural-language database for the given key. You can safely use the return value of ::jldb::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 ::jldb::set_strings or ::jldb::set_defaults), ::jldb::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.
(In the jstools libraries, 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 natural-language database for the given key.
There's currently no way to provide a default 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 ::jldb::set_strings or ::jldb::set_defaults), ::jldb::binding won't return a valid event sequence (it'll return {}); you need to check for that before using the result in a bind command.
(In the jstools libraries, 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 human-readable accelerator label for key in the current database, this procedure returns the result of ::jldb::binding, which normally produces the actual Tk event specification, as described in Tk's bind(n) manual page. It's generally better to provide a concise accelerator label, because the Tk-format event specifications tend to be fairly long.
The procedure returns {} (the null string) if no binding is set in the current natural-language 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.)
(In the jstools libraries, this procedure is used by the j:menu:commands procedure in the jmenu.tcl library to indicate accelerator bindings visually in menus.)
In your own code, you should use ::jldb::set_defaults instead. (Using ::jldb::set_strings would prevent your applications from being localized.)