Verified Owner
Top Description Inners Fields Constructors Methods
org.jabref.logic.l10n

public Class Localization

extends Object
Class Inheritance
Annotations
@AllowedToUseStandardStreams:Needs to have acess to System.err because it\'s called very early before our loggers
Imports
java.util.ArrayList, .Arrays, .Collections, .Enumeration, .HashMap, .Locale, .Map, .MissingResourceException, .Objects, .Optional, .ResourceBundle, .Set, java.util.stream.Collectors, org.jabref.architecture.AllowedToUseStandardStreams, org.jspecify.annotations.NonNull, org.slf4j.LoggerFactory

Provides handling for messages and menu entries in the preferred language of the user.

Notes

All messages and menu-entries in JabRef are stored in escaped form like "This_is_a_message". This message serves as key inside the l10n properties files that hold the translation for many languages. When a message is accessed, it needs to be unescaped and possible parameters that can appear in a message need to be filled with values.

This implementation loads the appropriate language by importing all keys/values from the correct bundle and stores them in unescaped form inside a LocalizationBundle which provides fast access because it caches the key-value pairs.

The access to this is given by the functions lang(String, Object...) and that developers should use whenever they use strings for the e.g. GUI that need to be translatable.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Localization.LocalizationBundle

A bundle for caching localized strings.

Field Summary

Modifier and TypeField and Description
private static Locale
private static Localization.LocalizationBundle
pack-priv static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Map<String, String>

Returns:

Lookup map for the baseBundle.
createLookupMap
(ResourceBundle
JabRef language bundle with keys and values for translations.
baseBundle
)

Helper function to create a Map from the key/value pairs of a bundle.

private static void
createResourceBundles(Locale
Localization to use.
locale
)

Creates and caches the language bundles used in JabRef for a particular language.

public static Localization.LocalizationBundle

Returns:

The internally cashed bundle.
getMessages
()

Returns the messages bundle, e.g. to load FXML files correctly translated.

public static String

Returns:

The message with replaced parameters
lang
(String
The key of the message in unescaped form like "All fields"
key
,
Object...
Replacement strings for parameters %0, %1, etc.
params
)

Public access to all messages that are not menu-entries

private static String

Returns:

The final message with replaced parameters.
lookup
(@NonNull Localization.LocalizationBundle bundle, @NonNull String
The lookup key.
key
,
String...
The parameters that should be inserted into the message
params
)

This looks up a key in the bundle and replaces parameters %0, ..., %9 with the respective params given.

public static void
setLanguage(Language
Language identifier like "en", "de", etc.
language
)

Sets the language and loads the appropriate translations.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

localeback to summary
private static Locale locale
localizedMessagesback to summary
private static Localization.LocalizationBundle localizedMessages
RESOURCE_PREFIXback to summary
pack-priv static final String RESOURCE_PREFIX

Constructor Detail

Localizationback to summary
private Localization()

Method Detail

createLookupMapback to summary
private static Map<String, String> createLookupMap(ResourceBundle baseBundle)

Helper function to create a Map from the key/value pairs of a bundle.

Parameters
baseBundle:ResourceBundle

JabRef language bundle with keys and values for translations.

Returns:Map<String, String>

Lookup map for the baseBundle.

createResourceBundlesback to summary
private static void createResourceBundles(Locale locale)

Creates and caches the language bundles used in JabRef for a particular language. This function first loads correct version of the "escaped" bundles that are given in l10n. After that, it stores the unescaped version in a cached LocalizationBundle for fast access.

Parameters
locale:Locale

Localization to use.

getMessagesback to summary
public static Localization.LocalizationBundle getMessages()

Returns the messages bundle, e.g. to load FXML files correctly translated.

Returns:Localization.LocalizationBundle

The internally cashed bundle.

langback to summary
public static String lang(String key, Object... params)

Public access to all messages that are not menu-entries

Parameters
key:String

The key of the message in unescaped form like "All fields"

params:Object[]

Replacement strings for parameters %0, %1, etc.

Returns:String

The message with replaced parameters

lookupback to summary
private static String lookup(@NonNull Localization.LocalizationBundle bundle, @NonNull String key, String... params)

This looks up a key in the bundle and replaces parameters %0, ..., %9 with the respective params given. Note that the keys are the "unescaped" strings from the bundle property files.

Parameters
bundle:@NonNull Localization.LocalizationBundle

The LocalizationBundle which is usually Localization#localizedMessages.

key:@NonNull String

The lookup key.

params:String[]

The parameters that should be inserted into the message

Returns:String

The final message with replaced parameters.

setLanguageback to summary
public static void setLanguage(Language language)

Sets the language and loads the appropriate translations. Note, that this function should be called before any other function of this class.

Parameters
language:Language

Language identifier like "en", "de", etc.

org.jabref.logic.l10n back to summary

private Class Localization.LocalizationBundle

extends ResourceBundle
Class Inheritance

A bundle for caching localized strings. Needed to support JavaFX inline binding.

Field Summary

Modifier and TypeField and Description
private final Map<String, String>
Inherited from java.util.ResourceBundle:
parent

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
containsKey(String key)

Overrides java.util.ResourceBundle.containsKey.

Determines whether the given key is contained in this ResourceBundle or its parent bundles.

public Enumeration<String>
getKeys()

Implements abstract java.util.ResourceBundle.getKeys.

Returns an enumeration of the keys.

public final Object
handleGetObject(@NonNull String key)

Implements abstract java.util.ResourceBundle.handleGetObject.

Gets an object for the given key from this resource bundle.

protected Set<String>
handleKeySet()

Overrides java.util.ResourceBundle.handleKeySet.

Returns a Set of the keys contained only in this ResourceBundle.

Inherited from java.util.ResourceBundle:
clearCacheclearCachegetBaseBundleNamegetBundlegetBundlegetBundlegetBundlegetBundlegetBundlegetBundlegetBundlegetLocalegetObjectgetStringgetStringArraykeySetsetParent

Field Detail

lookupback to summary
private final Map<String, String> lookup

Constructor Detail

LocalizationBundleback to summary
pack-priv LocalizationBundle(Map<String, String> lookupMap)

Method Detail

containsKeyback to summary
public boolean containsKey(String key)

Overrides java.util.ResourceBundle.containsKey.

Doc from java.util.ResourceBundle.containsKey.

Determines whether the given key is contained in this ResourceBundle or its parent bundles.

Annotations
@Override
getKeysback to summary
public Enumeration<String> getKeys()

Implements abstract java.util.ResourceBundle.getKeys.

Doc from java.util.ResourceBundle.getKeys.

Returns an enumeration of the keys.

Annotations
@Override
handleGetObjectback to summary
public final Object handleGetObject(@NonNull String key)

Implements abstract java.util.ResourceBundle.handleGetObject.

Doc from java.util.ResourceBundle.handleGetObject.

Gets an object for the given key from this resource bundle. Returns null if this resource bundle does not contain an object for the given key.

Annotations
@Override
handleKeySetback to summary
protected Set<String> handleKeySet()

Overrides java.util.ResourceBundle.handleKeySet.

Doc from java.util.ResourceBundle.handleKeySet.

Returns a Set of the keys contained only in this ResourceBundle.

The default implementation returns a Set of the keys returned by the getKeys method except for the ones for which the handleGetObject method returns null. Once the Set has been created, the value is kept in this ResourceBundle in order to avoid producing the same Set in subsequent calls. Subclasses can override this method for faster handling.

Annotations
@Override