Java ResourceBundle locale() NULL 异常

Java ResourceBundle locale() NULL exception

我正在尝试在 java ResourceBundle 中动态设置语言环境,例如:
ResourceBundle rb = ResourceBundle.getBundle(pathAndFilename,locale());

我试图在我的以下代码中涵盖所有可能性,以检测浏览器语言并进行设置,如果捆绑包中不存在,则可以将 en_US 用作默认语言:

    private Locale setLanguage(){
        Locale locale = request.getLocale();
                if (locale.getLanguage().equals("ar")){             
                    locale = new Locale("ar", "AE");                    
                }
                else if (locale.getLanguage().equals("fa")){                    
                    locale = new Locale("fa", "IR");
                }
                else if (locale.getLanguage().equals("en")){                    
                    locale = new Locale("en", "US");
                }
                else{
                    locale = new Locale("en", "US");
                }
    return locale;
    }

    private Locale locale(){
        Locale locale=null;     
            if(country == null){
                locale = setLanguage();                 
            }else{          
                locale = new Locale(language, country);
            }
    return locale;
    }

但是方法 locale() 正在返回 null_NULL
请指教我哪里出错了?

我搜索了很多但找不到我的错误

尝试更改 setLanguage() 方法中的代码: 取自 Locale class

的 javaDoc

ISO 639 is not a stable standard— some languages' codes have changed. Locale's constructor recognizes both the new and the old codes for the languages whose codes have changed, but this function always returns the old code. If you want to check for a specific language whose code has changed, don't do:

if (locale.getLanguage().equals("he")) // BAD!
    ...

相反,做

if (locale.getLanguage().equals(new Locale("he").getLanguage()))
    ...

可能是您在以下方法中获取 "null" 作为值。所以将其更改为 if(country == null || country.equals("null")){:

private Locale locale(){
    Locale locale=null;     
        if(country == null || country.equals("null")){
            locale = setLanguage();                 
        }else{          
            locale = new Locale(language, country);
        }
return locale;
}

如果您仍然遇到问题,请告诉我