org.nuiton.i18n.bundle.I18nBundleEntry Maven / Gradle / Ivy
/*
* *##% Lutin utilities library
* Copyright (C) 2004 - 2008 CodeLutin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* . ##%* */
package org.nuiton.i18n.bundle;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
import org.nuiton.i18n.I18nUtil;
import org.nuiton.i18n.I18nFileReader;
/**
* A class to represent an entry in a bundle.
*
* The object matches exactly one resource file in a given scope.
*
* The object has three properties :
*
* - {@link #path} : the path to resource file where to find transaltion for the entry.
*
- {@link #locale} : the locale of the entry
*
- {link #scope} ; the scope of the entry
*
* This object defines a equals order base on property {@link #path}.
*
* This object is {@link Comparable}, the order relation is defined like this :
*
* - sort first on {@link #scope}, in the scope order (see {@link I18nBundleScope}),
*
- if scopes are equals, sort on {@link #locale} string representation.
*
*
* @author chemit
* @see I18nBundleScope
*/
public class I18nBundleEntry implements Comparable {
/** path to resource file */
protected URL path;
/** local of the entry, can be null if general scope */
protected Locale locale;
/** scope of the entry */
protected I18nBundleScope scope;
/**
* Constructor if an bundle entry.
*
* It is defined by a path
of the resource file, a scope and a locale.
*
* @param path the path of the resource file fo the bundle entry
* @param locale the given locale of the bundle entry
* @param scope the scope of the given entry
*/
public I18nBundleEntry(URL path, Locale locale, I18nBundleScope scope) {
this.path = path;
this.locale = locale;
this.scope = scope;
}
public URL getPath() {
return path;
}
public Locale getLocale() {
return locale;
}
public I18nBundleScope getScope() {
return scope;
}
/**
* Method to match or not a bundle entry for a given scope and locale.
*
* We use the inclusive property of scope, means that we accept all entries on the path
* to the generalest entry for a givne locale.
*
* @param locale the locale to match
* @param scope the highest scope to match
* @return true
if the entry match the scope and locale
* *
*/
public boolean matchLocale(Locale locale, I18nBundleScope scope) {
if (this.locale == null) {
// a general bundle entry is always matched!
return true;
}
if (locale == null) {
// can not match a specialized entry with a general scope
return false;
}
// match full locale, or at least a language
return this.locale.equals(locale) ||
(this.scope.ordinal() < scope.ordinal() && locale.getLanguage().equals(this.locale.getLanguage()));
}
/**
* For a given language, load the resource file of this entry into the resource
* properties object.
*
* @param resource the save of resources already loaded
* @throws IOException if any pb while reading resource file
*/
public void load(Properties resource) throws IOException {
InputStream inputStream = null;
StringBuilder sb = new StringBuilder();
try {
I18nFileReader fileReader = new I18nFileReader();
inputStream = getPath().openStream();
//String encoding = language.getEncoding();
if (I18nBundle.log.isDebugEnabled()) {
sb.append(getPath()).append("\n");
}
// TC 20081117 always use ISO_8859_1_ENCONDING, since java does it like this.
fileReader.load(inputStream, I18nUtil.ISO_8859_1_ENCONDING);
if (I18nBundle.log.isDebugEnabled()) {
for (Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy