Support

Account

Home Forums General Issues Use ACF as multilanguage solution

Solved

Use ACF as multilanguage solution

  • Hey!

    this is more theoretical at the moment

    I am working on a site with many acfields. And i need to have the page in two languages. At the moment i am testing wpml but it does not really fit.

    So my idea was, to use acf itself to have multilanguage function.
    For example i have the field “text” and can add the field “text_english” for the english-text. Doing this I can translate only the fields that are necessary, for example there is no need to duplicate informations like dates, names and so on.

    Do somebody have a similar approach? Could this be done easily?
    I think it would be great due to complexity and performance.

    Is it possible? Is there f.e. an easy way to switch the fields? For example the user uses the english version, all fields would be switched to the english ones. For example “text” will be “text_english”, “title” will be “title_english” and so on…

    Maybe somebody has experiences with this
    Thanks for any hints.
    All the best

  • WPML is supposed to be the best multilingual plugin available, but like you I’m not completely sold on it.

    In order to have a site with multiple languages you’ll need to have some setting that is global to tell what language the site is supposed to be displaying. I would use this variable. I would also stick with standard language codes like “en”, “fr”, “es”, etc. I would then name all of my fields consistently using that as either a prefix of suffix to my field name.

    You could then create a function something like the following. This is just a quick idea. In reality I’d more than likely make this a filter or something more complicate because this does not account for all the details, but it would work.

    
    function get_multilingual_field($field_name) {
        global $language;
        $value get_field($field_name.'_'.$language);
        if (!$value) {
            // no value returned for language
            // get the default
            $value = get_field($field_name);
        }
        return $value;
    }
    
  • 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!

  • It seems like a good idea. There are a few things about WPML that I find I don’t like, performance and the fact that the interface is not as intuitive as I’d like it to be. I’m told that as the number of languages increases that the performance, both front and back end is significantly impacted. Performance with ACF can also be impacted with extremely large field groups, but this can be mitigated to some degree with some thoughtful logic. The other thing that I did not like was the number of warnings and errors that it produced with error reporting on, especially when trying to run cron jobs where the errors caused the cron to break. Granted, it’s been a while since I tried it and they are supposed to be improving it.

    It looks like you’re well on your way to creating something that is both quicker and more usable than storing each language as a separate post with all the fields duplicated even when they don’t really need translation. This is a simpler approach and I like simpler approaches without a lot of over thinking.

  • Part 2:
    I have a futher question: is it possible to provide in the backend a possibility to have a second field for a translation on demand? Lets say, i have some fields with text and for this certain fields i have a button that says something like “need to translate” and by clicking this button a field will be created (named like the original field plus prefix/suffix).

    Or is it better (for performance / databse…) to decide width fields should or could be translate and to provide a second field for all this fields.

    Or is it better to work with a conditional logic, to provide a second field?

    thanks alot!

  • Performance wise, it may be better to simply provide the fields and use the content if they are completed. This means only one if statement and one or two requests depending on if the first returns something.

    On the other hands, see the WP doc on get_post_meta() and calling it without specifying a meta key.

    For usability, it would likely be better to provide a checkbox field for the user to select the other languages they want to translate to. This would meant you’d always need to perform at least to requests and possibly a 3rd.

    1 – see if translation is on for a language
    2 – ? Get the content for the language if it’s selected
    3 – ? Get the default language if translation is not selected or if 2 returns no value

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.