use of org.olat.core.util.SortedProperties in project OpenOLAT by OpenOLAT.
the class I18nManager method createNewLanguage.
public boolean createNewLanguage(String localeKey, String languageInEnglish, String languageTranslated, String authors) {
if (!i18nModule.isTransToolEnabled()) {
throw new AssertException("Can not create a new language when the translation tool is not enabled and the transtool source pathes are not configured! Check your olat.properties files");
}
if (i18nModule.getAvailableLanguageKeys().contains(localeKey)) {
return false;
}
// Create new property file in the brasato bundle and re-initialize
// everything
String coreFallbackBundle = i18nModule.getApplicationFallbackBundle();
File transToolCoreLanguagesDir = i18nModule.getTransToolApplicationOptLanguagesSrcDir();
String i18nDirRelPath = "/" + coreFallbackBundle.replace(".", "/") + "/" + I18nManager.I18N_DIRNAME;
File transToolCoreLanguagesDir_I18n = new File(transToolCoreLanguagesDir, i18nDirRelPath);
File newPropertiesFile = new File(transToolCoreLanguagesDir_I18n, I18nModule.LOCAL_STRINGS_FILE_PREFIX + localeKey + I18nModule.LOCAL_STRINGS_FILE_POSTFIX);
// Prepare property file
// Use a sorted properties object that saves the keys sorted alphabetically to disk
Properties newProperties = new SortedProperties();
if (StringHelper.containsNonWhitespace(languageInEnglish)) {
newProperties.setProperty("this.language.in.english", languageInEnglish);
}
if (StringHelper.containsNonWhitespace(languageTranslated)) {
newProperties.setProperty("this.language.translated", languageTranslated);
}
if (StringHelper.containsNonWhitespace(authors)) {
newProperties.setProperty("this.language.translator.names", authors);
}
OutputStream fileStream = null;
try {
// Create necessary directories
File directory = newPropertiesFile.getParentFile();
if (!directory.exists())
directory.mkdirs();
// Write to file file now
fileStream = new FileOutputStream(newPropertiesFile);
newProperties.store(fileStream, null);
fileStream.flush();
// Now set new language as enabled to allow user to translate the language.
Collection<String> enabledLangKeys = i18nModule.getEnabledLanguageKeys();
enabledLangKeys.add(localeKey);
// Reinitialize languages with new language
i18nModule.reInitializeAndFlushCache();
// Now add new language as new language (will re-initialize everything a second time)
i18nModule.setEnabledLanguageKeys(enabledLangKeys);
return true;
} catch (FileNotFoundException e) {
throw new OLATRuntimeException("Could not create new language file::" + newPropertiesFile.getAbsolutePath(), e);
} catch (IOException e) {
throw new OLATRuntimeException("Could not create new language file::" + newPropertiesFile.getAbsolutePath() + ", maybe permission denied? Check your directory permissions", e);
} finally {
try {
if (fileStream != null)
fileStream.close();
} catch (IOException e) {
log.error("Could not close stream after creating new language file::" + newPropertiesFile.getAbsolutePath(), e);
}
}
}
use of org.olat.core.util.SortedProperties in project openolat by klemens.
the class I18nManager method readPropertiesFile.
private Properties readPropertiesFile(Locale locale, String bundleName, String key, boolean logDebug) {
InputStream is = null;
Properties props = new SortedProperties();
try {
// Start with an empty property object
// Use a sorted properties object that saves the keys sorted alphabetically to disk
//
// 1) Try to load the bundle from a configured source path
// This is also used to load the overlay properties
File baseDir = i18nModule.getPropertyFilesBaseDir(locale, bundleName);
if (baseDir != null) {
File f = getPropertiesFile(locale, bundleName, baseDir);
// proceed with 2)
if (f.exists()) {
is = new FileInputStream(f);
if (logDebug)
log.debug("loading LocalStrings from file::" + f.getAbsolutePath(), null);
}
}
// resource will not be found within jars
if (is == null) {
String fileName = (locale == null ? METADATA_FILENAME : buildI18nFilename(locale));
String relPath = bundleName.replace('.', '/') + "/" + I18N_DIRNAME + "/" + fileName;
ClassLoader classLoader = this.getClass().getClassLoader();
is = classLoader.getResourceAsStream(relPath);
if (logDebug && is != null)
log.debug("loading LocalStrings from classpath relpath::" + relPath, null);
}
// langpacks)
if (is != null) {
props.load(is);
}
} catch (IOException e) {
throw new AssertException("LocalStrings for key::" + key + " could not be loaded", e);
} finally {
IOUtils.closeQuietly(is);
}
return props;
}
use of org.olat.core.util.SortedProperties in project OpenOLAT by OpenOLAT.
the class I18nManager method readPropertiesFile.
private Properties readPropertiesFile(Locale locale, String bundleName, String key, boolean logDebug) {
InputStream is = null;
Properties props = new SortedProperties();
try {
// Start with an empty property object
// Use a sorted properties object that saves the keys sorted alphabetically to disk
//
// 1) Try to load the bundle from a configured source path
// This is also used to load the overlay properties
File baseDir = i18nModule.getPropertyFilesBaseDir(locale, bundleName);
if (baseDir != null) {
File f = getPropertiesFile(locale, bundleName, baseDir);
// proceed with 2)
if (f.exists()) {
is = new FileInputStream(f);
if (logDebug)
log.debug("loading LocalStrings from file::" + f.getAbsolutePath(), null);
}
}
// resource will not be found within jars
if (is == null) {
String fileName = (locale == null ? METADATA_FILENAME : buildI18nFilename(locale));
String relPath = bundleName.replace('.', '/') + "/" + I18N_DIRNAME + "/" + fileName;
ClassLoader classLoader = this.getClass().getClassLoader();
is = classLoader.getResourceAsStream(relPath);
if (logDebug && is != null)
log.debug("loading LocalStrings from classpath relpath::" + relPath, null);
}
// langpacks)
if (is != null) {
props.load(is);
}
} catch (IOException e) {
throw new AssertException("LocalStrings for key::" + key + " could not be loaded", e);
} finally {
IOUtils.closeQuietly(is);
}
return props;
}
use of org.olat.core.util.SortedProperties in project openolat by klemens.
the class I18nManager method createNewLanguage.
public boolean createNewLanguage(String localeKey, String languageInEnglish, String languageTranslated, String authors) {
if (!i18nModule.isTransToolEnabled()) {
throw new AssertException("Can not create a new language when the translation tool is not enabled and the transtool source pathes are not configured! Check your olat.properties files");
}
if (i18nModule.getAvailableLanguageKeys().contains(localeKey)) {
return false;
}
// Create new property file in the brasato bundle and re-initialize
// everything
String coreFallbackBundle = i18nModule.getApplicationFallbackBundle();
File transToolCoreLanguagesDir = i18nModule.getTransToolApplicationOptLanguagesSrcDir();
String i18nDirRelPath = "/" + coreFallbackBundle.replace(".", "/") + "/" + I18nManager.I18N_DIRNAME;
File transToolCoreLanguagesDir_I18n = new File(transToolCoreLanguagesDir, i18nDirRelPath);
File newPropertiesFile = new File(transToolCoreLanguagesDir_I18n, I18nModule.LOCAL_STRINGS_FILE_PREFIX + localeKey + I18nModule.LOCAL_STRINGS_FILE_POSTFIX);
// Prepare property file
// Use a sorted properties object that saves the keys sorted alphabetically to disk
Properties newProperties = new SortedProperties();
if (StringHelper.containsNonWhitespace(languageInEnglish)) {
newProperties.setProperty("this.language.in.english", languageInEnglish);
}
if (StringHelper.containsNonWhitespace(languageTranslated)) {
newProperties.setProperty("this.language.translated", languageTranslated);
}
if (StringHelper.containsNonWhitespace(authors)) {
newProperties.setProperty("this.language.translator.names", authors);
}
OutputStream fileStream = null;
try {
// Create necessary directories
File directory = newPropertiesFile.getParentFile();
if (!directory.exists())
directory.mkdirs();
// Write to file file now
fileStream = new FileOutputStream(newPropertiesFile);
newProperties.store(fileStream, null);
fileStream.flush();
// Now set new language as enabled to allow user to translate the language.
Collection<String> enabledLangKeys = i18nModule.getEnabledLanguageKeys();
enabledLangKeys.add(localeKey);
// Reinitialize languages with new language
i18nModule.reInitializeAndFlushCache();
// Now add new language as new language (will re-initialize everything a second time)
i18nModule.setEnabledLanguageKeys(enabledLangKeys);
return true;
} catch (FileNotFoundException e) {
throw new OLATRuntimeException("Could not create new language file::" + newPropertiesFile.getAbsolutePath(), e);
} catch (IOException e) {
throw new OLATRuntimeException("Could not create new language file::" + newPropertiesFile.getAbsolutePath() + ", maybe permission denied? Check your directory permissions", e);
} finally {
try {
if (fileStream != null)
fileStream.close();
} catch (IOException e) {
log.error("Could not close stream after creating new language file::" + newPropertiesFile.getAbsolutePath(), e);
}
}
}
Aggregations