Localizing Strings with an Extension Method
This is rather uncommon situation, but we needed a way to easily translate strings in our code against the resources deployed in 12\Resources. We created extension methods for that:
{
return Localize(source, (uint) Thread.CurrentThread.CurrentCulture.LCID);
}
public static string Localize(this string source, uint language)
{
var pattern = string.Format("^{0}$",
Regex.Escape("$Resources:FILE,KEY;").
Replace("FILE", @"\w+").
Replace("KEY", ".+"));
if (!Regex.IsMatch(source, pattern)) return source;
var parts = source.Split(new[] { ':', ',' }, 3);
var file = parts[1];
var key = parts[2];
return SPUtility.GetLocalizedString(
string.Format("$Resources:{0}", key), file, language);
}
And now when we need to translate a string in our code, we can simply use the following code:
// using Sininen.Meteoriitti.SharePoint.Common.Extensions;
var x = "$Resources:ResourceFile,ResourceKey;".Localize();
var y = "$Resources:ResourceFile,ResourceKey;".Localize(
SPContext.Current.Web.Language);
Popularity: 3% [?]
Worked great. Thanks!
Best regards,
Steve
http://www.winnipegtreeservice.com
If you are alone you must visit the best web platform for lonley guys to chat with some hot sexy yong ladies all night long – nutten – and you will not regret it!