3

We do internatializing (sp?) by this:

<asp:Label ID="labelPhone" runat="server"></asp:Label>

Then in the aspx.cs:

labelPhone.Text = (string)GetGlobalResourceObject("lang", "Phone") + ":";

But then if there are more labels I have to repeat:

labelPhone2.Text = (string)GetGlobalResourceObject("lang", "Phone") + ":";
labelPhone3.Text = (string)GetGlobalResourceObject("lang", "Phone") + ":";

Can this be avoided?

Like jQuery giving all identival labels the same class, then assigning text to that class:

 $(".phonelabel").text("Telefon:");
2
  • 1
    Have you checked how ASP.NET localization works? You don't need to set individual labels. You could refer to the resource key directly, eg: Text="<%$ Resources:Label1TextKey %>". You could also use templates for specific fields, eg a Phone template that includes the localized label and field. Commented Feb 28, 2017 at 9:28
  • Where does GetGlobalResourceObject come from? Have you tried using Resource Files ? Commented Feb 28, 2017 at 9:32

3 Answers 3

1

In C# you can perform single statement assignment like this:

labelPhone.Text = labelPhone2.Text = labelPhone3.Text =
(string)GetGlobalResourceObject("lang", "Phone") + ":";

or you can do it in a loop:

Label[] labels = new[] { labelPhone, labelPhone2, labelPhone3 };
Array.ForEach(labels, x => { x.Text = 
          (string)GetGlobalResourceObject("lang", "Phone") + ":"; });
Sign up to request clarification or add additional context in comments.

6 Comments

OK, I'll go for the first, but I would have preferred I would have been able to just add a new phone label without having to add it to the list of assignments.
I don't think you can check out - stackoverflow.com/questions/19417839/…
@LeifNeland have you checked how localization works in ASP.NET? You don't need to set individual labels, nor do it in the code-behind.
There may be a way to do it using reflection. I will look into it if I have time.
There is no need for reflection. This is a localization question. ASP.NET provides several mechanisms. For example, the text can be set on the tag itself : Text="<%$ Resources:Label1TextKey %>".
|
0

u can use this extension method.

public static class ControlExtensions
{
    public static void ForControls<T>(this Control ctrl, Func<T, bool> where, Action<T> action) where T : Control
    {
        if (ctrl is T)
            if (where(ctrl as T))
                action(ctrl as T);
        foreach (Control childControl in ctrl.Controls)
        {
            childControl.ForControls(where, action);
        }
    }
}

using..

            this.ForControls<Label>(
            // you can define any other condition
            c => c.ID.StartsWith("labelPhone"),
            // you can make any other process for this controll
            c => c.Text = (string)GetGlobalResourceObject("lang", "Phone") + ":");

Comments

0

As Panagiotis write:

 <asp:Label runat="server" Text="<%$ Resources:Lang, Phone %> "  CssClass="box" />

If the comment were an answer, i would have selected it as the winner ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.