Need Quality Code? Get Silver Backed

Sharing Translation Resources



by Gary H

By reason of weird translation, many such sets of instructions read like poems anyhow.

Brian Ferneyhough

We recently started work with a client to help them take a number of legacy websites global. This has involved getting the sites ready for internationalisation - a fun task given that the sites were originally put together in WebForms 2.0 a decade ago.

ASP.Net provides many tools for helping with translation of page content for other cultures. Fortunately for our client the scale of the changes we needed to make was relatively small so utilising tried and tested RESX resource files was the simplest and fastest solution.

As we were translating a number of sites across the estate we very quickly ran into a number of common error messages and button labels that we would love to translate once then reuse across the estate. Typically to translate a property in your ASPX page you would use the resource expression builder. Something like:

	Text="<%$ Resource: ResourceFile, StringName %>"/>

Unfortunately the default resource expression builder will only search for resource files in the App_GlobalResources and App_LocalResources folders. It won't look for resources in linked projects. To get around this we created an external resource expression builder. To start with, the code:

public class ExternalResourceExpressionBuilder : ExpressionBuilder
	public override CodeExpression GetCodeExpression(
		BoundPropertyEntry entry, object parsedData, 
			ExpressionBuilderContext context)
		return new CodeSnippetExpression(entry.Expression);

This is then registered with a line in the web.config under the system.web element:

	<add expressionPrefix="ExternalResource" 
		type="YourNamespace.ExternalResourceExpressionBuilder" />

We can now use our expression builder to lookup a resource string stored in a RESX in another linked project. The usage is:

	Text="<%$ ExternalResource: Qualified.Path.To.String %>"/>

And we're done! Armed with a library of common labels in a resource file in a shared project we can start sharing the translations across multiple sites.

C# , ASP.Net , i18n

Comments are Locked for this Post