Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Scheduler LocalizationScheduler Localization
Previous
 
Next
New Post
2/15/2008 8:52 AM
 

I am developing a module that sends out emails to remind people about incomplete assignments. I have wrote a class in my module to access the scheduler but is there a way to localize the content. most content is pulled from a database which can be changed but some is static like "Dear <username>:" I would like to pull the greeting from a resource file but I can't seem to access it from the class as its not  a PortalModuleBase. How do I access a LocalResourceFile from a scheduler class?

 
New Post
2/18/2008 11:26 AM
 

I just ran into this issue myself.  Firstly, you can check out this Gemini issue to keep up to date with the change to the core necessary.

What I did to get my code working was to:
1) Create a resource file for my scheduled job
2) Create a const string called LocalResourceFile in my scheduled job that points to the relative path to that file
3) Rewrote the localization code so that it doesn't make use of HttpContext and doesn't require a PortalSettings object, which is below in C# (I've highlighted the most significant changes):

   1:  #region Localization
   2:   
   3:  private enum CustomizedLocale
   4:  {
   5:      None = 0,
   6:      Portal = 1,
   7:      Host = 2
   8:  }
   9:   
  10:  public static string GetString(string name, string resourceFileRoot, int portalId)
  11:  {
  12:      return GetString(name, resourceFileRoot, portalId, null, false);
  13:  }
  14:   
  15:  public static string GetString(string name, string resourceFileRoot, int portalId, bool disableShowMissingKeys)
  16:  {
  17:      return GetString(name, resourceFileRoot, portalId, null, disableShowMissingKeys);
  18:  }
  19:   
  20:  public static string GetString(string name, string resourceFileRoot, int portalId, string requestedResourceLanguage)
  21:  {
  22:      return GetString(name, resourceFileRoot, portalId, requestedResourceLanguage, false);
  23:  }
  24:   
  25:  public static string GetString(string name, string resourceFileRoot, int portalId, string requestedResourceLanguage, bool disableShowMissingKeys)
  26:  {
  27:      if (string.IsNullOrEmpty(name))
  28:      {
  29:          return string.Empty;
  30:      }
  31:      string portalDefaultLangauge = null;
  32:      if (!Null.IsNull(portalId))
  33:      {
  34:          portalDefaultLangauge = (new PortalController()).GetPortal(portalId).DefaultLanguage;
  35:      }
  36:      IDictionary<string, string> resources = GetResource(resourceFileRoot, portalDefaultLangauge, portalId, requestedResourceLanguage);
  37:   
  38:      // make the default translation property ".Text"
  39:      if (name.IndexOf(".", StringComparison.Ordinal) < 1)
  40:      {
  41:          name += ".Text";
  42:      }
  43:   
  44:      // If the key can't be found try the Local Shared Resource File resources
  45:      if (resourceFileRoot != null && (resources == null || !resources.ContainsKey(name)))
  46:      {
  47:          // try to use a module specific shared resource file
  48:          string localSharedFile = resourceFileRoot.Substring(0, resourceFileRoot.LastIndexOf("/", StringComparison.Ordinal) + 1) + Localization.LocalSharedResourceFile;
  49:          resources = GetResource(localSharedFile, portalDefaultLangauge, portalId, null);
  50:      }
  51:   
  52:      // If the key can't be found try the Shared Resource Files resources
  53:      if (resources == null || !resources.ContainsKey(name))
  54:      {
  55:          resources = GetResource(Localization.SharedResourceFile, portalDefaultLangauge, portalId, null);
  56:      }
  57:   
  58:      // If the key still can't be found then it doesn't exist in the Localization Resources
  59:      if (Localization.ShowMissingKeys && !disableShowMissingKeys)
  60:      {
  61:          if (resources == null || !resources.ContainsKey(name))
  62:          {
  63:              return "RESX:" + name;
  64:          }
  65:          else
  66:          {
  67:              return "[L]" + resources[name];
  68:          }
  69:      }
  70:      return resources[name];
  71:  }
  72:   
  73:  private static IDictionary<string, string> GetResource(string resourceFileRoot, string defaultLanguage, int portalId, string requestedResourceLanguage)
  74:  {
  75:      defaultLanguage = defaultLanguage.ToUpperInvariant();
  76:      string fallbackLanguage = Localization.SystemLocale.ToUpperInvariant();
  77:      string userLanguage;
  78:      if (requestedResourceLanguage == null)
  79:      {
  80:          userLanguage = Thread.CurrentThread.CurrentCulture.ToString().ToUpperInvariant();
  81:      }
  82:      else
  83:      {
  84:          userLanguage = requestedResourceLanguage;
  85:      }
  86:   
  87:      //  Ensure the user has a language set
  88:      if (string.IsNullOrEmpty(userLanguage))
  89:      {
  90:          userLanguage = defaultLanguage;
  91:      }
  92:   
  93:      Locale userLocale = Localization.GetSupportedLocales()[userLanguage];
  94:      if (userLocale != null && !string.IsNullOrEmpty(userLocale.Fallback))
  95:      {
  96:          fallbackLanguage = userLocale.Fallback.ToUpperInvariant();
  97:      }
  98:   
  99:      // Get the filename for the userLanguage version of the resource file
 100:      string userFile = GetResourceFileName(resourceFileRoot, userLanguage);
 101:   
 102:      // Set the cachekey as the userFile + the PortalId
 103:      string cacheKey = userFile.Replace("~/", "/").ToUpperInvariant() + portalId.ToString(CultureInfo.InvariantCulture);
 104:      if (!string.IsNullOrEmpty(Globals.ApplicationPath))
 105:      {
 106:          cacheKey = cacheKey.Replace(Globals.ApplicationPath, string.Empty);
 107:      }
 108:   
 109:      // Attempt to get the resources from the cache
 110:      IDictionary<string, string> resources = DataCache.GetCache(cacheKey) as IDictionary<string, string>;
 111:      if (resources == null)
 112:      {
 113:          // resources not in Cache so load from Files
 114:          // Create resources dictionary
 115:          resources = new Dictionary<string, string>();
 116:          // First Load the Fallback Language ensuring that the keys are loaded
 117:          string fallbackFile = GetResourceFileName(resourceFileRoot, fallbackLanguage);
 118:          resources = LoadResource(resources, cacheKey, fallbackFile, CustomizedLocale.None, portalId);
 119:          //  Add any host level customizations
 120:          resources = LoadResource(resources, cacheKey, fallbackFile, CustomizedLocale.Host, portalId);
 121:          //  Add any portal level customizations
 122:          resources = LoadResource(resources, cacheKey, fallbackFile, CustomizedLocale.Portal, portalId);
 123:  
 124:          // if the defaultLanguage is different, load it
 125:          if (!string.IsNullOrEmpty(defaultLanguage) && defaultLanguage != fallbackLanguage && userLanguage != fallbackLanguage)
 126:          {
 127:              string defaultFile = GetResourceFileName(resourceFileRoot, defaultLanguage);
 128:              resources = LoadResource(resources, cacheKey, defaultFile, CustomizedLocale.None, portalId);
 129:              //  Add any host level customizations
 130:              resources = LoadResource(resources, cacheKey, defaultFile, CustomizedLocale.Host, portalId);
 131:              //  Add any portal level customizations
 132:              resources = LoadResource(resources, cacheKey, defaultFile, CustomizedLocale.Portal, portalId);
 133:          }
 134:          //  If the user language is different load it
 135:          if (userLanguage != defaultLanguage && userLanguage != fallbackLanguage)
 136:          {
 137:              resources = LoadResource(resources, cacheKey, userFile, CustomizedLocale.None, portalId);
 138:              //  Add any host level customizations
 139:              resources = LoadResource(resources, cacheKey, userFile, CustomizedLocale.Host, portalId);
 140:              //  Add any portal level customizations
 141:              resources = LoadResource(resources, cacheKey, userFile, CustomizedLocale.Portal, portalId);
 142:          }
 143:      }
 144:      return resources;
 145:  }
 146:   
 147:  private static string GetResourceFileName(string resourceFileRoot, string language)
 148:  {
 149:      string resourceFileName;
 150:      if (resourceFileRoot != null)
 151:      {
 152:          if (language == Localization.SystemLocale.ToUpperInvariant() || string.IsNullOrEmpty(language))
 153:          {
 154:              switch (resourceFileRoot.Substring(resourceFileRoot.Length - 5).ToUpperInvariant())
 155:              {
 156:                  case ".RESX":
 157:                      resourceFileName = resourceFileRoot;
 158:                      break;
 159:                  case ".ASCX":
 160:                  case ".ASPX":
 161:                      resourceFileName = resourceFileRoot + ".resx";
 162:                      break;
 163:                  default:
 164:                      resourceFileName = resourceFileRoot + ".ascx.resx";
 165:                      break;
 166:              }
 167:          }
 168:          else
 169:          {
 170:              switch (resourceFileRoot.Substring(resourceFileRoot.Length - 5).ToUpperInvariant())
 171:              {
 172:                  case ".RESX":
 173:                      resourceFileName = resourceFileRoot.Replace(".RESX", "." + language + ".resx");
 174:                      break;
 175:                  case ".ASCX":
 176:                  case ".ASPX":
 177:                      resourceFileName = resourceFileRoot + "." + language + ".resx";
 178:                      break;
 179:                  default:
 180:                      resourceFileName = resourceFileRoot + ".ascx." + language + ".resx";
 181:                      break;
 182:              }
 183:          }
 184:      }
 185:      else if (language == Localization.SystemLocale.ToUpperInvariant() || string.IsNullOrEmpty(language))
 186:      {
 187:          resourceFileName = Localization.SharedResourceFile;
 188:      }
 189:      else
 190:      {
 191:          resourceFileName = Localization.SharedResourceFile.Replace(".RESX", "." + language + ".resx");
 192:      }
 193:      return resourceFileName;
 194:  }
 195:   
 196:  private static IDictionary<string, string> LoadResource(IDictionary<string, string> resources, string cacheKey, string ResourceFile, CustomizedLocale CheckCustomCulture, int portalId)
 197:  {
 198:      string f = null;
 199:      // Are we looking for customised resources
 200:      switch (CheckCustomCulture)
 201:      {
 202:          case CustomizedLocale.None:
 203:              f = ResourceFile;
 204:              break;
 205:          case CustomizedLocale.Portal:
 206:              f = ResourceFile.Replace(".RESX", ".Portal-" + portalId.ToString(CultureInfo.InvariantCulture) + ".resx");
 207:              break;
 208:          case CustomizedLocale.Host:
 209:              f = ResourceFile.Replace(".RESX", ".Host.resx");
 210:              break;
 211:      }
 212:      // If the filename is empty or the file does not exist return the dictionary
 213:      string filePath = HostingEnvironment.MapPath(f);
 214:      if (f == null || !File.Exists(filePath))
 215:      {
 216:          return resources;
 217:      }
 218:   
 219:      bool xmlLoaded;
 220:      XPathDocument doc = null;
 221:      CacheDependency dp = new CacheDependency(filePath);
 222:      try
 223:      {
 224:          doc = new XPathDocument(filePath);
 225:          xmlLoaded = true;
 226:      }
 227:      catch
 228:      {
 229:          xmlLoaded = false;
 230:      }
 231:   
 232:      if (xmlLoaded)
 233:      {
 234:          foreach (XPathNavigator nav in doc.CreateNavigator().Select("root/data"))
 235:          {
 236:              if (nav.NodeType != XPathNodeType.Comment)
 237:              {
 238:                  resources[nav.GetAttribute("name", string.Empty)] = nav.SelectSingleNode("value").Value;
 239:              }
 240:          }
 241:          try
 242:          {
 243:              int cacheMinutes = 3 * Convert.ToInt32(Globals.PerformanceSetting, CultureInfo.InvariantCulture);
 244:              if (cacheMinutes > 0)
 245:              {
 246:                  DataCache.SetCache(cacheKey, resources, dp, DateTime.MaxValue, new TimeSpan(0, cacheMinutes, 0));
 247:              }
 248:          }
 249:  #pragma warning disable 1692
 250:  #pragma warning disable EmptyGeneralCatchClause
 251:          catch {}
 252:  #pragma warning restore EmptyGeneralCatchClause
 253:  #pragma warning restore 1692
 254:      }
 255:      return resources;
 256:  }
 257:   
 258:  #endregion
 

Hope it helps!


Brian Dukes
Engage Software
St. Louis, MO
866-907-4002
DNN partner specializing in custom, enterprise DNN development.
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Scheduler LocalizationScheduler Localization


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out