0

I've been reading how to translate web sites either by using local or global resx files. Although this works great for buttons and small pieces of text in general, when I'm facing the translation of big chunks of text it seems kind of odd to me having to store long strings in the resx files, and spacing the text in small keys makes me unable to re-read what I just wrote.

For instance:

<p> <%$ Resources:MyResources, Welcome %> <%$ Resources:MyResources, to %> <%$ Resources:MyResources, my %> <%$ Resources:MyResources, wonderful %> <%$ Resources:MyResources, Website %> , <%$ Resources:MyResources, Where %> <%$ Resources:MyResources, you %> <%$ Resources:MyResources, can %> <%$ Resources:MyResources, Find %> <%$ Resources:MyResources, amazing %> <%$ Resources:MyResources, information %> <%$ Resources:MyResources, about %> <%$ Resources:MyResources, this %><%$ Resources:MyResources, and %><%$ Resources:MyResources, that %> <p>

Or:

<%$ Resources:MyResources, WelcomeToMyWonderfulWebsiteWhereYouCanFindAmazingInformationAboutThisAndThat %>

Is there a better way to handle multilingual support? Thanks,

(forgot to add that I'm using VS2013 and this is for a C# WebForms site with aspx and ascx)

malarres
  • 2,941
  • 1
  • 21
  • 35
  • 1
    Check this project [https://github.com/turquoiseowl/i18n](https://github.com/turquoiseowl/i18n). With it you can make more readable templates. – Vitaliy Smolyakov Dec 02 '16 at 08:57
  • Thanks @VitaliySmolyakov ! I've been looking at this project and for certain it would be better to use [[[Welcome to My Wonderful Website where you can find amazing information about this and that]]] instead of WelcomeToMyWonderfulWebsiteWhereYouCanFindAmazingInformationAboutThisAndThat as key. Do you have any experience using it? At first glance it looks complicated to set this up! – malarres Dec 02 '16 at 18:56
  • 1
    Just install, add configuration method, translate some page for test and it's running :). There may be not obvious problem if you have 2 or 3 chars parts in your path, like zzz.com/api/call. Url localizer think that API is unknown language tag and remove it from url. Request comes to zzz.com/call. In last version /api/ was added to filter to exclude from processing. – Vitaliy Smolyakov Dec 05 '16 at 07:05

1 Answers1

1

You can render directly from Resources, you don't to use the <%$ syntax. You can use <%= (or the auto-encoding <%: version instead):

In your case, I would have a single Resources.resx file in my project, with an entry for the Welcome Blurb with a short key:

Key: WelcomeMessage
Value (in `Resources.resx`): "Welcome to my website where..."
Value (in `Resources.de-DE.resx`): "Willkommen auf meiner Website..."

And in my .aspx files using <%:

<p><%: Resources.WelcomeMessage %>

Or using <%= if you want to render raw HTML:

<p><%= Resources.WelcomeMessage %>

Razor syntax:

<p>@Resources.WelcomeMessage</p>

Or for readability:

<p>@( Resources.WelcomeMessage )</p>

Resources is a static class that exists and automatically retrieves the correct resource value based on your current Thread culture value (so be sure you have code that correctly changes the Thread's culture), returning your fallback value if a translation doesn't apply.

You can view the original value in Visual Studio by hovering your mouse over each property value as the XML comments generated by the Resx designer include the first 100 or so characters from the resource string.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • Thanks for your response. However :( with the <%:notation and with the <%= notation I receive this error: Compiler Error Message: CS0234: The type or namespace name 'WelcomeMessage' does not exist in the namespace 'Resources' (are you missing an assembly reference?) – malarres Dec 02 '16 at 18:37
  • And with the Razor syntax I see simply the @Resources.WelcomeMessage and not the value of it – malarres Dec 02 '16 at 18:38
  • @malarres Razor syntax only works in `.cshtml` files. – Dai Dec 02 '16 at 20:14
  • @malarres either import the `YourProject.Resources` namespace (which contains the `YourProject.Resources.Resources` class) or change the `Resources` class namespace to be in your project root namespace ( http://stackoverflow.com/questions/1333356/changing-resource-files-resx-namespace-and-access-modifier ) – Dai Dec 02 '16 at 20:15