注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

win32--多语言  

2013-05-01 11:37:59|  分类: C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

//MutiLanguages.h

#ifdef MutiLanguagesAPI
#else
#define MutiLanguagesAPI _declspec(dllimport)
#endif

#include <map>
#include <string>
#include "tinystr.h"
#include "tinyxml.h"
using namespace std;

enum
{
 LANG_GET_CURRENT_PATH_ERROR = 1,
 LANG_CHS_NOT_EXIST = 2,
 LANG_CHT_NOT_EXIST = 3,
 LANG_ENG_NOT_EXIST = 4,
 LANG_SET_LANGUAGE_VER_SUCCESS = 5
};

typedef unsigned char byte;
typedef unsigned int uint;

class MutiLanguagesAPI MutiLanguages
{
    private:
     map<string,string>* m_pMap;
  WCHAR *m_pValue;

 private:
     void   LoadItemContents(char* path);
  string DealWithEscape(string value);
  string GetCurrentPath();
  string GetValueByKey(const char* key);
  void UTF8toUTF16(/*in*/const byte* utf8Text, /*in*/uint cbUtf8Text, /*out*/wchar_t* utf16Text, /*in,out*/uint& ccUtf16Text);
  uint UTF8toUTF16CharCount(/*in*/const byte* utf8Text, /*in*/uint cbUtf8Text);

    public:
  MutiLanguages();
  ~MutiLanguages();
  int    LoadLanguage();
  WCHAR*  Get(const char* key);

};

 

 

///////////////////////////////////////////////////////////////////////

//MutiLanguages.cpp

#include "stdafx.h"
#include <io.h>

#define MutiLanguagesAPI _declspec(dllexport)
#include "MutiLanguages.h"

MutiLanguages::MutiLanguages()
{
 this->m_pMap = new map<string,string>();
 this->m_pValue = NULL;
}
MutiLanguages::~MutiLanguages()
{
 if(NULL != this->m_pMap)
 {
  this->m_pMap->clear();
  delete this->m_pMap;
 }
 if(NULL != this->m_pValue)
 {
  delete[] this->m_pValue;
 }
}

int MutiLanguages::LoadLanguage()
{
 string currentPath = this->GetCurrentPath();
 if("" == currentPath)
 {
  return LANG_GET_CURRENT_PATH_ERROR;
 }
 char *fileName = new char[MAX_PATH];
 memset(fileName,0,MAX_PATH);
 LANGID langVerId = GetUserDefaultUILanguage();
    switch(langVerId) //判断当前操作系统的语言版本; 
    {
        case 0x0804:  //简体中文
            strcpy(fileName, (currentPath+("\\CHS.xml")).c_str());
   if(_access(fileName,0)==-1)
   {
    return LANG_CHS_NOT_EXIST;
   }
            break;

        case 0x0404: //繁体中文(台湾)
        case 0x0c04: //繁体中文(香港)
        case 0x1004: //繁体中文(新加坡)
            strcpy(fileName,(currentPath+"\\CHT.xml").c_str());
   if(_access(fileName,0)==-1)
   {
    return LANG_CHT_NOT_EXIST;
   }
            break;
          
        case 0x0409:  //美式英语
        case 0x0809:  //英式英语
        default:
   strcpy(fileName,(currentPath+"\\ENG.xml").c_str());
   if(_access(fileName,0)==-1)
   {
    return LANG_ENG_NOT_EXIST;
   }
            break;
    }
    this->LoadItemContents(fileName); //根据语言版本设置对应语言;
 if(NULL != fileName)
 {
  delete[] fileName;
  fileName = NULL;
 }
 return LANG_SET_LANGUAGE_VER_SUCCESS;
}
WCHAR* MutiLanguages::Get(const char* key)
{
 if(NULL != this->m_pValue)
 {
  delete[] this->m_pValue;
 }
 string value = this->GetValueByKey(key);
 int len = value.length();

 uint u16len = UTF8toUTF16CharCount((const byte*)value.c_str(),len);
 this->m_pValue = new WCHAR[u16len+1];
 memset(this->m_pValue,0,u16len+1);
 UTF8toUTF16((const byte*)value.c_str(),len,this->m_pValue,u16len);

 return this->m_pValue;
}
string MutiLanguages::GetValueByKey(const char* key)
{
 string strVal;
 string strKey(key);
    if(this->m_pMap->count(strKey)>0)
    {
  strVal = (*(this->m_pMap))[strKey];
  strVal = DealWithEscape(strVal);
    }
 else
 {
  strVal = string(key);
 }
 return strVal;
}

void MutiLanguages::LoadItemContents(char* fileName)
{
    if(!(this->m_pMap)->empty())  //如果之前存在,则清空;
    {
        (this->m_pMap)->clear();
    }
 TiXmlDocument doc(fileName);
 if(doc.LoadFile())
 {
    TiXmlHandle hDoc(&doc);
    TiXmlElement *pRoot,*pPrarm;
    pRoot=doc.FirstChildElement("Language");
  if(pRoot)
  {
     pPrarm=pRoot->FirstChildElement("Item");
     while(pPrarm)
     {
     const char* pKey = (pPrarm->Attribute("key"));
     const char* pVal = pPrarm->Attribute("value");
     this->m_pMap->insert(pair<string,string>(string(pKey),string(pVal)));
        pPrarm = pPrarm->NextSiblingElement();
     }
   }
 }
}
string MutiLanguages::DealWithEscape(string value)
{
 int pos = value.find("\\");
 if(pos<0)
 {
  return value;
 }
 
 int len = value.length();
 char* pVal = new char[len+1];
 memset(pVal,0,len+1);
 memcpy(pVal,value.c_str(),len);
 *(pVal+len) = '\0';

 char* pTmp = new char[len+1];
 memset(pTmp,0,len+1);

 pos = 0;
 for(int i=0;i<len;i++)
 {
  if(pVal[i] != '\\')
  {
   pTmp[pos] = pVal[i];
  }
  else
  {
   switch(pVal[i+1])
   {
    case 't':
     pTmp[pos] = '\t';
     i++;  //跳过两个字符,即pVal[i]与pVal[i+1],下同;
     break;
    case 'r':
     pTmp[pos] = '\r';
     i++;
     break;
    case 'n':
     pTmp[pos] = '\n';
     i++;
     break;
    case '\\':
     pTmp[pos] = '\\';
     i++;
     break;
    default:
     pTmp[pos] = pVal[i];//不处理;
     break;
   }
  }
  pos++;
 }
 pTmp[pos] = '\0';
 
 //len = pos-1;
 string changedValue(pTmp,pos);
 delete[] pVal;
 delete[] pTmp;

 return changedValue;
}
string MutiLanguages::GetCurrentPath()
{
 WCHAR wPath[MAX_PATH] = {};
 if(0==GetModuleFileName(NULL,wPath,MAX_PATH))
 {
  return "";
 }
 int nLen = WideCharToMultiByte(CP_ACP,0,wPath,-1,NULL,0,NULL,NULL);
 char* cPath = new char[nLen];
 WideCharToMultiByte(CP_ACP,0,wPath,-1,cPath,nLen,NULL,NULL);
 string strTmp = string(cPath);
 int pos = strTmp.find_last_of('\\');
 string currentPath(strTmp.substr(0,pos));
 if(NULL != cPath)
 {
  delete[] cPath;
  cPath = NULL;
 }
 return currentPath;
}

 

void MutiLanguages::UTF8toUTF16(/*in*/const byte* utf8Text, /*in*/uint cbUtf8Text, /*out*/wchar_t* utf16Text, /*in,out*/uint& ccUtf16Text)
{
    _ASSERTE(utf16Text != NULL);
    _ASSERTE(utf8Text != NULL);
    uint oldccUtf16Text = ccUtf16Text;
    while(cbUtf8Text > 0 && ccUtf16Text > 0)
    {
        if((*utf8Text & 0x80) == 0)
        {
            *utf16Text = utf8Text[0];
            cbUtf8Text -= 1;
            utf8Text += 1;
        }
        else if((*utf8Text & 0xE0) == 0xC0)
        {
            if(cbUtf8Text >= 2 && ((utf8Text[1] & 0xC0) == 0x80))
            {
                *utf16Text = 0;
                *utf16Text |= (utf8Text[0] & 0x1F) << 6;
                *utf16Text |= (utf8Text[1] & 0x3F);
                cbUtf8Text -= 2;
                utf8Text += 2;
            }
            else
            {
                break;
            }
        }
        else if((*utf8Text & 0xF0) == 0xE0)
        {
            if(cbUtf8Text >= 3 && ((utf8Text[1] & 0xC0) == 0x80) && ((utf8Text[2] & 0xC0) == 0x80))
            {
                *utf16Text = 0;
                *utf16Text |= (utf8Text[0] & 0x1F) << 12;
                *utf16Text |= (utf8Text[1] & 0x3F) << 6;
                *utf16Text |= (utf8Text[2] & 0x3F);
                cbUtf8Text -= 3;
                utf8Text += 3;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
        ccUtf16Text -= 1;
        utf16Text += 1;
    }
    ccUtf16Text = oldccUtf16Text - ccUtf16Text;
 *utf16Text = 0;
}
uint MutiLanguages::UTF8toUTF16CharCount(/*in*/const byte* utf8Text, /*in*/uint cbUtf8Text)
{
    _ASSERTE(utf8Text != NULL);
    uint ccUtf16Text = 0;
    while(cbUtf8Text > 0)
    {
        if((*utf8Text & 0x80) == 0)
        {
            cbUtf8Text -= 1;
            utf8Text += 1;
        }
        else if((*utf8Text & 0xE0) == 0xC0)
        {
            if(cbUtf8Text >= 2 && ((utf8Text[1] & 0xC0) == 0x80))
            {
                cbUtf8Text -= 2;
                utf8Text += 2;
            }
            else
            {
                break;
            }
        }
        else if((*utf8Text & 0xF0) == 0xE0)
        {
            if(cbUtf8Text >= 3 && ((utf8Text[1] & 0xC0) == 0x80) && ((utf8Text[2] & 0xC0) == 0x80))
            {
                cbUtf8Text -= 3;
                utf8Text += 3;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
        ++ccUtf16Text;
    }
    return ccUtf16Text;
}

  评论这张
 
阅读(528)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018