What is the process for generating the string HttpContext.GetGlobalResourceObject("languagetext","PersonalReportFrench") using C#?

I have a hardcoded JavaScript value in my aspx page that is currently functioning.

PersonalReportLink.innerHTML = '<%=(String)HttpContext.GetGlobalResourceObject(@"languagetext","PersonalReportFrench")%>';

Now I want to make it dynamic. Here is what I am attempting:

sel_lang = "French" //this is the value obtained from the user PerReportStr = '<%= (String)HttpContext.GetGlobalResourceObject(@"languagetext","PersonalReport'+ sel_lang + '") %>';

This code gives me an empty string after execution, i.e., PerReportStr="" I believe the correct keyword is not being passed to the resource file.

The reason for this issue is that I am unable to construct the below string correctly using C#.

(String)HttpContext.GetGlobalResourceObject(@"languagetext","PersonalReportFrench")

Can someone please assist me in creating the above string using C#?

Answer №1

If the JavaScript code is directly embedded in the ASPX page, you can access global resources using this syntax:

PersonalReportLink.innerHTML = '<%= GetGlobalResourceObject("languagetext","PersonalReportFrench") %>';

However, there might be issues when trying to localize strings within JavaScript files. If the above method doesn't work, a more complex approach may be needed.

To use JavaScript files as resources, you can utilize the ScriptManager's EnableScriptLocalization attribute.

Add the following code snippet to any page that requires the resource:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true">
    <Scripts>
        <asp:ScriptReference Path="~/scriptResources.js" ResourceUICultures="fr-FR,de-DE" />
    </Scripts>
</asp:ScriptManager>

The key points to remember here are the path to the script file, the list of cultures specified in ResourceUICultures="fr-FR,de-DE", and setting EnableScriptGlobalization="true".

Create a JS file named scriptResources.js in your project and define resources for your default language:

var PersonalReportFrench = 'My English text';
var resourceTwo = 'More English text';

Additionally, create another JS file for localized strings, such as scriptResources.fr-FR.js:

var PersonalReportFrench = 'Mon texte Français';
var resourceTwo = 'Texte plus Français';

Assuming that

Thread.CurrentThread.CurrentCulture
is correctly set to the desired culture matching the locale in the script file name and ResourceUICultures attribute, the ScriptManager will load the appropriate JS file, allowing usage of variables in your JS code like this:

PersonalReportLink.innerHTML = PersonalReportFrench;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using RadStyleSheetManager for Optimizing CSS Caching

After implementing the RadStyleSheetManager in my master pages using the given code snippet, I noticed a significant improvement in the performance of my web application. In Internet Explorer 8, the number of dynamically generated WebResource.axd files had ...

ui-router: Issues with utilizing the <ui-view> element within a bespoke directive

In my current project, I am utilizing version 0.3.1 of ui-router. Within my custom directive, there is a <ui-view></ui-view> tag present. <div > <button type="button" class="btn btn-primary btn-circle btn-lg pull-left" ui-sref="u ...

Encounter a net::ERR_EMPTY_RESPONSE error while trying to deploy an Angular application on a production server

I have successfully developed an Angular App on my local machine and now I am facing challenges while trying to deploy it on a Windows production server. I have set up Apache to serve the App along with the Rest Service API. Accessing the App through the ...

Transforming JavaScript's POST Ajax into AngularJS' POST Factory

I am attempting to convert an Ajax call with WSSE authentication into an AngularJS factory. The request method is Post. The purpose of this conversion is to access the Adobe Analytics Rest API, retrieve data in JSON format, and then visualize it using d3. ...

Joining strings together using double quotes inside the parentheses of a function

It's recommended to utilize the function in this manner: $function.share("msg");. However, I am interested in appending a variable to the strings. $function.share("https://www.youtube.com/watch?v="+$Id+""); Unfortunately, it seems to be not function ...

Executing PHP script simultaneously in the background with jQuery

I have a PHP script that performs time-consuming tasks in the background. All I want is to display a simple message to the user: "Command executed. Invites pending." The rest of the processing will happen behind the scenes in my .php file. I'm consid ...

When using AngularJS html5Mode, the page refresh functionality can disrupt the routing within an ASP.NET MVC framework

Currently, I am implementing angularjs routing within my asp.net application. Within a folder named template, there are 3 html pages which are Home, About, and Error. Below is the content of my app.js file var app = angular.module('myApp', [&ap ...

Ensure that the text field in vue.js restricts input to integers and cannot be left empty

Trying to implement an input field in vue.js that only accepts integer values, ensures the quantity is not 0, and defaults to 1 if left empty. I attempted to block decimals with the code: <input type="number" min="1" @keydown="filterKey"></input& ...

Identify the default constructor generated by the compiler through reflection in C#

My project is focused on .NET 3.5 SP1 and I am utilizing CommentChecker to validate my XML documentation. Everything is functioning correctly until I encounter a class structure like the one below: /// <summary> /// documentation /// </summary> ...

Disabling a button after clicking using jQuery

There are occasions when I can inadvertently trigger the submit button twice, resulting in the ajax being triggered twice as well. Below is my HTML code: <div class="buttons-area"> <input id="step-two" class="btn btn-primary" type="submit" v ...

Unable to send emails with BCC recipients

Currently, I am facing an issue while sending emails to a single 'To' recipient along with a list of 'Bcc' recipients. Although the Bcc recipients are added successfully to the mailMessage's Bcc collection, they are not bei ...

Socket IO: Error - The call stack has exceeded the maximum size limit

Whenever a client connects to my node.js server, it crashes with a 'RangeError: Maximum call stack size exceeded' error. I suspect there's a recursive problem somewhere in my code that I can't seem to find. This is the code on my serve ...

Utilizing dat.gui sliders for rotating bones imported from Blender JSON/COLLADA files within three.js

Looking for guidance on manipulating bones in a json / collada file exported from Blender. Have been attempting this without success. Seeking to incorporate sliders (via dat.gui) for rotating each bone individually. Any pointers or assistance would be gr ...

Tips for preventing an MVC 4 ajax application from automatically redirecting to LogIn.Aspx

For our latest project, we are transitioning from MVC 3 to MVC 4 and implementing Ajax calls to fetch most of the content from the server. In MVC 3, we had the following configuration in the web.config file: <authentication mode="Forms"> ...

Organizing Parsed JSON Data with JavaScript: Using the _.each function for Sorting

var Scriptures = JSON.parse( fs.readFileSync(scriptures.json, 'utf8') ); _.each(Scriptures, function (s, Scripture) { return Scripture; }); This code extracts and displays the names of each book from a collection of scriptures (e.g., Genesis, ...

Angular6 - accessing elements that are not visible on the page

Currently, I am facing a situation where I have a component embedded in my HTML template that needs to remain hidden until a certain condition is met by checking a box. The tricky part is that the condition for setting "showControl" to true relies on calli ...

Can a Form be set to always stay on top without using the TopMost property?

I am looking to achieve a specific functionality with my WinForm - I want it to always be on top of a particular window, but not be globally topmost. For example, I want my form to appear above a game window when clicked on, but not above other application ...

What is the best way to verify the presence of values in a table row?

I am having some difficulty finding the correct solution for what I am trying to achieve. Here is the code snippet: var _1 = "Something1"; var _2 = "Something2"; var result = "dsadas"; $('<tr><td>' + _1 + '</td><td> ...

Guide to successfully integrate MathJax into your React application

I developed an application using HTML that successfully rendered MathJax equations through a script tag. However, after transitioning to React, the MathJax equations are no longer appearing. I have tried adding the MathJax script (shown below) in the comp ...

Identifying repetitive elements within an array of objects using JavaScript/Node.js

In my code, I have an array named offers containing objects structured like this: { sellItem: { id: _, quantity: _ }, buyItem: { id: _, quantity: _ }, volume: _ } My goal is to identify duplicates w ...