Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
How to use one ResourceBundleProvider for all resource bundles in all Modules?

I am developing a multi-module Java application. Each module contains several resource bundles: when a class Demo.java requires translations, a resource bundle Demo.properties in the same package is created. As the application is not supposed to crash when a translation is missing, I want the translation key to be used as a fallback.

Since Java 9, a ResourceBundleProvider should be the right way to implement custom resource bundles:

package my.app.core;

public class KeyFallbackResourceBundleProvider extends AbstractResourceBundleProvider {

    @Override
    public ResourceBundle getBundle(String baseName, Locale locale) {
        var bundle = super.getBundle(baseName, locale);
        return new ResourceBundle() {

            @Override
            protected Object handleGetObject(String key) {
                try {
                    return bundle.getObject(key);
                } catch (MissingResourceException e) {
                    return key;
                }
            }

            @Override
            public Enumeration<String> getKeys() {
                return bundle.getKeys();
            }
        };
    }
}

Now, I need ResourceBundle#getBundle(String) to use that provider in every class that loads a resource bundle. It seems, however, that the name the provider is supposed to have depends on the name of the bundle to load, which is not acceptable for my use case. In addition, I want to use the same provider for all modules (which can directly depend on it).

Is there a way of achieving this without having to implement a ResourceBundleProvider for every resource bundle? And if not, is there any other way to implement the key fallback mechanism I described?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda