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)
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.
Related
I was reading about how to work with SqlDataSource objects by adding properties in the markup, and for the ConnectionString property, the author used something like
<asp:SqlDataSource ... ConnectionString="<%$ ConnectionStrings:lyric %>" ... >
The use of <%$ %> is new to me and the author didn't really explain it. I mean, I can see pretty easily what it does in this instance.
But, I want to learn the syntax inside <%$ %> to access other things inside Web Config. I don't know what this type of markup is called so I can't google it. I'm having a rough time finding it.
Do tags like <%# %>, <%# %>, and <%$ %> have a special blanket name? (so I can learn other variations)
Is there a specific name for <%$ %>? (so I can look it up and learn the syntax)
Thanks in advance
They are called "ASP.NET inline expressions" or "ASP.NET inline code-blocks". You can learn more about them here: http://support.microsoft.com/kb/976112
<%$ ... %> is the syntax for an expression builder.
I was using ASP.NET Web Forms and ASP.NET MVC for some period of time.
So <%= %> in views mean Response.Write(), <%: %> introduced in MVC adds html escaping.
In SqlDataSource control designer generates something like this ConnectionString="<%$ ConnectionStrings:FooConnectionString %>" and in repeater you use <%# Eval("") %> sytax.
My question is what exactly do <%$ %> and <%# %> tags, what methods they traslated into and how do they behave?
<%$ %> is the expression syntax.
There are some built in shortcuts for AppSettings, Resources and ConnectionStrings. You can also write your own.
<%# %> is the databinding expression syntax.
This is used in databound controls to resolve property values from the object being bound.
I clipped this text from a book (I can't remember which book) a while ago as I thought it explained the <%# %> syntax well -
Those of you familiar with classic ASP applications might think that
the <%# %> syntax looks very familiar. It is similar in purpose, but
you need to make sure that you don't confuse the two because doing so
could cause your application to function improperly. Whereas in ASP
(and ASP.NET), the <%= %> syntax causes whatever is inside the
brackets to be evaluated at render time, the <%# %> brackets unique to
ASP.NET are evaluated only during binding. As you will see later in
this section, the page and each bindable control on the page have a
DataBind() method. The expressions contained within the data binding
brackets (<%# %>) are evaluated only when the control's DataBind
method is invoked.
That only answers half your question, but Rob Stevenson-Leggett's excellent answer covers everything else. I just thought I'd add this by way of some further illustration.
I have written a blog post about the different types of expressions available in ASP.NET Web Forms:
Expressions vs. Statements, part 2: ASP.NET Code Block Types (internet archive)
It explains the following expression blocks:
<%$ %>
ASP.NET Expression Syntax, used to bind against application settings, connection strings, and resources.
<%# %>
ASP.NET Data-Binding syntax, only evaluated when calling a data binding method of the control.
<% %>
Code blocks, the code becomes part of RenderMethodDelegate. The code should be statements. Use Response.Write to output something.
<%= %>
Same as above, except the code should be a single expression. It will be wrapped in HtmlTextWrite.Write().
where we use <%# %> <%= %> <%# %> etc.
what else asp tags can be added in asp.net web pages?
<%%> is short hand for:
<script runat="server">
</script>
Anyting inside the <% and %> is server side code.
The other variants are also shortcuts:
<%#%> is a page directrive
<%=%> is short for Response.Write
<%:%> is short for Response.Write, adding html encoding (introduced with .NET 4.0)
<%#%> is a binding expression
This page is a good reference to all these.
<%# %> - page directives, Register control
<%= %> - for the server code
<%# %> - for the eval kind of function its also for the server code
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
ASP.NET “special” tags
What is the difference between <%# ... %>, <%= ... %> and <%$ ... %>?
I couldn't find anything information about this. It's impossible to find "<%=" using a search engine.
Do these tags have a name?
<%= ... %> is generally equivalent to Response.Write(...)
it cannot be used in a control attribute that is runat="server"
<%: ... %> (as of .NET v4.0) is an html encoded version of <%= %> (as #Eric mentions)
<%# ... %> is used in data-binding context for Bind, Eval or Output (as #Ray mentions)
<%$ ... %> is used in the context of a control attribute with runat="server" (google "expression builder" also have a look at making a general purpose 'Code' expression builder. it is evaluated when the attribute/Parameter is required by the control.
<%# %> will attempt to databind to a data source, using the Bind() function. This makes it a two-way function (read and write).
<%= %> will make the data read-only.
<%# %> is evaluated during data binding. It does not necessarily require Eval() or Bind() and Matthew suggested - I use it frequently to display plain text in a repeater control.
<%= %> is evaluated as the page renders. It is equivalent to calling Response.Write().
<%# %> can ONLY be used in data-binding context.
<%= %> expects a string value which it will then include in the output stream. So either a string variable or a method which returns a string. Anything else will cause an error.
I found some good information that clarifies the terminology for your future google searches:
http://authors.aspalliance.com/aspxtreme/aspnet/syntax/aspnetpagesyntax.aspx
Code Render Blocks:
<% inline code %>
A shortcut for HttpResponse.Write:
<%=inline expression %>
Data Binding Expressions:
<%# databinding expression %>
In a property:
<tagprefix:tagname property = "<%# databinding expression %>" runat="server" />
Server-side comments, such that they do not appear in the client's page source:
<%-- commented out code or content --%>
I've programmed in both classic ASP and ASP.NET, and I see different tags inside of the markup for server side code.
I've recently come across a good blog on MSDN that goes over the difference between:
<%= (percentage together with equals sign) and
<%# (percent sign and hash/pound/octothorpe)
(<%# is evaluated only at databind, and <%= is evaluated at render), but I also see:
<%$ (percent and dollar sign) and
<%# (percent sign and at symbol).
I believe <%# loads things like assemblies and perhaps <%$ loads things from config files? I'm not too sure.
I was just wondering if anyone could clarify all of this for me and possibly explain why it's important to create so many different tags that seemingly have a similar purpose?
<% %> - is for inline code (especially logic flow)
<%$ %> - is for evaluating expressions (like resource variables)
<%# %> - is for Page directives, registering assemblies, importing namespaces, etc.
<%= %> - is short-hand for Response.Write (discussed here)
<%# %> - is used for data binding expressions.
<%: %> - is short-hand for Response.Write(Server.HTMLEncode()) ASP.net 4.0+
<%#: %> - is used for data binding expressions and is automatically HTMLEncoded.
<%-- --%> - is for server-side comments
You've covered 2 of them (<%# is evaluated only at databind, and <%= is evaluated at render), and the answer for "<%#" is that it's compiler directives (ie., stuff like what you'd put on a compiler's command line).
I don't know about "<%$".