Search in sources :

Example 1 with SortedProperties

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);
        }
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SortedProperties(org.olat.core.util.SortedProperties) Properties(java.util.Properties) SortedProperties(org.olat.core.util.SortedProperties) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with SortedProperties

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;
}
Also used : AssertException(org.olat.core.logging.AssertException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SortedProperties(org.olat.core.util.SortedProperties) Properties(java.util.Properties) SortedProperties(org.olat.core.util.SortedProperties) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with SortedProperties

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;
}
Also used : AssertException(org.olat.core.logging.AssertException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SortedProperties(org.olat.core.util.SortedProperties) Properties(java.util.Properties) SortedProperties(org.olat.core.util.SortedProperties) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with SortedProperties

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);
        }
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SortedProperties(org.olat.core.util.SortedProperties) Properties(java.util.Properties) SortedProperties(org.olat.core.util.SortedProperties) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

File (java.io.File)4 IOException (java.io.IOException)4 Properties (java.util.Properties)4 JarFile (java.util.jar.JarFile)4 AssertException (org.olat.core.logging.AssertException)4 SortedProperties (org.olat.core.util.SortedProperties)4 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 JarOutputStream (java.util.jar.JarOutputStream)2 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)2