Support

Account

Home Forums General Issues Use ACF as multilanguage solution Reply To: Use ACF as multilanguage solution

  • this is absolutely great!

    I am testing this and its looking very promising,
    f.e. because it can be used more specific (f.e. for a certain field) and is not that general.

    at the moment i have this:

    Links in header.php

    <?$data = array('lang'=>'en');?>
    <a href="?<?echo http_build_query($data) . "\n";?>">English</a>
    <?$data = array('lang'=>'ger');?>
    <a href="?<?echo http_build_query($data) . "\n";?>">Deutsch</a>
    

    in functions.php I set a cookie from the links above

    
    ///// Set cookie
    add_action('init', function() {
    global $language;	
    	
    	
    /////set cookie from GET:
    	
    if(isset($_GET['lang']))
    {
    $lang = $_GET['lang'];	 	
    setcookie('lang', $lang, strtotime('+1 day'),"/", false);
     }
    		
    ///receive value from cookie and get	
    if (isset($_COOKIE['lang'])){
    		  
    echo "<br>cookie:<br>";
    echo $_COOKIE['lang'];   
      
    $language = $_COOKIE['lang']; ///sanitize this later… 
    } 
    
    if (isset($_GET['lang'])) {
    echo "<br>GET:<br>";
    echo $_GET['lang'];  
    $language = $_GET['lang']; //// sanitize
    		
    } 
    
    echo "<br>language global is: ".$language."<br><br>";		
    
    });
    //// end set cookie
    

    in the post template i use this

    	<? echo get_multilingual_field("haupttext"); ?>
    

    and in functions.php i declare the function like this

    
    ///multilanguage field
    function get_multilingual_field($field_name) {
    global $language;	
    
     $value = get_field($field_name.'_'.$language, false, false);
        if (!$value) {
            // no value returned for language
            // get the default
    		
    		
    			// no translation print out a message		
    			if ($language=="en")
    			{
    	        $value = "Sorry, no translation available<br><br>";
    		
    			}		        
            $value.= get_field($field_name,false, false);
        }
    
        return $value;
    }
    	
    

    i can use this approach very flexible, for example
    i have a function to show the date
    and i can modify it to show german/american date very easily

    
    function datumzeit($id) {
    global $language;
    /////std date		
    $folge_std_min= 'j.n.Y, H:i \h';
    
    if($language=="en")
    {
    $folge_std_min= 'n.j.Y, h:i A';
    }		
    $timestamp = get_field("zeitstempel", $id);
    echo date($folge, $timestamp);
    }
    

    i also have a switch function to translate a certain field (based on a dropdown list)…

    and so on, thank you so much for your help!
    what do you think is this a good way to do this?

    PS: Sorry for the not so nice code and the debug-infos…
    PPS: to use this all echos in the first function need to delete

    PPPS: I just disabled wpml and the performance is insane. Backend loads in 2,4 sek. With wpml / string translation etc the loading time was 8 sek!