summaryrefslogtreecommitdiff
path: root/src/org/openoffice/tools/language/LanguageHelper.java
blob: b2d23b02f7051e666a866bebddb612e2536bfb19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package org.openoffice.tools.language;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Utility methods handling the language definitions and templates.
 * 
 * TODO Store the language objects in a static way for perf. reasons
 * 
 * @author cbosdonnat
 *
 */
public class LanguageHelper {
    
    private static final String DEFAULT_LANGS_DIR = "langs";

    private static File sLangsDir; 

    /**
     * Changes the languages configuration directory.
     * 
     * @param pConfigDir the new directory containing the languages definitions.
     */
    public static void setLanguagesDir( File pConfigDir ) {
        sLangsDir = pConfigDir; 
    }
    
    public static String[] getLanguagesIds() {
        File langsDir = getLangsDir();
        
        String[] langs = langsDir.list( new FilenameFilter() {
            
            @Override
            public boolean accept(File pDir, String pName) {
                return !pName.startsWith(".");
            }
        });
        
        return langs;
    }
    
    public static Language getLanguage( String pLang ) {
        Language language = null;
        try {
            language = new Language( pLang, new File( getLangsDir( ), pLang ) );
        } catch (IOException e) {
            e.printStackTrace();
        }
        return language;
    }
    
    private static File getLangsDir( ) {
        
        if ( sLangsDir == null ) {
            try {
                File jarFile = new File(LanguageHelper.class.getProtectionDomain()
                        .getCodeSource().getLocation().toURI());
                sLangsDir = new File( jarFile.getParentFile(), DEFAULT_LANGS_DIR );

            } catch (URISyntaxException e) {
            }
        }
        return sLangsDir;
    }
}