Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NET4
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Reflection;
- using System.Resources;
- namespace Core {
- internal static class SR {
- const string RESOURCE_NAME = "UIStrings";
- const string CONTEXT_STRING_DELIMITER = "||";
- const string NEWLINE_STRING_DELIMITER = @"\n";
- const string TAB_STRING_DELIMITER = @"\t";
- // resource manager for retrieving strings
- static readonly ResourceManager s_resourceMgr;
- #if NET4
- static readonly ConcurrentDictionary<string,string> s_cache = new ConcurrentDictionary<string,string>();
- #else
- static readonly Dictionary<string,string> s_cache = new Dictionary<string,string>();
- #endif
- static SR() {
- s_resourceMgr = GetResourceManager(RESOURCE_NAME);
- }
- public static string GetString(string ident) {
- return GetString(ident,null);
- }
- public static string GetString(string ident,params object[] args) {
- if(ident == null) {
- throw new ArgumentNullException("ident");
- }
- string localizedString;
- try {
- // if it's already in the cache...
- if(s_cache.ContainsKey(ident)) {
- // pull from the cache
- localizedString = s_cache[ident];
- }
- else {
- // get the string from resources using the current ui culture
- localizedString = s_resourceMgr.GetString(ident,CultureInfo.CurrentUICulture);
- // just use the identifier as a last-ditch default
- if(localizedString == null) {
- localizedString = ident;
- }
- // and add it to the cache for future use
- s_cache[ident] = localizedString;
- }
- }
- catch(MissingManifestResourceException) {
- // just use the identifier as a last-ditch default
- localizedString = ident;
- s_cache[ident] = ident;
- }
- localizedString = FormatResource(args,localizedString);
- return localizedString;
- }
- static string FormatResource(object[] args,string localizedString) {
- if(localizedString.IndexOf(NEWLINE_STRING_DELIMITER) >= 0) {
- localizedString = localizedString.Replace(NEWLINE_STRING_DELIMITER,Environment.NewLine);
- }
- if(localizedString.IndexOf(TAB_STRING_DELIMITER) >= 0) {
- localizedString = localizedString.Replace(TAB_STRING_DELIMITER,"\t");
- }
- // locate context/no-context string delimiter
- int splitAt = localizedString.IndexOf(CONTEXT_STRING_DELIMITER,StringComparison.Ordinal);
- if(splitAt >= 0) {
- if(args == null || args.Length == 0) {
- // use the no-context part before the split point
- localizedString = localizedString.Substring(0,splitAt);
- }
- else {
- // splitAt is two characters before the beginning of the context string
- try {
- localizedString = String.Format(
- CultureInfo.InvariantCulture,
- localizedString.Substring(splitAt + CONTEXT_STRING_DELIMITER.Length),
- args
- );
- }
- catch(FormatException) {
- // use the no-context part before the split point
- localizedString = localizedString.Substring(0,splitAt);
- }
- }
- }
- return localizedString;
- }
- // get the resource manager for our strings
- static ResourceManager GetResourceManager(string resourceName) {
- string ourNamespace = MethodInfo.GetCurrentMethod().DeclaringType.Namespace;
- string basename = ourNamespace + "." + resourceName.TrimStart('.');
- // create our resource manager
- return new ResourceManager(
- basename,
- Assembly.GetExecutingAssembly()
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment