MissingResourceException for the vietnamese locale

148 Views Asked by At

I am trying to use ResourceBundle to read data from a ".properties" file but it cannot be loaded. I receive this error:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name language_Vi, locale vi

Code which collects the properties

// Vietnamese login version
private static void VietnameseLogin() { 
    Locale locale = new Locale("Vi"); 
    ResourceBundle bundle = ResourceBundle.getBundle("language", locale); 
    Login(bundle); 
}

// English login version
private static void EnglishLogin() {
    Locale locale = Locale.ENGLISH;
    ResourceBundle bundle = ResourceBundle.getBundle("language", locale);
    Login(bundle); 
} 

Directory structure

file structure

1

There are 1 best solutions below

2
DuncG On BEST ANSWER

The names of language should be lowercase and variants are uppercase - so be precise with name of the files: language_en.properties and language_vi.properties. It is a good idea to keep your preferred language file as the fallback so rename one of the above to just language.properties without the 2 digit iso country code.

The following code would search for resource bundle named "language" as class and properties definitions in the top level of every classpath jar and directory:

// DEPRECATED IN JDK19, use Locale.of("vi")
Locale locale = new Locale("Vi"); 

ResourceBundle bundle = ResourceBundle.getBundle("language",locale);

=> SEARCHES FOR:

Class language.class
resource language.properties
Class language_vi.class
resource language_vi.properties
PLUS class+properties for lang and lang+variants of Locale.getDefault() eg language_en and language_en_GB

Resource pathnames should not contain \\.If you wish to use a directory to keep them, use /. So this would search under a sub-folder:

ResourceBundle bundle = ResourceBundle.getBundle("mydir/name",locale);

=> SEARCHES FOR:

Class mydir/name.class
Resource mydir/name.properties
Class mydir/name_vi.class
Resource mydir/name_vi.properties
PLUS mydir/name class+properties for lang and lang+variants of Locale.getDefault() eg lmydir/name_en and mydir/name_en_GB