Goal
This article discusses a problem that needs to be solved for developing multi-lingual interface programs, that is, how to organize and access text resources.
This article focuses on the Windows platform to discuss the present and provide processing code.
Windows scenarios
Windows Nether Surface Development, in addition to Directui and other third-party development platform, then is based on MFC/WTL, Windows API, we will focus on VC + + development.
About VC + + How to manage text resources, either use the VC environment comes with the resource compilation mode, or other ways to deal with.
Using the Compile Resource method
That is, direct use of the VC environment provides the form of character resources, this method is simple and easy to operate.
That is, the string is compiled into a binary file as a resource, provided with a consistent resource API to access, and the corresponding string is accessed with an identifier.
How you can edit a text file
We want the text resource to be left out of the compilation environment, independently saved as an out-of-program file, and modified by non-programmers.
That is, the method of operation and the rules of Organization must be confirmed first. It is not only easy to encode the application when the program is developed, but also to expand and modify it independently (such as adding language resources, Imitate bionics).
For this requirement, I certainly hope that in the development of the program to access resources with some kind of identity, according to this habit does not require the original programmer to hold another read. In addition, we need to make the text storage have certain basis and rules to organize.
Therefore, we can consider the way in which rows are positioned and organized by segment. This kind of file is clever and Windows itself supports the INI file.
Text Resource Management INI implementation
Compile the environment this does not repeat, VS2008 environment, the console project can be. Put the code below.
Class Langmodule
classlangmodule{enumbuffsize {maxSize =0x7FFF}; TCHAR buffer[maxsize+1]; CString IniPath; Public: Langmodule (lpctstr path) {iniswitch (path); } voidIniswitch (LPCTSTR Path) {IniPath=Path; memset (Buffer,0,sizeof(buffer)); } LPCTSTR readysection (LPCTSTR lpappname/*Section name*/) {DWORD C=:: Getprivateprofilesection (lpappname, buffer, maxSize, IniPath); return(c)?Buffer:null; } DWORD Stringsplit (LPCTSTR sStr, LPCTSTR array[], DWORD count) {DWORD n=0; intLen =0; for(; n < count &&(Len= _tcslen (SSTR)) >0; n++, sstr++) {Array[n]=sStr; SStr+=Len; } returnN; }};
View Code
Main
StaticLPCTSTR Testini = _t ("E:\\codelab\\devexperiment\\debug\\test.ini");intMainvoid){ enum{Tx0, Tx1, Tx2, Tx3, Tx4, Tx5, Tx6, Tx7, txcount}; LPCTSTR Array[txcount]={NULL}; Langmodule T (Testini); printf ("\n[test]\n"); DWORD Count=T.stringsplit (T.readysection (_t ("Test") ), array, _countof (array)); printf ("Tx0:%s\n", array[tx0]); printf ("Tx3:%s\n", array[tx3]); printf ("Tx5:%s\n", array[tx5]); printf ("\nlist string[%d]:\n", Count); for(DWORD i =0; I < count; i++) printf (">%s\n", Array[i]); ////////////////////////////////////////////////////////////////////////// //test2printf ("\n[test2]\n"); Count= T.stringsplit (T.readysection (_t ("test2") ), array, _countof (array)); printf ("Tx0:%s\n", array[tx0]); printf ("Tx3:%s\n", array[tx3]); printf ("Tx5:%s\n", array[tx5]); printf ("\nlist string[%d]:\n", Count); for(DWORD i =0; I < count; i++) printf (">%s\n", Array[i]); return 0;}
View Code
#INI
[test] apitest2 Chinese test a little bit stringiniextend [test2] ; Dear comrade classmate friend Hi,allhello
Debug output
Access to multilingual text resources (Windows:ini)