Altering the language code on LinkedIn

As a beginner in programming, I successfully added the linkedin share button to my test webpage. Now, I am hoping to make the button change language based on the user's language selection on the webpage.

Linkedin uses a five character language code (ru_RU, en_US, ko_KR, etc), while my test webpage utilizes a two character language code (ru, en, ko). Therefore, I need to create a javascript function that will convert the two character code to the five character code.

Currently, I am using django 1.4 and accessing the language preference with "{{ user.get_profile.language_preference }}" which returns the two character language code.

Below is the script I tried to create for converting the language code:


<script type="text/javascript">
function language_code_extended(){
    var language_code_value = "{{ user.get_profile.language_preference }}";
    if ( language_code_value == ru ) { 
        return "ru_RU"
    }
    else if ( language_code_value == "fr" ) { 
        return "fr_FR"
    }
    else if ( language_code_value == "ko" ) {  
        return "ko_KR"
    }
    else {
        return "en_US"
    }
}
</script>

And here is the code for my linkedin share button:


<!-- LinkedIn Like Button code -->
<script src="//platform.linkedin.com/in.js" type="text/javascript">
    //lang: ru_RU
    lang: language_code_extended()
</script>

Unfortunately, this setup is not functioning as intended. Can anyone identify where I may be going wrong?

Thank you.

Answer №1

It is recommended to enclose the strings you are comparing in quotes, like this:

language_code_value == "ru"

Otherwise, the comparison will be looking for a variable named ru to compare to.

There are some additional considerations to take into account when making this conversion:

  • Having an if condition test for every language can become cumbersome and prone to errors.

  • The mapping of a language code ru to a code representing both a language and a country ru_RU assumes that someone speaking a certain language is from a specific country, which may not always be accurate. Many languages are spoken in multiple countries with different conventions.

It might be beneficial to explore alternative methods for achieving this, as it could lead to a more efficient and accurate solution.

I apologize for my lack of expertise in Django, but I suggest investigating whether it or a third-party package can provide you with the correct IETF language tag directly.

Edit

You may encounter issues with the following:

var language_code_value = "{{ user.get_profile.language_preference }}";

language_code_value will contain the literal string value

{{ user.get_profile.language_preference }}
, which will not match the two-digit language codes you intend to compare it to.

Answer №2

Found the solution using a unique approach that didn't involve JavaScript.

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

Troubleshooting Node.js - MongoDB document removal issue

I am attempting to delete all documents from a collection that contain a field named uuid with values matching the $in operator along with an array I provide. However, for some reason the deletion is not functioning as expected. Below is the code snippet a ...

What is the best approach to integrating an idle timeout feature with identityserver4 and aspnet identity for a secure implementation?

Currently, I am developing a login site using IdentityServer4 (server UI) with .NET Identity in .NET Core 2.2 Razor Pages. I have implemented a javascript modal alert that notifies users of an impending idle timeout and redirects them to the logout screen ...

Develop a radio button filter utilizing the "Date of Creation" attribute, implemented with either jquery or JavaScript

I am currently utilizing a customized Content Query Web Part to load a series of list items. Each of these items has an XSLT attribute known as the Created Date. Shown below is an example of the markup: <div class="item" createddate="2014-01-22 13:02 ...

Ensure NodeJS/JSDom waits for complete rendering before performing scraping operations

I am currently facing an issue with scraping data from a website that requires login credentials. When I try to do this using JSDom/NodeJS, the results are inconsistent compared to using a web browser like Firefox. Specifically, I am unable to locate the l ...

Executing a time-consuming function call within the componentDidMount lifecycle method of a React component

When working with my React component, I utilize the componentDidMount function to pass a string received through props to a processing function. This function then returns another string which is used to update the component's state. import React, { C ...

Storing extensive JSON data with AJAX, jQuery, and Java

Currently, I am utilizing jQuery AJAX to call a server-side method and sending a JSON string to the controller. Everything works smoothly when the JSON size is small, but as soon as it exceeds 7kb, the server side rejects the JSON string. I suspect that t ...

Switch out a visual element for Dropzone.js

During my recent project, I utilized Dropzone.js for image uploads. However, I am now interested in transforming the dropzone area into an actual image. For instance, if there is a "featured image" attached to an article, users should be able to drag and ...

Ways to stop click propagation in the case of a parent anchor link containing a button among its children

Every time I click on the Link parent, it triggers a click event on the button as well. I want these events to be independent. <Link className="product-item__link" to={`/products/${product.category}/${product.id}`} > <div className ...

Unraveling the Mysteries of AngularJS in a Tutorial Snippet

After reading through the enlightening theory snippets in Step 3 of AngularJS Tutorial, one particular passage piqued my curiosity: The scope, which connects our controller and template to create a dynamic view, is not isolated within its own bounda ...

When a radiobutton is clicked, a jQuery call to a PHP function triggers an AJAX request which results in a JavaScript function becoming unrefer

Currently, I have a situation where two radio buttons are representing different products. When one of them is clicked, the goal is to update the price displayed on the website based on the selected product. Everything seems to be working fine when using t ...

What is the best approach to ensure the navbar is responsive based on the viewport size?

I am working on creating a responsive webpage that will show different navigation based on the viewport size. Currently, I have written this JavaScript code: var shownav = document.getElementById('show-nav'); if (screen.width < "720"){ ...

eliminating the hues beneath the lines on Morris region charts

I'm seeking advice on how to remove colors below the lines in Morris area charts. Any ideas? Here's the code snippet I've been using: Morris.Area({ element: 'area-example', data: [ { y: '2006', a: 100, b: 90 }, ...

Ui Bootstrap (angularjs) sidebar is not functioning properly

I am encountering an issue with a sidenav that I am in the process of creating for one of my projects. The goal is to develop a side menu that shifts the content to the right when opened and returns it to the left when closed, essentially functioning as a ...

Issue with Firebase Cloud function not terminating despite receiving a 204 response code

Currently, I am developing a cloud function to manage server operations for a gaming panel. Everything seems to be functioning correctly except that after the request is completed, it fails to trigger the expected "data", "end", or "closed" events which ...

What sets apart the browser/tab close event from the refresh event?

Can you help me understand the difference between a browser/tab close event and a refresh event? I've been researching this on Stack Overflow, but I'm still having trouble with it. My goal is to be able to log out a user through a server call whe ...

Tips for effectively utilizing the display:none property to conceal elements and permanently eliminate them from the DOM

While working on my website, I utilized CSS media queries to hide certain elements by using display: none. Even though this effectively hides the element from view, it still lingers in the DOM. Is there a way to completely eliminate the element from the ...

At random intervals, a ReferenceError is triggered stating that Vue is not defined

While working on an application that uses templates rendered by Apache Velocity, I encountered the error "Uncaught ReferenceError: Vue is not defined" when trying to incorporate vue.js components. Oddly enough, this error is not consistent - it occurs most ...

Next.js throws an error when trying to access the document object while using React Aria overlays

Recently, I've been diving into Next.js for web development and stumbled upon commerce, a template specifically designed for e-commerce websites using Next.js. While exploring the codebase, I noticed the Sidebar component which leverages React Aria fo ...

What would be the best way to structure this workflow as a JavaScript data format?

I have a complex workflow that I need to represent as a JavaScript data structure. This workflow involves a series of questions and answers where the response to one question determines the next one asked. Here is a basic example of what this workflow migh ...

Issues encountered with certain Tailwind styles functioning improperly when deployed in a production environment with Next.js

It seems that there are some style issues occurring in the production build hosted on Netlify for a specific component. The problematic component is a wrapper located at ./layout/FormLayout.tsx. Below is the code for this wrapper: const FormLayout: React.F ...