最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Html two language option with button (without having to redirect to different page ) - Stack Overflow

programmeradmin3浏览0评论

I'm trying to make second language option to the website. Here are the details for the project :

1) I'm not trying to use Google translator system or any other auto translator service to change the entire website language.

2) I only trying to translate the main description part in the website.

3) I already have written and saved translated version of the description text.

4)I also made a dropdown language option button in a separate file but under same template for both language.

So, to make it clear, my question is :

How can I use language option button to switch the language between English and Korean (from English to Korean, and from Korean to English depending on what user select) with the translated description text I have?

----- code below is the dropdown language option button ----------------

    <!DOCTYPE html>
    <html>
    <head>
	    <title></title>

	    <meta name="viewport" content="width=device-width, initial-scale=1">
	    <link rel="stylesheet" href=".3.7/css/bootstrap.min.css">
	    <script src=".3.1/jquery.min.js"></script>

	    <script src=".3.7/js/bootstrap.min.js"></script>
    </head>

    <body>

    <div class="container">            
       <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Language Option
        <span class="caret"></span></button>
        <ul class="dropdown-menu">
          <li><a href="#">English</a></li>
          <li><a href="#">Korean</a></li>
        </ul>
      </div>
    </div>

    </body>
    </html>

I'm trying to make second language option to the website. Here are the details for the project :

1) I'm not trying to use Google translator system or any other auto translator service to change the entire website language.

2) I only trying to translate the main description part in the website.

3) I already have written and saved translated version of the description text.

4)I also made a dropdown language option button in a separate file but under same template for both language.

So, to make it clear, my question is :

How can I use language option button to switch the language between English and Korean (from English to Korean, and from Korean to English depending on what user select) with the translated description text I have?

----- code below is the dropdown language option button ----------------

    <!DOCTYPE html>
    <html>
    <head>
	    <title></title>

	    <meta name="viewport" content="width=device-width, initial-scale=1">
	    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
	    <script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

	    <script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>

    <body>

    <div class="container">            
       <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Language Option
        <span class="caret"></span></button>
        <ul class="dropdown-menu">
          <li><a href="#">English</a></li>
          <li><a href="#">Korean</a></li>
        </ul>
      </div>
    </div>

    </body>
    </html>

Share Improve this question edited Oct 27, 2018 at 19:28 Maarti 3,7194 gold badges19 silver badges36 bronze badges asked Oct 27, 2018 at 12:15 dubooduboodubooduboo 2912 gold badges3 silver badges7 bronze badges 1
  • You can use i18 if you are planning to use any javascript framework if you want to do in plain HTML you can serve two different static files with content changed. – Vikram Thakur Commented Oct 27, 2018 at 12:23
Add a ment  | 

2 Answers 2

Reset to default 6

This is how i do it when i build a multi-lingual website. I put notes inside the code for clarification.

<form>
    <label for="lang-switch">
        <span lang="ko">언어</span>
        <span lang="en">Language</span>
    </label>
    <select id="lang-switch">
        <option value="en">English</option>
        <option value="ko" selected>한국어</option>
    </select>
</form>

<p>
    <span lang="ko">한국어 텍스트</span>
    <span lang="en">Text in English</span>
</p>

<script src="https://code.jquery./jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
$('[lang]').hide(); // hide all lang attributes on start.
$('[lang="ko"]').show(); // show just Korean text (you can change it)
$('#lang-switch').change(function () { // put onchange event when user select option from select
    var lang = $(this).val(); // decide which language to display using switch case. The rest is obvious (i think)
    switch (lang) {
        case 'en':
            $('[lang]').hide();
            $('[lang="en"]').show();
        break;
        case 'ko':
            $('[lang]').hide();
            $('[lang="ko"]').show();
        break;
        default:
            $('[lang]').hide();
            $('[lang="ko"]').show();
        }
});
</script>

Hope it solve your problem.

(since i don't speak Korean i used google-translate for the example)

Javascript is your best bet. In your <li> tags, you want to add an onclick event listener so that you can change the text of an HTML element when you select a list item.

<li onclick="toggleLanguage('English')"><a href="#">English</a></li>
<li onclick="toggleLanguage('English')"><a href="#">Korean</a></li>

Here, the onclick attribute calls the function: function toggleLanguage(language).

Next, create a <script> tag after the body to call your javascript code.

<script>
    function toggleDescriptor(language) {
        let description = document.getElementById("description");
        if (language === "Korean") {
            description.innerHTML = "Show Korean Text";
        }
        else {
            description.innerHTML = "Show English Text";
        }  
    }
<script>

This function accesses the element that you want to change the text to. In this case, I created an h1 tag with an id="description".

Here is an example of the all the changes:

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

<div class="container">
  <h1 id="description">Show English Text</h1>
   <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Language Option
    <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li onclick="toggleLanguage('English')"><a href="#">English</a></li>
      <li onclick="toggleLanguage('Korean')"><a href="#">Korean</a></li>
    </ul>
  </div>
</div>

</body>
<script>
  function toggleLanguage(language) {
    let description = document.getElementById("description");
    if (language === "Korean") {
      description.innerHTML = "Show Korean Text";
    }
    else {
      description.innerHTML = "Show English Text";
    }
  }
</script>
</html>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论