All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.x5.template.ChunkLocale Maven / Gradle / Ivy

There is a newer version: 3.6.2
Show newest version
package com.x5.template;

import java.util.HashMap;
import java.util.IllegalFormatException;
import java.util.Locale;
import java.util.MissingResourceException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

import com.csvreader.CsvReader;
import com.x5.util.JarResource;

public class ChunkLocale
{
    private String localeCode;
    private HashMap translations;

    private static HashMap locales = new HashMap();

    public static ChunkLocale getInstance(String localeCode, Chunk context)
    {
        ChunkLocale instance = locales.get(localeCode);
        if (instance != null) {
            return instance;
        } else {
            instance = new ChunkLocale(localeCode, context);
            locales.put(localeCode,instance);
            return instance;
        }
    }

    public static void registerLocale(String localeCode, String[] translations)
    {
        // this is mainly here just for testing.
        ChunkLocale instance = new ChunkLocale(localeCode, translations);
        locales.put(localeCode, instance);
    }

    private ChunkLocale(String localeCode, Chunk context)
    {
        this.localeCode = localeCode;
        loadTranslations(context);
    }

    private ChunkLocale(String localeCode, String[] strings)
    {
        // this is mainly here just for testing.
        this.localeCode = localeCode;
        if (strings != null && strings.length > 1) {
            this.translations = new HashMap();
            for (int i=0; i+1();

            while (reader.readRecord()) {
                entry = reader.getValues();

                if (entry != null && entry.length > 1 && entry[0] != null && entry[1] != null) {
                    String key = entry[0];
                    String localString = entry[1];
                    translations.put(key,localString);
                }
            }

        } catch (IOException e) {
            System.err.println("ERROR loading locale DB: "+localeCode);
            e.printStackTrace(System.err);
        }
    }

    private Charset grokLocaleDBCharset()
    {
        String override = System.getProperty("chunk.localedb.charset");
        if (override != null) {
            Charset charset = null;
            try {
                charset = Charset.forName(override);
            } catch (IllegalCharsetNameException e) {
            } catch (UnsupportedCharsetException e) {
            }
            if (charset != null) return charset;
        }

        try {
            return Charset.forName("UTF-8"); // sensible default
        } catch (Exception e) {
        }

        // ok fine, whatever you got.
        return Charset.defaultCharset();
    }

    @SuppressWarnings("rawtypes")
    private InputStream locateLocaleDB(Chunk context)
    throws java.io.IOException
    {
        // (1) if chunk.localedb.path is defined,
        // check there for a file named xx_XX/translate.csv
        String sysLocalePath = System.getProperty("chunk.localedb.path");
        if (sysLocalePath != null) {
            File folder = new File(sysLocalePath);
            if (folder.exists()) {
                File file = new File(folder, localeCode + "/translate.csv");
                if (file.exists()) {
                    return new FileInputStream(file);
                }
            }
        }

        // (2) check the classpath for a resource named /locale/xx_XX/translate.csv
        String path = "/locale/" + localeCode + "/translate.csv";
        InputStream in = this.getClass().getResourceAsStream(path);
        if (in != null) return in;

        // (2a) check the caller's class resources
        Class classInApp = TemplateSet.grokCallerClass();
        if (classInApp != null) {
            in = classInApp.getResourceAsStream(path);
            if (in != null) {
                return in;
            }
        }

        // (3a) TODO - use context to grok app's resource context
        // and check there (eg, should work inside servlet context)

        // (3) check inside jars on the classpath...
        String cp = System.getProperty("java.class.path");
        if (cp == null) return null;

        String[] jars = cp.split(":");
        if (jars == null) return null;

        for (String jar : jars) {
            if (jar.endsWith(".jar")) {
                in = JarResource.peekInsideJar("jar:file:"+jar, path);
                if (in != null) return in;
            }
        }

        // (4) give up!
        return null;
    }

    public String translate(String string, String[] args, Chunk context)
    {
        return processFormatString(string,args,context,translations);
    }

    public static String processFormatString(String string, String[] args, Chunk context)
    {
        return processFormatString(string,args,context,null);
    }

    public static String processFormatString(String string, String[] args,
            Chunk context, HashMap translations)
    {
        if (string == null) return null;

        String xlated = string;
        if (translations != null && translations.containsKey(string)) {
            xlated = translations.get(string);
        }

        if (args == null || context == null || !xlated.contains("%s")) {
            return xlated;
        }

        // prepare format-substitution values from args
        // eg for strings like "Hello %s, welcome to the site!"
        Object[] values = new String[args.length];

        for (int i=0; i 1) {
                String lang    = langAndCountry[0];
                String country = langAndCountry[1];
                if (lang != null && lang.trim().length() > 0) {
                    if (country != null && country.trim().length() > 0) {
                        Locale locale = new Locale(lang, country);
                        // confirm that this is a valid locale
                        try {
                            if (locale.getISO3Country() != null) {
                                if (locale.getISO3Language() != null) {
                                    // VALID LOCALE!  RETURN!
                                    return locale;
                                }
                            }
                        } catch (MissingResourceException e) {
                        }
                    }
                }
            }
        }

        return null;
    }

    public String toString()
    {
        return this.localeCode;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy