KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MLanguage


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.sql.*;
17 import java.util.*;
18
19 import org.compiere.Compiere;
20 import org.compiere.util.*;
21
22 /**
23  * Language Model
24  *
25  * @author Jorg Janke
26  * @version $Id: MLanguage.java,v 1.4 2003/10/04 03:55:45 jjanke Exp $
27  */

28 public class MLanguage extends X_AD_Language
29 {
30
31     /**
32      * Get Language Model from Language
33      * @param ctx context
34      * @param lang language
35      * @return language
36      */

37     public static MLanguage get (Properties ctx, Language lang)
38     {
39         return get (ctx, lang.getAD_Language());
40     } // getMLanguage
41

42     /**
43      * Get Language Model from AD_Language
44      * @param ctx context
45      * @param AD_Language language e.g. en_US
46      * @return language or null
47      */

48     public static MLanguage get (Properties ctx, String JavaDoc AD_Language)
49     {
50         MLanguage lang = null;
51         String JavaDoc sql = "SELECT * FROM AD_Language WHERE AD_Language=?";
52         PreparedStatement pstmt = null;
53         try
54         {
55             pstmt = DB.prepareStatement(sql);
56             pstmt.setString(1, AD_Language);
57             ResultSet rs = pstmt.executeQuery();
58             if (rs.next())
59                 lang = new MLanguage (ctx, rs);
60             rs.close();
61             pstmt.close();
62             pstmt = null;
63         }
64         catch (SQLException ex)
65         {
66             s_log.error("get", ex);
67         }
68         try
69         {
70             if (pstmt != null)
71                 pstmt.close();
72         }
73         catch (SQLException ex1)
74         {
75         }
76         pstmt = null;
77         return lang;
78     } // get
79

80     /**
81      * Load Languages (variants) with Language
82      * @param ctx context
83      * @param LanguageISO language (2 letter) e.g. en
84      * @return language
85      */

86     public static MLanguage[] getWithLanguage (Properties ctx, String JavaDoc LanguageISO)
87     {
88         ArrayList list = new ArrayList();
89         String JavaDoc sql = "SELECT * FROM AD_Language WHERE LanguageISO=?";
90         PreparedStatement pstmt = null;
91         try
92         {
93             pstmt = DB.prepareStatement(sql);
94             pstmt.setString(1, LanguageISO);
95             ResultSet rs = pstmt.executeQuery();
96             while (rs.next())
97                 list.add(new MLanguage (ctx, rs));
98             rs.close();
99             pstmt.close();
100             pstmt = null;
101         }
102         catch (SQLException ex)
103         {
104             s_log.error("getWithLanguage", ex);
105         }
106         try
107         {
108             if (pstmt != null)
109                 pstmt.close();
110         }
111         catch (SQLException ex1)
112         {
113         }
114         pstmt = null;
115         //
116
MLanguage[] languages = new MLanguage[list.size()];
117         list.toArray(languages);
118         return languages;
119     } // get
120

121
122     /*************************************************************************/
123
124     /**
125      * Load Language Model
126      * @param ctx context
127      * @param rs result set
128      */

129     public MLanguage (Properties ctx, ResultSet rs)
130     {
131         super (ctx, rs);
132     } // MLanguage
133

134     /**
135      * Create Language
136      * @param ctx context
137      * @param AD_Language language code
138      * @param Name name
139      * @param CountryCode country code
140      * @param LanguageISO language code
141      */

142     private MLanguage (Properties ctx, String JavaDoc AD_Language, String JavaDoc Name,
143         String JavaDoc CountryCode, String JavaDoc LanguageISO)
144     {
145         super (ctx, 0);
146         setAD_Language (AD_Language); // en_US
147
setIsBaseLanguage (false);
148         setIsSystemLanguage (false);
149         setName (Name);
150         setCountryCode(CountryCode); // US
151
setLanguageISO(LanguageISO); // en
152
String JavaDoc sql = "SELECT NVL(MAX(AD_Language_ID),0)+1 AS DefaultValue FROM AD_Language";
153         setAD_Language_ID(DB.getSQLValue(sql));
154     } // MLanguage
155

156     /** Logger */
157     private static Logger s_log = Logger.getCLogger (MLanguage.class);
158
159     /**
160      * String Representation
161      * @return info
162      */

163     public String JavaDoc toString()
164     {
165         return "MLanguage[" + getAD_Language() + "-" + getName()
166             + ",Language=" + getLanguageISO() + ",Country=" + getCountryCode()
167             + "]";
168     } // toString
169

170     /**
171      * Get Locale
172      * @return Locale
173      */

174     public Locale getLocale()
175     {
176         return new Locale (getLanguageISO(), getCountryCode());
177     } // getLocale
178

179
180     /*************************************************************************/
181
182     /**
183      * Setup
184      * @param args args
185      */

186     public static void main(String JavaDoc[] args)
187     {
188         System.out.println("Language");
189         Compiere.startupClient();
190         Log.setTraceLevel(4);
191
192         System.out.println(MLanguage.get(Env.getCtx(), "de_DE"));
193         System.out.println(MLanguage.get(Env.getCtx(), "en_US"));
194
195         /**
196         Locale[] locales = Locale.getAvailableLocales();
197         for (int i = 0; i < locales.length; i++)
198         {
199             Locale loc = locales[i];
200             if (loc.getVariant() != null && loc.getVariant().length() != 0)
201                 continue;
202             if (loc.getCountry() != null && loc.getCountry().length() != 0)
203                 continue;
204
205             System.out.println(loc.toString()
206                 + " - " + loc.getDisplayName()
207                 + " + " + loc.getCountry()
208                 + " + " + loc.getLanguage()
209             );
210             MLanguage lang = new MLanguage (Env.getCtx(), loc.toString(),
211                 loc.getDisplayName(), loc.getCountry(), loc.getLanguage());
212             lang.save();
213             System.out.println(lang);
214         }
215        /**/

216     } // main
217

218 } // MLanguage
219
Popular Tags