Search code examples
phpwordpresswordpress-themingtranslationadvanced-custom-fields

Wordpress how to translae acf plugin fields to another language?


I need to translate Choose File & No file chosen to another language (Japanese).

The following code is from acf field.

I looked into the code but there are no Choose File & No file chosen.

Would you please let me know how to translate them in this case?

enter image description here

<div class="acf-input">
<div class="acf-file-uploader" data-library="all" data-mime_types="" data-uploader="basic">
    <input type="hidden" name="acf[field_60bc51d8898c5]" value="" data-name="id">   <div class="show-if-value file-wrap">
        <div class="file-icon">
            <img data-name="icon" src="" alt="">
        </div>
        <div class="file-info">
            <p>
                <strong data-name="title"></strong>
            </p>
            <p>
                <strong>File name:</strong>
                <a data-name="filename" href="" target="_blank"></a>
            </p>
            <p>
                <strong>filesize:</strong>
                <span data-name="filesize"></span>
            </p>
        </div>
        <div class="acf-actions -hover">
            <a class="acf-icon -cancel dark" data-name="remove" href="#" title="Delete"></a>
        </div>
    </div>
    <div class="hide-if-value">     
        <label class="acf-basic-uploader">
            <input type="file" name="acf[field_60bc51d2899c5]" id="acf-field_60bc51d2899c5">
                               </label>             
    </div>
</div>
</div>



Thank you.


Solution

  • If you want to translate just a handful of words and not the entire plugin, then you could use the following method!

    So, every well-developed plugin/theme has a text domain associated with it, so that you could hook into it and manipulate any text generated by that plugin/theme. For example text domain for woocommerce plugin is woocommerce! For ACF plugin, it's acf!

    Usually you could find the text domain in the main file of the plugin. For ACF plugin you could find it in the acf.php located in the root directory of the plugin. See the screenshot below!

    enter image description here

    After you found the text domain then you could hook into it using gettext, ngettext etc. filter hooks! See the code below:

    add_filter('gettext', 'your_theme_translate_acf_fields', 20, 3);
    
    function your_theme_translate_acf_fields($translated, $text, $domain)
    {
      if ('acf' == $domain) :
        switch ($translated) {
          case 'No file chosen': // THIS IS THE TEXT GENERATED BY THE ACF PLUGIN
            $translated = 'ファイルが選択されていません';
            break;
          case 'Choose File': // THIS IS THE TEXT GENERATED BY THE ACF PLUGIN
            $translated = 'ファイルを選ぶ';
            break;
        }
      endif;
    
      return $translated;
    }
    

    Notice: Capitalization matters!

    Code goes to your functions.php of your active theme. Tested and works!

    Wordpress translation filter hooks:
    gettext
    ngettext