Saltar al contenido

~ / archivo / stackoverflow / 50935965

hide/show language based div with jquery

StackOverflow · javascript, jquery, html, css · ayudé
  1. BrainBox pregunta +2 2018-06-19 19:52

    I have a problem with my web page hiding and showing different <div> elements on it. I want to translate the page and add two buttons to switch between the languages.

    $('#hu').click(function() {
      $('#show').css('display', 'none');
      $('#hide').show();
    });
    $('#en').click(function() {
      $('#hide').css('display', 'none');
      $('#show').show();
    });
    #hide {
      display: none;
    }
    
    #show {
      display: true;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input class="translate" type="button" id="en" value="English">
    <input class="translate" type="button" id="hu" value="Magyar">
    
    <ul>
      <li class="current">
        <div id="show"><a href=index.html>Főoldal</a></div>
      </li>
      <li>
        <div id="show"><a href=oneletrajz.html>Önéletrajz</a></div>
      </li>
      <li>
        <div id="show"><a href=kapcsolat.html>Kapcsolat</a></div>
      </li>
      <li>
        <div id="hide"><a href=index.html>Home</a></div>
      </li>
      <li>
        <div id="hide"><a href=oneletrajz.html>About US</a></div>
      </li>
      <li>
        <div id="hide"><a href=kapcsolat.html>Contact Us</a></div>
      </li>
    </ul>

    • fen1x 2018-06-19 19:54 Element ids should be unique on your page - use classes instead.
    • Tsurule Vol 2018-06-19 20:07 Hi, that example is pretty close to your question: jsfiddle.net/WSaMM
    • dandavis 2018-06-19 20:17 i would suggest you use css to show/hide instead of js, which you can use to toggle one body class to set lang: .en.,es{display:none} body.en .en, body.es .es { display: block; } and us js to achieve something like <input type=button onclick="document.body.className='en'" value=English>
    • Miguel Ortiz yo 2018-06-19 20:33 Check my updated answer :)
  2. Praveen Kumar Purushothaman respuesta aceptada +3 2018-06-19 19:56

    You have totally confused between the #hide, #show, etc. What you need is a class for each language and a flag (optional).

    1. Have a language class en or es and have items on it.
    2. Use .hide() and .show() for hiding and showing.
    3. On loading, hide one of the languages.
    4. Don't ever duplicate id values. It's a crime in HTML. Use classes instead.
    5. Use the language classes on the <li> than the child.

    $(function() {
      $('.Magyar').hide();
      $('#hu').click(function() {
        $('.English').hide();
        $('.Magyar').show();
      });
      $('#en').click(function() {
        $('.English').show();
        $('.Magyar').hide();
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input class="translate" type="button" id="en" value="English" />
    <input class="translate" type="button" id="hu" value="Magyar" />
    
    <ul>
      <li class="current Magyar">
        <div><a href=index.html>Főoldal</a></div>
      </li>
      <li class="Magyar">
        <div><a href=oneletrajz.html>Önéletrajz</a></div>
      </li>
      <li class="Magyar">
        <div><a href=kapcsolat.html>Kapcsolat</a></div>
      </li>
      <li class="current English">
        <div><a href=index.html>Home</a></div>
      </li>
      <li class="English">
        <div><a href=oneletrajz.html>About US</a></div>
      </li>
      <li class="English">
        <div><a href=kapcsolat.html>Contact Us</a></div>
      </li>
    </ul>

    • Ivan 2018-06-19 20:02 It is really how they do it on large websites?
    • Praveen Kumar Purushothaman 2018-06-19 20:03 @Ivan Nopes... They use many other ways, like server side translation and in case of PHP, they use .po translation files. In case Ruby on Rails, they use .yaml files and most of the translations happening in the server side will have a function like _("translation-text-key"). :)
    • BrainBox 2018-06-19 20:09 Thanks for the fast answer, it's working rly great. I also wanted to know if there is a better way to do the translate of the webpage. I am still at the beginning of learning these things as you can see. I apriciate any help. I heard that php is not realy a good option if you want to add several sever side stuff. So is there any better way to do a translate for a webpage or can you recomend one that is better in your opinion?
    • Praveen Kumar Purushothaman 2018-06-19 20:11 @BrainBox Ha ha! You are welcome. Who told PHP is not really a good language? Most of the top sites are using PHP. WordPress is one of the best examples. Check out how translations is made in PHP: php.net/manual/en/about.translations.php
    • BrainBox 2018-06-19 20:23 I'm creating a page with my friend as a reference and we want to do it a professional way however we are still learning all the stuff. We asked one of our contacts how to start and he said that if we want to add database like sql or any other more complex stuff to the website we will need serious backend and he recommended java, asp.net or node.js. By the way he exactly said this: "php is a disgusting and non conventional language. It wasn't made by an expert, the whole technology is thoughtless and you have to learn bad habits to solve certain thing with it which can cause problems elsewhere
    • Praveen Kumar Purushothaman 2018-06-19 20:26 @BrainBox That's completely wrong. There are 100s of websites that rely on PHP and they are really good. When your contact says Node JS is better than PHP (yes, it's better in many terms), but the way he compared is a joke. Try to get a good insight about PHP by searching for case studies in Google. That's a good way to start. :)
    • BrainBox 2018-06-19 20:26 I'm really sorry, clicked by mistake. Thank you for your help again. I will take a deep look into it then. I already had a good translate solution with php we dropped it thoughbecause of the above mentioned reason.
    • Praveen Kumar Purushothaman 2018-06-19 20:27 @BrainBox No worries at all. :) Check out my previous comment.
    • Praveen Kumar Purushothaman 2018-06-19 20:30 @BrainBox Not at all. Don't think PHP is a worst language, although many Java / .NET based people claim it (obviously they want their share). Go ahead and develop your app in PHP and you get a large community to support you! :) All the best!
  3. Miguel Ortiz yo respuesta 2018-06-19 20:08

    First change your names to something easy to understand at first sight.

    Finally your code works but not the way you want. The JS will look for the first ID element (not all of them) because ID Elements are unique. So, I've changed your ID elements for classes and applied the class to the <li> items, otherwise the "dots" will still be visible.

    $('#hu').click(function() {
      $('.eng').css('display', 'none');
      $('.hun').show();
    });
    $('#en').click(function() {
      $('.hun').css('display', 'none');
      $('.eng').show();
    });
    .hun {
      display: none;
    }
    
    .eng {
      display: true;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input class="translate" type="button" id="en" value="English">
    <input class="translate" type="button" id="hu" value="Magyar">
    
    <ul>
      <li class="eng">
        <div><a href=index.html>Főoldal</a></div>
      </li>
      <li class="eng">
        <div><a href=oneletrajz.html>Önéletrajz</a></div>
      </li>
      <li class="eng">
        <div><a href=kapcsolat.html>Kapcsolat</a></div>
      </li>
      <li class="hun">
        <div><a href=index.html>Home</a></div>
      </li>
      <li class="hun">
        <div><a href=oneletrajz.html>About US</a></div>
      </li>
      <li class="hun">
        <div><a href=kapcsolat.html>Contact Us</a></div>
      </li>
    </ul>

    Regarding the second part of your question, it all depends of the size of the website. If it's a small project then it's ok to do it this way.

    If we are talking about thousands of pages like (Wikipedia) this isn't a good way. In this case you would need a centralized translation template.

    For bigger sites, you could, for example use PHP to create HTML templates with language placeholders (check this pseudocode):

    <p>{HOME_LINK}</p>
    

    Then, use PHP to process the language selection (sending a request via JS/Query) and rendering the placeholder with the correct language from a file with the translations:

    if selection == EN then :
    // EN Translations
    HOME_LINK {Home}
    
    if selecction == UN then :
    // UN Translations
    HOME_LINK {Főoldal}
    
    • BrainBox 2018-06-19 20:50 I already met an example using diferent translate php files, I'M really unfamiliar with php language yet will check it deeply though. Thanks for your answer

← todos los hilos de StackOverflow