Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Serializable]
[AttributeUsage(AttributeTargets.Class
| AttributeTargets.Assembly
| AttributeTargets.Field
| AttributeTargets.Property)]
[MulticastAttributeUsage(MulticastTargets.Property)]
public sealed class LocalizeAttribute : LocationInterceptionAspect
{
public override bool CompileTimeValidate(LocationInfo locationInfo)
{
// ignore if not a string property
return locationInfo.LocationKind == LocationKind.Property &&
locationInfo.LocationType == typeof(string);
}
public override void OnSetValue(LocationInterceptionArgs args)
{
var str = args.Value as string;
if (string.IsNullOrWhiteSpace(str))
{
args.ProceedSetValue();
return;
}
// short-circuit using a custom 'context' that captures the intention to localize for
// a language
var localizationCtx = LocalizationContext.GetCurrentContext();
if (localizationCtx == null)
{
args.ProceedSetValue();
return;
}
// again, use the custom localization context which contains information from the gettext
// file to localize the original string
args.Value = localizationCtx.Translate(str);
args.ProceedSetValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.