Skip to content

Commit 0a0a409

Browse files
committed
Added support for modifying nugget translation at runtime. (#300)
1 parent b4d2395 commit 0a0a409

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Diff for: README.md

+36
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ code shows the most common options:
9191
// the message in the default language. Defaults to true.
9292
//i18n.LocalizedApplication.Current.MessageKeyIsValueInDefaultLanguage = false;
9393
94+
// Specifies a custom method called after a nugget has been translated
95+
// that allows the resulting message to be modified, for instance according to content type.
96+
// See [Issue #300](https://github.com/turquoiseowl/i18n/issues/300) for example usage case.
97+
i18n.LocalizedApplication.Current.TweakMessageTranslation = delegate(System.Web.HttpContextBase context, i18n.Helpers.Nugget nugget, i18n.LanguageTag langtag, string message)
98+
{
99+
switch (context.Response.ContentType)
100+
{
101+
case "text/html":
102+
return message.Replace("\'", "'");
103+
}
104+
return message;
105+
};
106+
94107
// Blacklist certain URLs from being 'localized' via a callback.
95108
i18n.UrlLocalizer.IncomingUrlFilters += delegate(Uri url) {
96109
if (url.LocalPath.EndsWith("sitemap.xml", StringComparison.OrdinalIgnoreCase)) {
@@ -409,6 +422,29 @@ locale/es/messages.po
409422
locale/es-MX/messages.po
410423
```
411424

425+
#### Custom Modifications To Translations
426+
427+
Nuggets translations can be modified at runtime as follows:
428+
429+
```
430+
protected void Application_Start()
431+
{
432+
...
433+
// Specifies a custom method called after a nugget has been translated
434+
// that allows the resulting message to be modified, for instance according to content type.
435+
// See [Issue #300](https://github.com/turquoiseowl/i18n/issues/300) for example usage case.
436+
i18n.LocalizedApplication.Current.TweakMessageTranslation = delegate(System.Web.HttpContextBase context, i18n.Helpers.Nugget nugget, i18n.LanguageTag langtag, string message)
437+
{
438+
switch (context.Response.ContentType)
439+
{
440+
case "text/html":
441+
return message.Replace("\'", "'");
442+
}
443+
return message;
444+
};
445+
}
446+
```
447+
412448
### URL Localization
413449

414450
In keeping with emerging standards for internationalized web applications, i18n provides support for

Diff for: src/i18n/Concrete/NuggetLocalizer.cs

+7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public string ProcessNuggets(string entity, LanguageItem[] languages)
9393
message += "[FORMAT EXCEPTION]";
9494
}
9595
}
96+
// Optional late custom message translation modification.
97+
if (LocalizedApplication.Current.TweakMessageTranslation != null) {
98+
message = LocalizedApplication.Current.TweakMessageTranslation(
99+
System.Web.HttpContext.Current.GetHttpContextBase(),
100+
nugget,
101+
lt,
102+
message); }
96103
// Output modified message (to be subsituted for original in the source entity).
97104
DebugHelpers.WriteLine("I18N.NuggetLocalizer.ProcessNuggets -- msgid: {0,35}, message: {1}", nugget.MsgId, message);
98105
//

Diff for: src/i18n/LocalizedApplication.cs

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading;
33
using System.Text.RegularExpressions;
44
using i18n.Domain.Helpers;
5+
using i18n.Helpers;
56
using i18n.Domain.Abstract;
67
using i18n.Domain.Concrete;
78

@@ -179,6 +180,39 @@ public string DefaultLanguage
179180
/// </remarks>
180181
public SetLanguageHandler SetPrincipalAppLanguageForRequestHandlers { get; set; }
181182

183+
/// <summary>
184+
/// Declares a method type for a custom method called after a nugget has been translated
185+
/// that allows the resulting message to be modified.
186+
/// </summary>
187+
/// <remarks>
188+
/// In general it is good practice to postpone the escaping of characters until they
189+
/// are about to be displayed and then according to the content type of the output.
190+
/// Thus, a single quote character need not be escaped if in JSON, but should be escaped
191+
/// if in HTML or Javascript.
192+
/// This method allows for such conditional modification of the message.
193+
/// </remarks>
194+
/// <param name="context">Current http context.</param>
195+
/// <param name="nugget">The subject nugget being translated.</param>
196+
/// <param name="langtag">Language being set.</param>
197+
/// <param name="message">The message string which may be modified.</param>
198+
/// <returns>
199+
/// Modified message string (or message if no modification).
200+
/// </returns>
201+
public delegate string TweakMessageTranslationProc(System.Web.HttpContextBase context, Nugget nugget, LanguageTag langtag, string message);
202+
203+
/// <summary>
204+
/// Registers a custom method called after a nugget has been translated
205+
/// that allows the resulting message to be modified.
206+
/// </summary>
207+
/// <remarks>
208+
/// In general it is good practice to postpone the escaping of characters until they
209+
/// are about to be displayed and then according to the content type of the output.
210+
/// This, a single quote character need not be escaped if in JSON, but should be escaped
211+
/// if in HTML or Javascript.
212+
/// This method allows for such conditional modification of the message.
213+
/// </remarks>
214+
public TweakMessageTranslationProc TweakMessageTranslation { get; set; }
215+
182216
/// <summary>
183217
/// Specifies the type of HTTP redirect to be issued by automatic language routing:
184218
/// true for 301 (permanent) redirects; false for 302 (temporary) ones.

0 commit comments

Comments
 (0)