Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a contact us form in asp.net with c#. The contact form has to be developed across several languages. So, I have decided to move all the label values in the form to a resources file and trying to access them in the code behind.

For example

ASP.NET
------------
ASP.NET
<div class="Container"  runat="server">
<div id="Phone"  runat="server"></div>
<asp:TextBox ID="TextBoxPhone" runat="server"></asp:TextBox>
</div>


C# -CODE BEHIND
---------
Phone.InnerHtml=Resources.ContactUs.Phone;

The code behind fails to load the Resources namespace in intellisence. It tells Resources does not exists. Can anybody help?
Posted

You can get a resource (resx) value in two ways:

1.
C#
ResourceFileName.YourKeyName

2.
C#
ResourceFileName.ResourceManager.GetString("YourKeyName");


If you have different resource files for different languages, I would suggest that create a common method to get the value based on the culture or language instead of directly calling a resource file.


Hope it helps

Azee...
 
Share this answer
 
Comments
Krishna Siva 30-Sep-13 3:31am    
Thanks for the reply. Could you please elaborate a little more on how to use this?
Azee 30-Sep-13 3:37am    
What are you referring to? Use what? Can you access the resource file by name?
Krishna Siva 1-Oct-13 0:23am    
Thanks for your help pal :)
Solved! Made the application as a website. If it is a web application, we need to access it through keys as mentioned in the above comment. Thanks. I will be happy if somebody posts a code snippet on how to access in web application. :)
 
Share this answer
 
In some cases, you may want to retrieve all resources, instead of a specific resource, from a .resx file. To do this, you can use the System.Resources.ResXResourceReader class, which provides an enumerator for all resources in the .resx file. The System.Resources.ResXResourceReader class implements IDictionaryEnumerator, which returns a DictionaryEntry object that represents a particular resource for each iteration of the loop. Its DictionaryEntry.Key property returns the resource's key, and its DictionaryEntry.Value property returns the resource's value.

The following example creates a ResXResourceReader object for the CarResources.resx file created in the previous example and iterates through the resource file. It adds the two Automobile objects that are defined in the resource file to a System.Collections.Generic.List(Of T) object, and it adds five of the six strings to a SortedList object. The values in the SortedList object are converted to a parameter array, which is used to display column headings to the console. The Automobile property values are also displayed to the console.
C#
VB

Imports System.Collections
Imports System.Collections.Generic
Imports System.Resources

Module Example
Public Sub Main()
Dim resxFile As String = ".\CarResources.resx"
Dim autos As New List(Of Automobile)
Dim headers As New SortedList()

Using resxReader As New ResXResourceReader(resxFile)
For Each entry As DictionaryEntry In resxReader
If CType(entry.Key, String).StartsWith("EarlyAuto") Then
autos.Add(CType(entry.Value, Automobile))
Else If CType(entry.Key, String).StartsWith("Header") Then
headers.Add(CType(entry.Key, String), CType(entry.Value, String))
End If
Next
End Using
Dim headerColumns(headers.Count - 1) As String
headers.GetValueList().CopyTo(headerColumns, 0)
Console.WriteLine("{0,-8} {1,-10} {2,-4} {3,-5} {4,-9}",
headerColumns)
Console.WriteLine()
For Each auto In autos
Console.WriteLine("{0,-8} {1,-10} {2,4} {3,5} {4,9}",
auto.Make, auto.Model, auto.Year,
auto.Doors, auto.Cylinders)
Next
End Sub
End Module
' The example displays the following output:
' Make Model Year Doors Cylinders
'
' Ford Model N 1906 0 4
' Ford Model T 1909 2 4
 
Share this answer
 
v2
=> Right Click on your web solution.
=> Add New Folder and Rename it to App_GlobalResources.
=> Right Click on newly added folder App_GlobalResources.
=> Click on Add->New Item....
=> Expand Visual C# Under Installed Templates Panel and Select General.
=> Add ResourcesFile (you may rename it as you wish like Multiligual).
=> Here you will find a key value pair in a grid like page.
=> Add a key and required value against that specific key.
=> This Resource file said Multiligual will be the default resource file of your project.
=> For different laguages add new files and rename them like if you want chinese the name will be Multiligual.zh-CN.resx similarly if it is english then the name will be Multiligual.en-US.resx (You may get the codes from here).
=> Play the actual game and set the culture of your Application or Page. In the case of page add UICulture="es" Culture="es-MX" to the page tag on the top of the aspx file.In the case of Application use fillowing line of codes where ever you want (like on application start and etc.)
C#
protected override void InitializeCulture()
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            base.InitializeCulture();
        }

What ever you set the culture the complier will read the keys from relevent extentioned file. Like in this case the key will be readen from Multiligual.en-US.resx. But if this file does not exists then the default file will be searched for the given key.
=> Now your job has been done. Now you just have to eat the cooked rice.
=> If you are going to give the key in ASPX file just give it like this.
ASP.NET
<asp:label runat="server" id="lblMultiligual" text="<%$ Resources: MultiLingual, key %>" xmlns:asp="#unknown"></asp:label>

=> If it is required in Code Behind File (.cs).
C#
GetGlobalResourceObject("MultiLingual", "key")


Wish you best of luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900