There are usually some error codes or identifiers in the program. for convenience in the program, Chinese characters are usually not applied, and some FileError or numbers are often used in the program. In addition, the enumerated objects that are often used in encoding identify the object state. Normally
There are usually some error codes or identifiers in the program. for convenience in the program, Chinese characters are usually not applied, and some FileError or numbers are often used in the program. In addition, the enumerated objects that are often used in encoding identify the object state. Generally, this information is directly or indirectly provided to users. what users need is a description of Chinese characters that are easy to understand. In the past, we either hardcoded the conversion rules of these identifiers and enumerations into the program, or directly prompted the user. The former is not very scalable, while the latter makes users confused. Now you can use the popular XML (configuration file) to retain the prompt information, and then use an object to convert the internal code in the machine into information that people can easily understand.
The conversion object is as follows:
/**////
/// Translation class, which translates internal codes into easy-to-understand Chinese characters
///
///
/// According to the information in the configuration file, translate the internal system code (error code, success Code) into Chinese (or a language that people can easily understand ).
///
Public static class Translation
...{
Private static System. IO. FileSystemWatcher watcher;
Private static XmlDocument content;
Private static string configFile;
Private static object locker = new object ();
/**////
/// Load the configuration file
///
///
Public static void Configure (string configFile)
...{
LoadFile (configFile );
If (watcher! = Null)
...{
Watcher. Dispose ();
}
Watcher = new FileSystemWatcher (Path. GetDirectoryName (configFile), Path. GetFileName (configFile ));
Watcher. Changed = new FileSystemEventHandler (watcher_Changed );
}
/**////
/// Load the default configuration file
///
Public static void Configure ()
...{
If (System. Web. HttpContext. Current! = Null)
...{
Configure (System. Web. HttpContext. Current. Server. MapPath ("~ /Translation. config "));
}
Else
...{
Configure (System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase "\" "translation. config ");
}
}
/**////
/// Load the file content
///
///
Private static void LoadFile (string configFile)
...{
Lock (locker)
...{
XmlDocument doc = new XmlDocument ();
Doc. Load (configFile );
Content = doc;
Translation. configFile = configFile;
}
}
/**////
/// When the file changes, load the file again
///
///
///
Private static void watcher_Changed (object sender, FileSystemEventArgs e)
...{
LoadFile (configFile );
}
/**////
/// Obtain the description of Enum. if Enum has a Flag, separate the description with commas (,).
///
///
///
Public static string GetEnumDescription (Enum enumValue)
...{
Return GetEnumDescription (enumValue ,",");
}
/**////
/// Obtain the description of Enum. if Enum has a Flag, use sparator to separate the description.
///
///
///
///
Public static string GetEnumDescription (Enum enumValue, string sparator)
...{
Type type = enumValue. GetType ();
// Check whether the Flags feature exists.
Object [] attrs = type. GetCustomAttributes (typeof (flagsattrites), false );
If (attrs. Length> 0)
...{
StringBuilder builder = new StringBuilder ();
Array arr = Enum. GetValues (type );
Foreach (Enum enu in arr) // describes how to obtain each value in a loop
...{
If (Convert. ToUInt64 (enumValue) & Convert. ToUInt64 (enu) = Convert. ToUInt64 (enu) // determine whether this value exists
...{
Builder. Append (GetEnumDes (type, enu. ToString ()));
Builder. Append (sparator );
}
}
If (builder. Length! = 0) // remove the last separator
Builder. Remove (builder. Length-sparator. Length, sparator. Length );
Return builder. ToString ();
}
Else
...{
Return GetEnumDes (type, enumValue. ToString ());
}
}
/**////
/// Obtain the description of an Enum type value
///
///
///
///
Private static string GetEnumDes (Type type, string value)
...{
String xquery = "/translation/enum/" type. FullName "/" value;
XmlNode node = content. SelectSingleNode (xquery );
If (node! = Null)
Return node. InnerText;
Else
Return value;
}
/**////
/// Specify the translation value
///
///
///
Public static string GetValueDescription (object obj)
...{
Return GetValueDescription ("default", obj );
}
/**////
/// Translate the specified value in the specified group
///
///
///
///
Public static string GetValueDescription (string group, object obj)
...{
If (obj = null)
Return "null ";
String xquery = "/translation/description [@ group = '" group "']/add [@ key = '" obj. ToString () "']/@ value ";
XmlNode node = content. SelectSingleNode (xquery );
If (node = null)
Return obj. ToString ();
Else
Return node. Value;