0

我想向用户发送一封本地化的电子邮件,但从资源文件中检索到的文本似乎是基于我的文化。

SmtpClient client = new SmtpClient();
client.Host = "xxx.xxx.xxx";
client.Credentials = new NetworkCredential("name", "password");
MailMessage mm = new MailMessage();
mm.Sender = new MailAddress("xxx@xxx.com");
mm.From = new MailAddress("xxx@xxx.com");
mm.To.Add(new MailAddress(email));
mm.Subject = Localization.EmailUserActiveTitle;
mm.Body = "<div><h3>" + Localization.EmailUserActiveBodyPart1 + "</h3></div></br>" +
            "<div>" + Localization.EmailUserActiveBodyPart2 + "</div>" +
            "<div><b>" + content + "</b></div></br>" +
            "<div>" + Localization.EmailUserActiveBodyPart3 + "</div>" +
            "<div>" + Localization.EmailUserActiveBodyPart4 + "</div>";
mm.IsBodyHtml = true;
mm.Priority = MailPriority.Normal;
client.Send(mm);

但是当我检索Localization.EmailUserActiveBodyPart1它时,它是根据我当前的文化进行本地化的。

如何检索指定的文化资源文件?

4

1 回答 1

1

ResourceManager 使用 Thread.CurrentThread.CurrentUICulture 属性来确定要加载的资源的本地化语言版本。

因此,如果您想强制本地化为特定语言(例如,与您发送电子邮件的用户相关的语言偏好),那么只需在您的代码之前执行此操作:

var previousUICulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR"); // Replace with the relevant culture name for your user

并在您的代码之后清理*:

Thread.CurrentThread.CurrentUICulture = previousUICulture;

*Obviously this is not a reliable clean-up. A finally block or wrapping this language-switching functionality in an IDisposable and using a using block would prevent your code running in a random language in case of a failure, but that's outside the point.

于 2012-05-22T10:40:20.267 回答