Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>But i am not able to compile my code. I am getting an error like An attribute argument must be a constant expression,typeof expression or array creation expression of an attribute parameter type</p> </blockquote> <p>Because you cannot Customize the Properties like <code>ErrorMessageResourceName</code> and <code>ErrorMessageResourceType</code> to fetch some Dynamic results.</p> <p>You can only Customize the <code>Length</code> Property in <code>StringLengthAttribute</code> class by passing the Parameterized Constructor in your Customized class. That means you can only <code>Override</code> the <code>Length</code> of <code>String</code></p> <p>Like you said you want to pass some key to get the corresponding <code>Error Message</code>, that is not possible.</p> <p>You can also write the below mentioned Message in you Resource file</p> <pre><code>"Maximum allowed length is {0}" </code></pre> <p>and at run time you can Format the String by Replacing it with Parametrized Constructor value(which is Max String Length) in your Custom Class</p> <blockquote> <p>Get User's language preference from Session and return the labelText here</p> </blockquote> <p>You have set the language in you Global.asax file. So, your resource file will be picked according to the <code>UserLanguage</code>. So, just specify the Key name and corresponding value will be fetched from <code>Resource file</code> according to the selected language</p> <p><strong>How can I set the language?</strong></p> <pre><code>public sealed class LanguageManager { /// &lt;summary&gt; /// Default CultureInfo /// &lt;/summary&gt; public static readonly CultureInfo DefaultCulture = new CultureInfo("en-US"); /// &lt;summary&gt; /// Available CultureInfo that according resources can be found /// &lt;/summary&gt; public static readonly CultureInfo[] AvailableCultures; static LanguageManager() { List&lt;string&gt; availableResources = new List&lt;string&gt;(); string resourcespath = Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "App_GlobalResources"); DirectoryInfo dirInfo = new DirectoryInfo(resourcespath); foreach (FileInfo fi in dirInfo.GetFiles("*.*.resx", SearchOption.AllDirectories)) { //Take the cultureName from resx filename, will be smt like en-US string cultureName = Path.GetFileNameWithoutExtension(fi.Name); //get rid of .resx if (cultureName.LastIndexOf(".") == cultureName.Length - 1) continue; //doesnt accept format FileName..resx cultureName = cultureName.Substring(cultureName.LastIndexOf(".") + 1); availableResources.Add(cultureName); } List&lt;CultureInfo&gt; result = new List&lt;CultureInfo&gt;(); foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { //If language file can be found if (availableResources.Contains(culture.ToString())) { result.Add(culture); } } AvailableCultures = result.ToArray(); CurrentCulture = DefaultCulture; if (!result.Contains(DefaultCulture) &amp;&amp; result.Count &gt; 0) { CurrentCulture = result[0]; } } /// &lt;summary&gt; /// Current selected culture /// &lt;/summary&gt; public static CultureInfo CurrentCulture { get { return Thread.CurrentThread.CurrentCulture; } set { Thread.CurrentThread.CurrentUICulture = value; Thread.CurrentThread.CurrentCulture = value; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload