工具类: 多国语言字符串操作类

参考了一些资料, 封装了一个国际化资源串操作类.

适合做Demo级别的资源国际化. 如果程序较大, 资源串存储的文件格式, 资源串读写实现都要改. 

prjLanguageResource.rar

测试程序:

// prjLanguageResource.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <tchar.h>
#include "Helper/Language/LanguageResHelper.h"
#include "Helper/file/fileHelper.h"
#include "Helper/string/stringHelper.h"

int _tmain(int argc, TCHAR * argv[])
{
    std::wstring    strRes;
    std::wstring    strLangResPathName;

    InitCmdlineDisp();

    strLangResPathName = GetTheModulePath();
    strLangResPathName += L"LangRes.ini";
    CLanguageRes    LangRes((wchar_t *)strLangResPathName.c_str());

    /// 建立多语言条目, 建立完成后,以后可以手工在语言文件中添加, 或做个语言文件录入程序

    /// 制作中文资源
//     LangRes.LangResSaveString(L"RESID_STR_PROG_NAME", L"中文测试程序", MAKELANGID(LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED));

    /// 制作英文资源
//     LangRes.LangResSaveString(L"RESID_STR_PROG_NAME", L"test program by english", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));

    /// 制作其他语种资源

    /// 得到资源字符串
    strRes = LangRes.LangResLoadString(L"RESID_STR_PROG_NAME");
    _tprintf(L"[RESID_STR_PROG_NAME] = %s\r\n", strRes.c_str());

    /// 使用资源字符串
    ::SetWindowText(GetConsoleWindow(), strRes.c_str());

    getchar();
	return 0;
}


国际化资源操作类:

/// @file       Helper\Language\LanguageResConst.h
/// @brief      多语言的常量定义
#ifndef __HELPER_LANGUAGE_LANGUAGERESCONST_H__
#define __HELPER_LANGUAGE_LANGUAGERESCONST_H__

#pragma pack(1)

#define LEN_LANGUAGE_RES_VALUE  4096
#define STR_LANG_RES_FILE_NAME  L"LangRes.ini"
#define STR_APPNAME_LANGUAGE_ID L"LANGUAGE_ID"

typedef struct _tag_LanguageId
{
    DWORD   wPrimLangId;    ///< Prim. lang. symbol
    DWORD   wSubLangId;    ///< Sublang. symbol

    _tag_LanguageId()
    {
        this->wPrimLangId = 0;
        this->wSubLangId = 0;
    }

    _tag_LanguageId(WORD wPrimLangId, WORD wSubLangId)
    {
        this->wPrimLangId = wPrimLangId;
        this->wSubLangId = wSubLangId;
    }
}TAG_LANGUAGE_ID;

#pragma pack()

const TAG_LANGUAGE_ID g_arrLanguageId[] = 
{
    TAG_LANGUAGE_ID(LANG_CHINESE_SIMPLIFIED,SUBLANG_CHINESE_SIMPLIFIED),    ///< 简体中文
    TAG_LANGUAGE_ID(LANG_ENGLISH, SUBLANG_ENGLISH_US),                      ///< 英文
};

#endif  // #ifndef __HELPER_LANGUAGE_LANGUAGERESCONST_H__

/// @file       \Helper\Language\LanguageResHelper.h
/// @brief      多语言资源工具类 

#ifndef __HELPER_LANGUAGE_LANGUAGE_RES_HELPER_H__
#define __HELPER_LANGUAGE_LANGUAGE_RES_HELPER_H__

#include <string>
#include "Helper/Language/LanguageResConst.h"
#include "Helper/UnicodeIni/UnicodeIni.h"

class CLanguageRes
{
public:
    CLanguageRes();
    CLanguageRes(wchar_t * pcLangResFilePathName);
    virtual ~CLanguageRes();

    /// 取资源串
    std::wstring LangResLoadString(const wchar_t * pcLangResId);

    /// 制作资源串
    bool LangResSaveString(const wchar_t * pcLangResId, wchar_t * pcResValue, DWORD dwLangId);
    std::wstring GetLanguageSectionById(DWORD dwLangId);   ///< 得到语言ID对应的Ini文件节名

private:
    void DataInit();    ///< 数据初始化
    void DataUnInit();  ///< 数据反初始化
    bool FindLocalLanguageId(DWORD & dwLocalLanguageId);    ///< 查找本地语言ID

private:
    bool            m_bFindLocalLanguageRes;    ///< 找到本地语言资源
    DWORD           m_dwLanguageId;             ///< 本地语言ID
    std::wstring    m_strLocalLanguageSection;  ///< 本地语言节, 都从这个节中读取资源串
    CUnicodeIni *   m_pIniOpt;                  ///< UnicodeIni文件操作类
};

#endif  ///< #ifndef __HELPER_LANGUAGE_LANGUAGE_RES_HELPER_H__

/// @file       LanguageResHelper.cpp
/// @brief      请参考LanguageResHelper.h

#include "stdafx.h"
#include "Helper/Language/LanguageResHelper.h"
#include "Helper/UnicodeIni/UnicodeIni.h"
#include "Helper/string/stringHelper.h"

CLanguageRes::CLanguageRes()
{
    DataInit();
}

CLanguageRes::CLanguageRes(wchar_t * pcLangResFilePathName)
{
    DataInit();

    if (NULL != pcLangResFilePathName)
    {
        SAFE_DELETE(m_pIniOpt);
        m_pIniOpt = new CUnicodeIni(pcLangResFilePathName, TRUE);
    }
}

CLanguageRes::~CLanguageRes()
{
    DataUnInit();
}

std::wstring CLanguageRes::GetLanguageSectionById(DWORD dwLangId)
{
    return stringFormatW(L"%s0x%x", STR_APPNAME_LANGUAGE_ID, dwLangId);
}

void CLanguageRes::DataInit()
{
    m_bFindLocalLanguageRes = FindLocalLanguageId(m_dwLanguageId);
    m_strLocalLanguageSection = GetLanguageSectionById(m_dwLanguageId);
    m_pIniOpt = new CUnicodeIni(STR_LANG_RES_FILE_NAME, TRUE);
}

void CLanguageRes::DataUnInit()
{
    SAFE_DELETE(m_pIniOpt);
}

bool CLanguageRes::FindLocalLanguageId(DWORD & dwLocalLanguageId)
{
    bool    bFind               =   false;
    size_t  nSize               =   sizeof(g_arrLanguageId) / sizeof(TAG_LANGUAGE_ID);
    size_t  nIndex              =   0;
    DWORD   dwLocalDefaultId    =   GetUserDefaultUILanguage();
    DWORD   dwLanguageId        =   0;

    dwLocalLanguageId = -1;
    for (nIndex = 0; nIndex < nSize; nIndex++)
    {
        dwLanguageId = static_cast<DWORD>(MAKELANGID(g_arrLanguageId[nIndex].wPrimLangId, g_arrLanguageId[nIndex].wSubLangId));
        if (dwLocalDefaultId == dwLanguageId)
        {
            bFind = true;
            dwLocalLanguageId = dwLocalDefaultId;
            break;
        }
    }

    return bFind;
}

std::wstring CLanguageRes::LangResLoadString(const wchar_t * pcLangResId)
{
    WORD            wLangId =   0;
    std::wstring    strRes  =   L"";    ///< 资源本地化串

    if (m_bFindLocalLanguageRes)
    {
        strRes.resize(LEN_LANGUAGE_RES_VALUE);
        m_pIniOpt->GetProfileString(m_strLocalLanguageSection.c_str(), pcLangResId, pcLangResId, (wchar_t *)(strRes.c_str()), LEN_LANGUAGE_RES_VALUE);
    }
    else 
        strRes = pcLangResId;

    return strRes;
}

bool CLanguageRes::LangResSaveString(const wchar_t * pcLangResId, wchar_t * pcResValue, DWORD dwLangId)
{
    std::wstring strLanguageSection  =   GetLanguageSectionById(dwLangId);
    return (m_pIniOpt->WriteProfileString(strLanguageSection.c_str(), pcLangResId, pcResValue)) ? true : false;
}


其他工具类略.


你可能感兴趣的:(工具类: 多国语言字符串操作类)