0

I am building an app that mostly provide REST services, nothing fancy. since my data consumed by the app can have multiple languages I thought about using the bundle files.

I created 3 files, one with the default file name and another two with specific languages. The files created using intellij IDE I am using.

I followed this guide https://www.baeldung.com/java-resourcebundle however on each run I am getting:

MissingResourceException: Can't find bundle for base name tp_app_strings, locale en_US

I tried numerous articles but none of them seems to resolve the issue.

One fun fact is that if I am using the @Value("classpath:tp_app_strings.properties") on a 'Resource' field I am able to get a reference to that file, so it spring is able to find it.

Additional thing that I tried was to create a WEB-INF directory and place the files there (read it in some article) but still no positive affect

The project structure is quite straight forward:

enter image description here

Spring boot version 2.2 running tomcat.

Any suggeestions would be highly appriciated

Sachith Dickwella
  • 1,388
  • 2
  • 14
  • 22
user2145673
  • 363
  • 1
  • 9
  • 23

2 Answers2

1

You can load the .properties file to the application context using @PropertySource annotation instead using @Value to load the .properties file to a org.springframework.core.io.Resource instance.

The usage;

@Configuration
@PropertySource("classpath:tp_app_strings.properties")
public class DefaultProperties {

    @Value("${property1.name}") // Access properties in the above file here using SpringEL.
    private String prop1;

    @Value("${property2.name}")
    private String prop2;
}

You wouldn't need java.util.ResourceBundle access properties this way. Use different or same class to load other .properties files as well.

Update 1:

In order to have the functionality of java.util.ResourceBundle, you can't just use org.springframework.core.io.Resource class. This class or non of it sub-classes don't provide functions to access properties by its name java.util.ResourceBundle whatsoever.

However, if you want a functionality like java.util.ResourceBundle, you could implement something custom like this using org.springframework.core.io.Resource;

@Configuration
public class PropertyConfig {

    @Value("classpath:tp_app_strings.properties")
    private Resource defaultProperties;

    @Bean("default-lang")
    public java.util.Properties getDefaultProperties() throws IOException {
        Properties props = new Properties();
        props.load(defaultProperties.getInputStream());

        return props;
    }
}

Make sure to follow correct naming convention when define the property file as java.util.Properties#load(InputStream) expect that.

Now you can @Autowire and use this java.util.Properties bean wherever you want just like with java.util.ResourceBundle using java.util.Properties#getProperty(String) or its overloaded counterpart.

Sachith Dickwella
  • 1,388
  • 2
  • 14
  • 22
  • Already did that, the thing is that this method is not not scalable, what if you have 50 or more strings? You will need to create the same amount of fields. I need the same logic as ResouceBundle where I can get a string by key – user2145673 Feb 11 '20 at 15:57
  • I was finaly able to resolve this issue by using the ResourceUtils.getFile(path_to_resource) method, it seems it is more suitable for spring boot. The @value anontation does not seems to find the file from some reason. I ended up wtih one class having two methods, one for EN string and one for IW (in my case) – user2145673 Feb 24 '20 at 12:49
0

I think it's problem of you properties file naming convention. use underline "_" for specifying locale of file like

filename_[languageCode]_[regionCode]

[languageCode] and [regionCode] are two letters standard code that [regionCode] section is optional

about code abbrivation standard take a look on this question

in your case change file name to tp_app_strings_en_US.properties

Saeed Alizadeh
  • 1,417
  • 13
  • 23
  • Still the same issue: – user2145673 Feb 11 '20 at 05:33
  • Still the same issue, changing the file name did not help "status": 500, "error": "Internal Server Error", "message": "Can't find bundle for base name tp_app_strings, locale en_US", "trace": "java.util.MissingResourceException: Can't find bundle for base name tp_app_strings, locale en_US\r\n\tat – user2145673 Feb 11 '20 at 05:50