Class I18nResourceFinder


  • public class I18nResourceFinder
    extends Object
    This class can find i18n resources. After construction you can set properties with the set methods. Then call find(). If it returns true, you can get information about the found resource with the get methods.

    Example

    Find properties:

    • baseName: "messages"
    • userLocales: ["nl_NL"]
    • honorifics: true
    • extension: "properties"

    It will try the following resources, in this order:

    • messages_nl_NL_v.properties (if the locale contains a country; v = vos, t = tu)
    • messages_nl_NL.properties (if the locale contains a country)
    • messages_nl_v.properties
    • messages_nl.properties
    • messages_v.properties
    • messages.properties

    If nothing is found, it will try the following locales: en_GB, en_US, en.

    • Constructor Detail

      • I18nResourceFinder

        public I18nResourceFinder​(String baseName)
        Constructs a new i18n resource finder.
        Parameters:
        baseName - the base name of the resource file to find
    • Method Detail

      • setUserLocale

        public void setUserLocale​(Locale userLocale)
        Sets the preferred locale of the user. If no resource is found for this locale, the resource finder will try en_GB, en_US or en. The default is the locale of the system.
        Parameters:
        userLocale - the preferred locale of the user
        See Also:
        setUserLocales(List)
      • setUserLocales

        public void setUserLocales​(List<Locale> userLocales)
        Sets the preferred locales of the user. If no resource is found for these locales, the resource finder will try en_GB, en_US or en. The default is the locale of the system.
        Parameters:
        userLocales - the preferred locales of the user (at least one)
      • setHonorifics

        public void setHonorifics​(boolean honorifics)
        Sets whether the resource should use honorifics. This is true for vos (u, vous, Sie) in tu-vos distinction, and false for tu (jij, tu, du). For languages without honorifics, such as English, there will be no resources with tu-vos designation, so the value of this property is not relevant. The default is true.
        Parameters:
        honorifics - true if the resource should use honorifics, false otherwise
      • setExtension

        public void setExtension​(String extension)
        Sets the extension of the resource file to find. You can set this to null if the file has no extension. The default is "properties".
        Parameters:
        extension - the extension of the resource file to find or null
      • getExtension

        public String getExtension()
        Returns the extension of the resource file to find. This can be null if the file has no extension. The default is "properties".
        Returns:
        the extension of the resource file to find or null
      • setResourceLocator

        public void setResourceLocator​(ResourceLocator resourceLocator)
        Sets the resource locator that should be used to find the resource. The default is a ClassLoaderResourceLocator that locates resources from the class loader.
        Parameters:
        resourceLocator - the resource locator
      • find

        public boolean find()
        Tries to find a resource matching the specified properties. If a resource is found, this method will return true and you can get details with the get methods.
        Returns:
        true if a resource was found, false otherwise
      • getLocale

        public Locale getLocale()
        You can call this method after find() returned true. It returns the locale of the found resource. This may be different than the preferred locale, because the resource finder also tries en_GB, en_US and en.
        Returns:
        the locale of the found resource
      • getName

        public String getName()
        You can call this method after find() returned true. It returns the name of the found resource.
        Returns:
        the name of the found resource
      • openStream

        public InputStream openStream()
                               throws IOException
        You can call this method after find() returned true. It opens the resource stream.
        Returns:
        the input stream for the resource
        Throws:
        IOException - if the resource can't be opened