开发者

Is it possible to fallback to a macrolanguage for a resourcebundle in Java (e.g. nynorsk -> norsk)

开发者 https://www.devze.com 2023-02-17 10:46 出处:网络
A web browser can request a language using the accept-language header and list a number of languages. Safari only lists one language on OSX, the current language set in the operating system. Now, this

A web browser can request a language using the accept-language header and list a number of languages. Safari only lists one language on OSX, the current language set in the operating system. Now, this is a problem when it comes to Norwegian. Norwegian has three language codes in ISO 639-1 (nb, nn, no). nb is Bokmål, nn is Nynorsk and no is a "macrolanguage" that groups the two.

If we only have a Bokmål l10n it would be nice to offer the Bokmål version to someone requesting Nynorsk rather than falling back to the default language. It seems logically to me that it should be possible to resolve a "best match" server side if a perfect match is not availabl开发者_StackOverflow中文版e (e.g. it seems like a minor leap that nb and nn would both fall back to no). As it stands now the onus is on client side to offer a list of accepted languages.

The application is in JSP, and uses JSTL to localize Strings which are ultimately resolved to resource bundles.

The same problem would apply to other macrolanguages that exist in written form.

So, how to handle this without duplicating language resources?


Create a class resource bundle for the Nynorsk language, and set its parent bundle to the Bokmal bundle. See http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader%29 for explanations on how the bundles are loaded :

public class foobar_nn extends ListResourceBundle {
    public foobar_nn {
        setParent(ResourceBundle.getBundle("foobar", new Locale("nb")));
    }

    @Override 
    protected Object[][] getContents()  {
        return new Object[0][0];
    }
}

Or just change your build process so that every xxx_nb.properties file is copied to xxx_nn.properties.


So I solved it in a generic way that lets me define "fallback" languages server side, centrally for all webapps.

What I do is that I wrap the HttpServletRequest and intercept getAttribute for javax.servlet.jsp.jstl.fmt.localizationContext.request. It is normally a String (the basename of the ResourceBundle), but you can resolve it and replace it with a LocalizationContext which will then be used by the fmt:message tags. When resolving it you pass a ResourceBundle.Control the implements getFallbackLocale that does the appropriate mapping.

Now, looking at the list of macrolanguages I don't see any other obvious languages where this would be common (I doubt people surf in a local Arabic dialect for example, but now I am prepared :), but in Norway it should be unavoidable, both Bokmål and Nynorsk are, to my understanding in common use on the web, and there must be plenty of web sites that are only localized to one of them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号