Interface LocalizationSourceRegistry

All Superinterfaces:
LocalizationSource

public interface LocalizationSourceRegistry extends LocalizationSource
A mutable localization source that can register localized strings.
  • Method Details

    • create

      static LocalizationSourceRegistry create(Locale defaultLocale)
      Creates a new LocalizationSourceRegistry instance.
      Parameters:
      defaultLocale - the default locale of the localization source
      Returns:
      a new LocalizationSourceRegistry instance
    • registerAll

      default void registerAll(Locale locale, Map<String,MessageFormat> formats)
      Registers a map of localized strings.
       
            final var strings = new HashMap<String, MessageFormat>();
            strings.put("example.hello", new MessageFormat("Hello {0}!", Locale.ENGLISH));
            strings.put("example.goodbye", new MessageFormat("Goodbye {0}!", Locale.ENGLISH));
            registry.registerAll(Locale.ENGLISH, strings);
        
      Parameters:
      locale - the locale to register the strings to
      formats - the map of localized strings
      Throws:
      IllegalArgumentException - if a key is already registered
    • registerAll

      default void registerAll(Locale locale, ResourceBundle bundle)
      Registers a resource bundle of localized strings.
      Parameters:
      locale - the locale to register the strings to
      bundle - the resource bundle to use
      Throws:
      IllegalArgumentException - if a key is already registered
    • registerAll

      default void registerAll(Locale locale, String baseName, ClassLoader loader)
      Registers a resource bundle of localized strings via the classpath.
       
            final Plugin plugin = ...;
            registry.registerAll(Locale.ENGLISH, "bundle", plugin.getClass().getClassLoader());
            registry.registerAll(Locale.FRENCH, "bundle", plugin.getClass().getClassLoader());
        
      Parameters:
      locale - the locale to register the strings to
      baseName - the base name of the resource bundle
      loader - the class loader to use
      Throws:
      IllegalArgumentException - if a key is already registered
    • registerAll

      default void registerAll(Locale locale, Path path) throws IOException
      Registers a resource bundle of localized strings via a file system.
       
            final var english = Paths.get("bundle_en.properties");
            registry.registerAll(Locale.ENGLISH, path);
            final var english = Paths.get("bundle_fr.properties");
            registry.registerAll(Locale.FRENCH, path);
        
      Parameters:
      locale - the locale to register the strings to
      path - the path to the bundle file
      Throws:
      IllegalArgumentException - if a key is already registered
      IOException
    • registerAll

      default void registerAll(Locale locale, Set<String> keys, Function<String,MessageFormat> function)
      Registers a set of localized strings by using a mapping function to obtain each string.
      Parameters:
      locale - the locale to register the strings to
      keys - the set of keys to register
      function - the mapping function
      Throws:
      IllegalArgumentException - if the key is already registered
    • register

      void register(String key, Locale locale, MessageFormat format)
      Registers a localized string.
      Parameters:
      key - the key of the string
      locale - the locale to register the string to
      format - the localized string
      Throws:
      IllegalArgumentException - if the key is already registered
    • registered

      boolean registered(String key)
      Checks if a key is already registered, for any locale.
      Parameters:
      key - the key to check
      Returns:
      true if the key is already registered, false otherwise
    • registered

      boolean registered(String key, Locale locale)
      Checks if a key is already registered for a specific locale.
      Parameters:
      key - the key to check
      locale - the locale to check
      Returns:
      true if the key is already registered for the specific locale, false otherwise
    • unregister

      void unregister(String key)
      Unregisters a localized string.
      Parameters:
      key - the key of the string
    • getDefaultLocale

      Locale getDefaultLocale()
      Returns the default locale of this source.