andrew4582

SR String Resource Class

Sep 9th, 2011
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. #define NET4
  2.  
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Reflection;
  8. using System.Resources;
  9.  
  10. namespace Core {
  11.  
  12.     internal static class SR {
  13.  
  14.         const string RESOURCE_NAME = "UIStrings";
  15.         const string CONTEXT_STRING_DELIMITER = "||";
  16.         const string NEWLINE_STRING_DELIMITER = @"\n";
  17.         const string TAB_STRING_DELIMITER = @"\t";
  18.  
  19.         // resource manager for retrieving strings
  20.         static readonly ResourceManager s_resourceMgr;
  21. #if NET4
  22.         static readonly ConcurrentDictionary<string,string> s_cache = new ConcurrentDictionary<string,string>();
  23. #else
  24.         static readonly Dictionary<string,string> s_cache = new Dictionary<string,string>();
  25. #endif
  26.  
  27.         static SR() {
  28.             s_resourceMgr = GetResourceManager(RESOURCE_NAME);
  29.         }
  30.  
  31.         public static string GetString(string ident) {
  32.             return GetString(ident,null);
  33.         }
  34.  
  35.         public static string GetString(string ident,params object[] args) {
  36.             if(ident == null) {
  37.                 throw new ArgumentNullException("ident");
  38.             }
  39.  
  40.             string localizedString;
  41.             try {
  42.                 // if it's already in the cache...
  43.                 if(s_cache.ContainsKey(ident)) {
  44.                     // pull from the cache
  45.  
  46.                     localizedString = s_cache[ident];
  47.                 }
  48.                 else {
  49.                     // get the string from resources using the current ui culture
  50.                     localizedString = s_resourceMgr.GetString(ident,CultureInfo.CurrentUICulture);
  51.  
  52.                     // just use the identifier as a last-ditch default
  53.                     if(localizedString == null) {
  54.                         localizedString = ident;
  55.                     }
  56.  
  57.                     // and add it to the cache for future use
  58.                     s_cache[ident] = localizedString;
  59.                 }
  60.             }
  61.             catch(MissingManifestResourceException) {
  62.                 // just use the identifier as a last-ditch default
  63.                 localizedString = ident;
  64.                 s_cache[ident] = ident;
  65.             }
  66.  
  67.             localizedString = FormatResource(args,localizedString);
  68.             return localizedString;
  69.         }
  70.  
  71.         static string FormatResource(object[] args,string localizedString) {
  72.  
  73.             if(localizedString.IndexOf(NEWLINE_STRING_DELIMITER) >= 0) {
  74.                 localizedString = localizedString.Replace(NEWLINE_STRING_DELIMITER,Environment.NewLine);
  75.             }
  76.             if(localizedString.IndexOf(TAB_STRING_DELIMITER) >= 0) {
  77.                 localizedString = localizedString.Replace(TAB_STRING_DELIMITER,"\t");
  78.             }
  79.             // locate context/no-context string delimiter
  80.             int splitAt = localizedString.IndexOf(CONTEXT_STRING_DELIMITER,StringComparison.Ordinal);
  81.             if(splitAt >= 0) {
  82.                 if(args == null || args.Length == 0) {
  83.                     // use the no-context part before the split point
  84.                     localizedString = localizedString.Substring(0,splitAt);
  85.                 }
  86.                 else {
  87.                     // splitAt is two characters before the beginning of the context string
  88.                     try {
  89.                         localizedString = String.Format(
  90.                           CultureInfo.InvariantCulture,
  91.                           localizedString.Substring(splitAt + CONTEXT_STRING_DELIMITER.Length),
  92.                           args
  93.                           );
  94.                     }
  95.                     catch(FormatException) {
  96.                         // use the no-context part before the split point
  97.                         localizedString = localizedString.Substring(0,splitAt);
  98.                     }
  99.                 }
  100.             }
  101.             return localizedString;
  102.         }
  103.  
  104.         // get the resource manager for our strings
  105.         static ResourceManager GetResourceManager(string resourceName) {
  106.             string ourNamespace = MethodInfo.GetCurrentMethod().DeclaringType.Namespace;
  107.             string basename = ourNamespace + "." + resourceName.TrimStart('.');
  108.  
  109.             // create our resource manager
  110.             return new ResourceManager(
  111.               basename,
  112.               Assembly.GetExecutingAssembly()
  113.               );
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment