How to use ResourceManager in website mode?

I am trying to do a manual translation for the application I'm working with. (There is already a working LocalizationModule, but it is dodgy, so I cannot use the <asp:Localize /> tags.

Usually with ResourceManager you should use it as Namespace.Folder.Resourcename (in the application). I am currently translating an existing asp.net site "(not a web application, so there is no namespace here ...).

Resources are located in the "Locales / resources" folder, which contains "fr-ca.resx" and "en-us.resx".

So, I used the code with something like this:

 public static string T(string search) { System.Resources.ResourceManager resMan = new System.Resources.ResourceManager( "Locales", System.Reflection.Assembly.GetExecutingAssembly(), null ); var text = resMan.GetString(search, System.Threading.Thread.CurrentThread.CurrentCulture); if (text == null) return "null"; else if (text == string.Empty) return "empty"; else return text; } 

and inside the page I have something like this <%= Locale.T("T_HOME") %>

When I update, I have the following:

Could not find resources suitable for the specified culture or neutral culture. Make sure "Locales.resources" is correctly built-in or connected to the assembly "App_Code.9yopn1f7" at compile time, or that all satellite assemblies required are downloadable and fully signed. Description: An unhandled exception occurred during the execution of the current web request. Look at the stack trace for error information and it came from the code.

Exception Details: System.Resources.MissingManifestResourceException: Could not find any resources suitable for the specified culture or neutral culture. Make sure "Locales.resources" is correctly built-in or connected to the assembly "App_Code.9yopn1f7" at compile time, or that all satellite assemblies required are downloadable and fully signed.

Source Error:

Line 14:
System.Resources.ResourceManager resMan = new System.Resources.ResourceManager ("Localization", System.Reflection.Assembly.GetExecutingAssembly (), zero); Line 15: Line 16: var text = resMan.GetString (search, System.Threading.Thread.CurrentThread.CurrentCulture); Line 17: Line 18: if (text == null)

Source file: C: \ Inetpub \ virtual domains \ galerieocarre.com \ subdomains \ DEV \ httpdocs \ App_Code \ Locale.cs Line: 16

I even tried loading the resource using Locales.fr-ca or only fr-ca nothing works here.

+4
source share
5 answers

Marvin Smit solution is great if you don't have access to HTTPContext

 const string ASSEMBLY_NAME = "App_GlobalResources"; const string RESOURCE_NAME = "Resources.MetaTagResource"; const string RESOURCE_MANAGER = "ResourceManager"; Assembly assembly = Assembly.Load(ASSEMBLY_NAME); Type type = assembly.GetType(RESOURCE_NAME); PropertyInfo propertyInfo = type.GetProperty(RESOURCE_MANAGER); ResourceManager resourceManager = propertyInfo.GetValue(null, new object[] { }) as ResourceManager; resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true); 

But if you have access to HTTPContext, just use HttpContext.GetGlobalResourceObject

 string title = HttpContext.GetGlobalResourceObject("MetaTagResource", "Title").ToString(); string keywords = HttpContext.GetGlobalResourceObject("MetaTagResource", "keywords").ToString(); string description = HttpContext.GetGlobalResourceObject("MetaTagResource", "Description").ToString(); 
+2
source

Perhaps you are just looking for the System.Web.Page database GetLocalResourceObject () ,?

see http://msdn.microsoft.com/en-us/library/ms153597.aspx

+1
source

When using resources in ASP.Net, you will see a specialized compiler (native property of the tool) in .resx files (ResXCodeGen)

The bottom line of this is that your resources are compiled into a class. The name of this class is the name you should use when you want to use the resources there.

The class name is created as follows:

{project namespace} {folder structure} {filename} .resx (.resx is NOT part of the generated name)

Since you mentioned that your resx file is in the name of the "Locales / resources" directory, you will need to use the name (it is assumed that the resx file is called "locales.resx")

"locales.resources.locales"

Adding a language and language, for example, "uk-en", is added by the resource manager, loading the appropriate assembly based on the culture that was specified when creating the ResourceManager instance. If none are specified, Thread.CurrentCulture is used. You do not have to use the language / language extensions yourself, leave this in the resource manager to handle this.

If you don't know how the resource class will end, you can always look at MSIL or use a reflector to determine the name of the resource class in the actual deployed assembly.

; - Added after comments where posted -------

I also looked at another code in which there was arround;

Have you tried the Reflection on App_GlobalResources approach?

1: you load the library "App_GlobalResources" in the context of the website (in HttpHandler, Aspx, Ascx, etc.). => Assembly.Load ("App_GlobalResources");

2: Go through the available types and take the ResourceManager property from each type. =>

PropertyInfo pi = type.GetProperty ("ResourceManager");

resManager = pi.GetValue (null, new object [] {}) as ResourceManager;

3: If he has one, get the required ResourceSet => resManager.GetResourceSet (englishCulture, true, true);

Hope this helps.

0
source

it is not c # but you can easily convert it to it

I have in the app_localResources directory with 2 files

myPage.aspx.resx
myPage.aspx.fr-ca.resx

and I use it like that on my .aspx page

 <asp:Label ID="lAddress1" runat="server" Text="<%$ Resources: lAddress1 %>"></asp:Label> 

that's how i manage it on my

 'using LCID from Public Enum elang En = &H1009 'en-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=1009&OS=Windows%202003%20%20Service%20Pack%201 Fr = &HC0C 'fr-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=0C0C&OS=Windows%202003%20%20Service%20Pack%201 End Enum 'strongly typed value in session Private _lang As elang = elang.En Public Property lang() As elang Get Return _lang End Get Set(ByVal value As elang) _lang = value End Set End Property 

and on every page I got

 Protected Overrides Sub InitializeCulture() If Not Me.IsPostBack Then Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(Sess.lang) Threading.Thread.CurrentThread.CurrentCulture = Globalization.CultureInfo.CreateSpecificCulture(Threading.Thread.CurrentThread.CurrentUICulture.Name) End If MyBase.InitializeCulture() End Sub 

when I want to use the resourcemanager I create a global resource file at the root level, I use it like this

 Dim rm = New System.Resources.ResourceManager("Resources.MyPage", Reflection.Assembly.Load("App_GlobalResources")) 
0
source

Edit

For a website project, read this Microsoft walkthrough .

I am afraid that you cannot use the ResourceMagager in a website project because it requires a namespace / assembly where the resources are located.

-one
source

Source: https://habr.com/ru/post/1299663/


All Articles