cbi18n
Search…
Configuration

Global Configuration

The module can be configured by adding a cbi18n key in the moduleSettings structure within the config/Coldbox.cfc
config/Coldbox.cfc
1
moduleSettings = {
2
3
cbi18n = {
4
// The default resource to load and aliased as `default`
5
"defaultResourceBundle" : "includes/i18n/main",
6
// The locale to use when none defined
7
"defaultLocale" : "en_US",
8
// The default storage for the locale
9
"localeStorage" : "[email protected]",
10
// What to emit to via the resource methods if a translation is not found
11
"unknownTranslation" : "**NOT FOUND**",
12
// If true, we will log to LogBox the missing translations
13
"logUnknownTranslation" : true,
14
// A-la-carte resources to load by name
15
"resourceBundles" : {},
16
// Your own CFC instantiation path
17
"customResourceService" : ""
18
}
19
20
};
Copied!

Configuration Settings Explained

Key
Type
Required
Default
Description
defaultResourceBundle
string
no
This is the path for a resource bundle that will be treated as the default resource bundle with an alias of default . The path must NOT include language or variants, just the name of path prefixincludes/i18n/main_en_US.properties should be specified as includes/i18n/main
resourceBundles
struct
no
{}
key-value struct of resource alias name and bundle path without the lang_COUNTRY.(properties|json) part.
defaultLocale
string
no
en_US
default locale
localeStorage
string
no
cbstorages service where current locale is stored
unknownTranslation
string
no
if no unknowTranslation is set getResource will fail on unknown resourceKeys
logUnknownTranslation
boolean
no
false
will log unknown translations to logbox
WARNING
When specifying resources in a MODULE, you should never specify a defaultResourceBundle, because it will conflict with your main settings. If you specify additional resourceBundles it is wise to choose an aliasname which will not conflict with other modules or your main settings
A resource bundle can be a .properties file containing your translations (java resources) OR a .json file. For instance, using the default settings above, you would need to create a includes/i18n/main_en_US.properties file with your translations. If you want to use JSON files, you should use an includes/i18n/main_en_US.json file.
ColdBox will auto-detect the extension of the resource bundle and deal with it appropriately. The supported extension types are:
  • .properties - Java resource bundle
  • .json - JSON resource bundle (flat or nested)
Java Properties
includes/i18n/main_en_US.properties
1
main.readthedocs=Read The Docs!
2
greeting=Hello {name}
Copied!

JSON Flat

includes/i18n/jsontest_en_US.json
1
{
2
"sub.IntroMessage": "Normal JSON"
3
}
Copied!
includes/i18n/jsontest_es_SV.json
1
{
2
"sub.IntroMessage": "JSON Normal"
3
}
Copied!

JSON Nested

includes/i18n/nested_en_US.json
1
{
2
"sub": {
3
"IntroMessage": "Nested JSON"
4
}
5
}
Copied!
includes/i18n/nested_es_SV.json
1
{
2
"sub": {
3
"IntroMessage": "JSON Incrustado"
4
}
5
}
Copied!

ColdBox Module Configuration

Every ColdBox module has the i18n capabilities available to them as well. They can use it to register their own resource bundles. The previous version allowed for a setting called i18n this is now called cbi18n to comply with the same global naming convention. Just update your key root in your Moduleconfig.cfc.
ModuleConfig.cfc
1
cbi18n = {
2
resourceBundles = {
3
"bundleIdentifier" = "#moduleMapping#/includes/module"
4
}
5
};
Copied!
The keys in the resourceBundles struct represent the bundle that can be passed to getResource or appended on the resource string with an @ sign.
1
i18n.getResource( "[email protected]" );
2
// same as
3
i18n.getResource( resource = "myTranslationKey", bundle = "bundleIdentifier" );
Copied!

Configuration With No Resource Bundles

You can also use the config declaration for just tracking user locales and no resource bundles support
1
//i18n & Localization
2
i18n = {
3
defaultLocale = "en_es",
4
localeStorage = "[email protected]"
5
};
Copied!
You eliminate the resource bundle element or leave it blank. The framework will not load the resource bundles.
IMPORTANT All language resource bundles are stored in the configuration structure of your application and are lazy loaded in. So if a language is not used, then it does not get loaded. This is separate to where the user's locale information is stored.
Last modified 2mo ago