Search code examples
hibernate-validator

Inject classloader to custom message interpolator to fetch resource bundles outside classpath


As part of internationalizing quarkus application, I placed resource bundle properties outside build classpath and now I need my custom resourcebundlemessage interpolator to look for bundles outside classpath by providing the classloader value. By default, message interpolator looks for classloader and since it is null, it is picking the default locale resource bundle. Could someone help me how should I inject classloader to custom message interpolator?


Solution

  • You probably want to use a custom ResourceBundleLocator:

    public class MyResourceBundleLocator implements ResourceBundleLocator {
        @Override
        public ResourceBundle getResourceBundle(Locale locale) {
            // Get the resource bundle from wherever you want.
            // Note that resources are retrieved at runtime,
            // so you can't access the source/build directories here.
            return <your result>;
        }
    }
    
    @ApplicationScoped
    public class MyValidatorFactoryCustomizer implements ValidatorFactoryCustomizer {
    
        @Override
        public void customize(BaseHibernateValidatorConfiguration<?> configuration) {
            configuration.messageInterpolator(new MyResourceBundleLocator());
        }
    }