KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > gui > html > MMLanguage


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.gui.html;
11
12 import java.util.*;
13
14 import org.mmbase.module.*;
15 import org.mmbase.module.core.*;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * Module for multi-language support.
21  * This module reads (english) terms and their localized value(s) from a
22  * configuration file.
23  * It can then return the localized terms when needed.
24  *
25  * @application SCAN - Removing this from Core requires changes in Casting
26  * @author Daniel Ockeloen
27  * @version $Id: MMLanguage.java,v 1.12 2005/10/26 07:35:36 michiel Exp $
28  */

29 public class MMLanguage extends ProcessorModule {
30
31     // logger
32
private static Logger log = Logging.getLoggerInstance(MMLanguage.class.getName());
33
34     /**
35      * Reference to the MMbase module.
36      */

37     MMBase mmb=null;
38     /**
39      * The language currently in use.
40      */

41     String JavaDoc languagePrefix;
42
43     public void init() {
44         // As the modules are loaded in a hashtable, mmlanguage can be initialized *before*
45
// MMBase whereby MMLanguage gets the *default* language value, rather than the
46
// set value in mmbaseroot.xml. Hence delay of setting language until first
47
// translation call.
48
languagePrefix = null;
49     }
50
51
52     /**
53      * Basic constructor
54      */

55     public MMLanguage() {}
56
57     /**
58      * Handles the $MOD-MMLANGUAGE-commands.
59      * Commands handled by this method are:
60      * <ul>
61      * <li> GET-term : translates 'term' to the current language, if possible
62      * (otherwise returns the term unchanged).</li>
63      * <li> LANGUAGE: returns the language prefix currently in use.</li>
64      * </ul>
65      * @param sp the current page context
66      * @param cmds the tokenized command
67      * @return the result of the command as a String
68      */

69     public String JavaDoc replace(scanpage sp, String JavaDoc cmds) {
70         StringTokenizer tok = new StringTokenizer(cmds,"-\n\r");
71         if (tok.hasMoreTokens()) {
72             String JavaDoc cmd=tok.nextToken();
73             if (cmd.equals("GET")) {
74                 if (tok.hasMoreTokens()) {
75                     return getFromCoreEnglish(tok.nextToken());
76                 } else {
77                     return "missing core term";
78                 }
79             } else if (cmd.equals("LANGUAGE")) {
80                 return languagePrefix;
81             }
82         }
83         return "No command defined";
84     }
85
86     public String JavaDoc getFromCoreEnglish(String JavaDoc term) {
87         // Set languagePrefix if not set yet. It isn't set at initialization time because
88
// we can't be sure the MMBase module has already been initialized.
89
if (languagePrefix == null) {
90             mmb=(MMBase)getModule("MMBASEROOT");
91             languagePrefix=mmb.getLanguage();
92         }
93
94         String JavaDoc translated=getInitParameter(languagePrefix+"_"+term);
95         if (translated==null || translated.equals("")) {
96             log.warn("MMLanguage -> could not convert : "+term+" into : "+languagePrefix);
97             return term;
98         } else {
99             return translated;
100         }
101     }
102 }
103
Popular Tags