Package com.trulytech.mantis.system

Source Code of com.trulytech.mantis.system.InternationalManager

package com.trulytech.mantis.system;

import com.trulytech.mantis.result.DBResult;
import java.util.LinkedHashMap;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import com.trulytech.mantis.util.ClientInfo;
import javax.servlet.jsp.PageContext;
import javax.servlet.ServletRequest;

/**
*
* <p>
* Title: Mantis
* </p>
*
* <p>
* Description: 国际化资源处理类
* </p>
*
* <p>
* Copyright: Copyright (c) 2002
* </p>
*
* <p>
* Company:
* </p>
*
* @author Wang Xian
* @version 1.0
*/
public class InternationalManager {

  // 是否初始化
  public static boolean isInited = false;

  /**
   * 初始化资源
   *
   * @throws Exception
   */
  public static void init() throws Exception {
    if (isInited)
      return;

    logWriter.Info("reading 18in1 resource...");
    DBResult Result = CacheManager
        .getCache(com.trulytech.mantis.system.Properties.InternationalResource);
    int nSize = Result.getResultBuffer().size();
    for (int i = 0; i < nSize; i++) {
      LinkedHashMap rec = (LinkedHashMap) Result.getResultBuffer().get(i);
      setResources(((String) rec.get("language_id")).toUpperCase(),
          ((String) rec.get("msgkey")).toUpperCase(), (String) rec
              .get("msg"));

    }
    logWriter.Info("reading 18in1 resource successfully");
    isInited = true;
  }

  /**
   * 设置资源
   *
   * @param Language
   *            String
   * @param Key
   *            String
   * @param Message
   *            String
   */
  private static void setResources(String Language, String Key, String Message) {
    LinkedHashMap map = (LinkedHashMap) com.trulytech.mantis.system.Properties.Message
        .get(Language);
    if (map == null) {
      LinkedHashMap rec = new LinkedHashMap();
      rec.put(Key, Message);
      com.trulytech.mantis.system.Properties.Message.put(Language, rec);
    } else {
      map.put(Key, Message);
      com.trulytech.mantis.system.Properties.Message.put(Language, map);
    }
  }

  /**
   * 读取资源
   *
   * @param Language
   *            String
   * @param Key
   *            String
   * @return String
   */
  public static String getResource(String Language, String Key) {
    if (!isInited)
      return "%" + Language + "." + Key + "%";
    else {
      LinkedHashMap map = (LinkedHashMap) com.trulytech.mantis.system.Properties.Message
          .get(Language);
      if (map == null) {
        map = (LinkedHashMap) com.trulytech.mantis.system.Properties.Message
            .get(com.trulytech.mantis.system.Properties.DefaultLanguage);
        if (map == null)
          return "%" + Language + "." + Key + "%";
        else {
          if ((String) map.get(Key) == null)
            return "%" + Language + "." + Key + "%";
          else
            return (String) map.get(Key);

        }
      } else {
        if ((String) map.get(Key) == null)
          return "%" + Language + "." + Key + "%";
        else
          return (String) map.get(Key);
      }
    }

  }

  /**
   * 通过PageContext中的语言读取资源
   *
   * @param context
   *            PageContext
   * @param Key
   *            String
   * @return String
   */
  public static String getResource(PageContext context, String Key) {
    HttpSession session = context.getSession();
    ServletRequest req = context.getRequest();
    String Language = null;
    if (session == null) {

      Language = req.getLocale().toString();

    }

    // 获得Session中的Language对象

    if (session
        .getAttribute(com.trulytech.mantis.system.Properties.Session_Language) == null)
      Language = req.getLocale().toString();
    else
      Language = (String) session
          .getAttribute(com.trulytech.mantis.system.Properties.Session_Language);

    return getResource(Language.toUpperCase(), Key.toUpperCase());

  }

  /**
   * 获得资源信息
   *
   * @param req
   *            HttpServletRequest
   * @param Key
   *            String
   * @return String
   */
  public static String getResource(HttpServletRequest req, String Key) {
    HttpSession session = req.getSession(false);
    String Language = null;
    if (session == null) {

      Language = req.getLocale().toString();

    }

    // 获得Session中的Language对象

    if (session
        .getAttribute(com.trulytech.mantis.system.Properties.Session_Language) == null)
      Language = req.getLocale().toString();
    else
      Language = (String) session
          .getAttribute(com.trulytech.mantis.system.Properties.Session_Language);

    return getResource(Language.toUpperCase(), Key.toUpperCase());

  }

}
TOP

Related Classes of com.trulytech.mantis.system.InternationalManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.