WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] How to translate/switch specific contents on templates (17 posts)

  1. ChowKaiDeng
    Member
    Posted 2 years ago #

    Hi!

    As I said before, the plugin is working perfect for me, but I have a question:

    I'm translating most of the content of my site using different Widgets for each language (combining them with Rich Widget and other stuff), but there are some elements that require a direct translation on my theme/template, for example, a slideshow that I'm using on the home page.

    For now, particulary on the home page, I've managed to use a different slideshow using this function:

    <?php
      $homepage = "/en/";
      $currentpage = $_SERVER['REQUEST_URI'];
      if($homepage==$currentpage):
    ?>
      <div>The slideshow with images in English</div>
    <?php else: ?>
      <div>The slideshow with images in Spanish</div>
    <?php endif; ?>

    ...but that just works well for me on the home page.

    Is there any way to set a function to show different contents on my templates depending on the language selected by the user, using a function like the one from above?

    And I have a second question: Why don't you add a "Donate" button (using something like Paypal) inside the settings page of the plugin? I would be more than happy to collaborate in order to maintain the plugin alive. I don't have too much money (and there are odd rules to control foreign currency in my country) but I could spend at least $10 each time that I use your plugin (right now, I'm using it in two projects).

    Thanks again!

    http://wordpress.org/extend/plugins/polylang/

  2. yoyurec
    Member
    Posted 2 years ago #

  3. ChowKaiDeng
    Member
    Posted 2 years ago #

    Thanks yoyurec! I'm checking it out, but as far as I can see, I think that would do the job.

    Greetings!

  4. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    I have no permanent internet connection these weeks. I can't look at the forum everyday and then a post can stay many days without answer from my side. I may also miss some posts. So I am very happy to see help coming from users rather than me ! Thank you yoyurec for answering this post.

    For the donation link, I will probably include one when the plugin will be more mature. Thank you for your intention.

  5. ChowKaiDeng
    Member
    Posted 2 years ago #

    Hi guys!

    I've tried the solution from the other post sugested by yoyurec and Chouby (using get_locale) but it didn't work for me. But finally, I've found a perfect and similar solution using get_bloginfo, that can print the lang values located in the <html> tag. Here's what it looks like:

    <?php
     $currentlang = get_bloginfo('language');
     if($currentlang=="en-US"):
    ?>
     <div>Content in English</div>
    <?php else: ?>
     <div>Content in Spanish (or other language)</div>
    <?php endif; ?>

    So, basically, you could use that function as many times as you want on a template to get different contents depending of the language selected by the user :)

    I suggest to include this solution as part of the documentation of the plugin as many users may want to use it. What do you think?

    Greetings!

  6. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Thank you for this alternative solution. It's curious that get_locale() does not work. I have still to write the documentation...

  7. ChowKaiDeng
    Member
    Posted 2 years ago #

    Well, is not that get_locale() "didn't work", it was more like "I couldn't make it work" :)

    Greetings!

  8. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    I included a documentation in the v0.4. I hope it will help...

  9. gxgl
    Member
    Posted 1 year ago #

    Hello everyone!

    As a mix of this post and this post

    You can do the next steps:
    Note: Use Notepad++ and format/encoding pages as UTF-8 for WordPress
    Then add next code as following:

    At top of polylang.php
    ----------------------
    /* Template Language Hack */
    add_action('show_current_language', array(&$this, 'show_current_language'));
    /* End of Template Language Hack */

    At the bottom of polylang.php
    -----------------------------
    /* Language Hack */
    function show_current_language() {
    global $curlang;
    $curlang = $this->curlang->slug;
    }
    /* End of Language Hack */

    In header.php (until the <?php wp_head(); ?>).
    ----------------------------------------------
    <!-- Language Hack -->
    <?php
    $curlang = "none";
    do_action('show_kcurrent_language');
    ?>

    <html <?php language_attributes(); ?>>
    <!-- End of Language Hack -->

    Where needed in pages (for maximum 3 languages):
    ------------------------------------------------
    <!-- Language Hack -->
    <?php $currentlang = get_bloginfo('language');
    if($currentlang=="en-US")
    echo "<div>All rights reserved.</div>";
    elseif ($currentlang=="ru-RU")
    echo "<div>Все права защищены.</div>";
    else
    echo "<div>Toate drepturile rezervate.</div>";
    ?>
    <!-- End of Language Hack-->

    Maybe there can be another option for this to use more than 3 languages (maybe using array or something else), but... I have no clue at this moment :)

    The steps above worked perfectly for me, using wordpress 3.3.2

    Regards to all!

  10. _WILCO_
    Member
    Posted 1 year ago #

    Hello ChowKaiDeng,

    I`m writing you because I'm using your code to translate the contents of the "Welcome Inn" theme in http://www.complejo-leo.es

    <?php
     $currentlang = get_bloginfo('language');
     if($currentlang=="en-US"):
    ?>
     <div>Content in English</div>
    <?php else: ?>
     <div>Content in Spanish (or other language)</div>
    <?php endif; ?>

    It's working perfectly for 2 languages. But I have a "big" small problem. I need Spanish, English and French. Please, could you help me with the correct code for 3 idioms? I'm using a variation but it doesn't work:

    <?php $currentlang = get_bloginfo('language');
    if($currentlang=="en-US"):?>
    <div class="icon-top icon-phone">Phone Number: <span>(+34)  924 517 048</span></div>
    	<?php else: ?>
        	<?php $currentlang = get_bloginfo('language');
    		if($currentlang=="fr_FR"):?>
    		<div class="icon-top icon-phone">Téléphone: <span>(+34)  924 517 048</span></div>
    		<?php else: ?>
    <div class="icon-top icon-phone">Teléfono de información: <span>(+34)  924 517 048</span></div>
    
     			<?php endif; ?>
    	<?php endif; ?>

    Thank you very much!

  11. _WILCO_
    Member
    Posted 1 year ago #

    Solved! ChowKaiDeng I used your code for 3 languages.

    I made a mistake with the shortcode fr_FR, but I change it for fr-FR and it's working correctly.

    Regards!

  12. Lopo Lencastre de Almeida
    Member
    Posted 1 year ago #

    Maybe there can be another option for this to use more than 3 languages (maybe using array or something else), but... I have no clue at this moment :)

    For this you don't need such a complex stuff and tons of code inside your theme; just turn your theme translatable using _e() and __() functions and creating a POT file (and .po/.mo files for each language) :)

    Check http://www.solostream.com/blog/tutorials/translate-wordpress-theme/ for more instructions.

    Neverthless, this idea is great if you want to select different CSS files for each active language.

    Just add something like this in your theme's header.php:

    <style type="text/css" media="screen">
        <!-- @import url( <?php bloginfo('template_directory'); ?>/styles/<?php get_bloginfo('language'); ?>.css ); -->
    </style>
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/styles/<?php get_bloginfo('language'); ?>.css"
          type="text/css" media="screen" />

    Of course, you need to have a dir like [your_theme]/styles and a bunch of CSS files like en-US.css, pt-PT.css and so on.

    On your style.css you just keep the common styles that would work for all languages and on the language specific CSS you keep all the rest, for instance <div> positioning, @webfonts, logo and other imagery replacement, etc.

    You can, of course, use the same trick but put instead your code on your theme's functions.php like this:

    // Loading mytheme css styles
    function mytheme_styles() {
       // Set the language CSS URI
       $lang_style = get_template_directory_uri().'/styles/'.get_bloginfo('language').'.css";
       // Adding your theme's main CSS style.css
       wp_register_style('mytheme', get_template_directory_uri().'/style.css',,'all');
       // Adding language specific CSS. Add dependency on styles.css
       wp_register_style('mytheme_lang', $lang_style, array('mytheme'));
       // Enqueue both
       wp_enqueue_style('mytheme_lang');
    }
    // Add it to loop
    add_action('wp_head', 'mytheme_styles');

    With this the CSS files will be auto inserted in <head> with the wp_head() function call. And it would benefit of any minifying options you have to speed up your site load.

    note: You could use pll_current_language('slug') instead of get_bloginfo('language') when using Polylang. Check the docs and search for pll_current_language to see the syntax and adapt the code above accordingly ;)

  13. Lopo Lencastre de Almeida
    Member
    Posted 1 year ago #

    Another option you have, if you use an upgradable theme you don't control totaly (best option is to make a sub-theme but would loose upgrading) is to create a small plugin called, for instance, Custom Language Styling :)

    <?php
    
    /*
    Plugin Name: Custom Language Styling
    Description: Set custom styles for specific languages
    Version: 0.1
    License: GPL
    Author: A Great Guy
    Author URI: http://www.example.com/
    */
    
    function customlangstyling_styles() {
       // Set the language CSS URI
       $lang_style = get_template_directory_uri().'/styles/'.get_bloginfo('language').'.css";
       // Adding your theme's main CSS style.css
       wp_register_style('customlangstyling', get_template_directory_uri().'/style.css',,'all');
       // Adding language specific CSS. Add dependency on styles.css
       wp_register_style('customlangstyling_lang', $lang_style, array('customlangstyling'));
       // Enqueue both
       wp_enqueue_style('customlangstyling_lang');
    }
    // Add it to loop
    add_action('wp_head', 'customlangstyling_styles');
    1. save it as customlangstyling.php
    2. create the directory wp-content/plugins/customlangstyling
    3. upload the .php file there
    4. Activate it on your dashboard
    5. That's it

    Hope it helps.

  14. giastwp
    Member
    Posted 1 year ago #

    Thanks to ChowKaiDeng and all the guys here!!

  15. giastwp
    Member
    Posted 1 year ago #

    Today i've tried to do what i've read in this thread but ..dont work!
    This is the code:

    <div class="nav-previous alignleft">
    <?php
    $currentlang = get_bloginfo('language');
    if($currentlang=="en-US"):?>
    <?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'mytheme' ) ); ?>
    <?php else: ?>
    <?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Precedente', 'mytheme' ) ); ?>
    <?php endif; ?>
    </div>

    Where i am wrong?

  16. ChowKaiDeng
    Member
    Posted 1 year ago #

    Hi giastwp,

    I've just made a quick test and everything seems fine from here.
    Did you created both the English and non English languages under the "Settings > Languages" option?
    Did you created at least 3 posts in english, each one with his own translation in the other language? (they must be linked using the tool available on the right "Language" panel right before the "Publish" panel in your post edit screen)

    First try to print something more simple like:

    <div class="nav-previous alignleft">
    <?php
    $currentlang = get_bloginfo('language');
    if($currentlang=="en-US"):?>
    This is English
    <?php else: ?>
    This is Spanish
    <?php endif; ?>
    </div>

    If it works, then it's something else related to your "previous_post_link"

    I hope it work for you. I'm in a bit of a hurry right now!

  17. giastwp
    Member
    Posted 1 year ago #

    Thank you again, the 3 rule works!! :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic