Repository for files organized by locale. I18N repository has multiple sets of files with identical names, grouped by
locale, see ASCII diagram below. Support for multiple locale can miss in which case this repository degenerates to a
standard filesystem directory.
Repository has a base directory that contains a number of locale directories. By convention uses locale language tag
- see
Locale#toLanguageTag(), to name these locale directories. Is not allowed to mix locale directory with
other sub-directories, that is, base directory should contain only valid locale directories. Files set are stored
into locale directory with optional sub-path.
Language tag should contain only lower case language and optional upper case country, separated by hyphen. Language
is encoded ISO 639 alpha-2 and for country uses ISO 3166 alpha-2. There is no support for locale variant, script or
extension.
BASE / LOCALEi / SUB-PATH / FILEi
/ FILEj
~
/ LOCALEj / SUB-PATH / FILEi
/ FILEj
In order to create an I18N repository one needs a configuration object, see
I18nRepository#I18nRepository(Config); in next section are described needed properties. To simplify
integration there is a dedicated configuration builder. Once instantiated, repository can create I18N files pool then
scan for files. If files pattern is provided scan only matching files. Scanning process takes care to initialize I18N
file locale from locale directory name.
If repository is created without multiple locale support, created I18N pool instance is single locale and I18N file
locale is not initialized and ignored by pool.
// create and configure I18N repository
ConfigBuilder builder = new I18nRepository.ConfigBuilder(repositoryPath);
I18nRepository repository = new I18nRepository(builder.build());
// create single or multiple locale I18N pool accordingly repository support
I18nPool pool = repository.getPoolInstance();
// traverses all files from repository, that is, from all locale
for(I18nFile i18nFile : repository) {
// create object instance and initialize it from file content
File file = i18nFile.getFile();
Template template = new Template(file);
// store created object instance on cache, bound to file name and detected locale
pool.put(file.getName(), template, i18nFile.getLocale());
}
Repository instance is configured using a
Config instance that is created by a builder, see
I18nRepository.ConfigBuilder. Configuration builder needs repository path and optional files pattern.
Repository path is a standard directories path but with ${locale} variable indicating locale directory position into
directories hierarchy, e.g. /var/www/vhosts/site/${locale}/emails/
. Directory path can be relative or
absolute. If files pattern is not provided accept all files.