Support » Plugin: qTranslate X » How to translate a custom field with qTranslate X?

  • Resolved m4j4

    (@m4j4)


    Hi,
    I have a custom field (created with ACF – Advanced Custom Filed plugin).
    In my theme I call it like that:

    <p><?php the_field ( 'description' ); ?></p>

    How can I translate this custom field with qTranslate X?

    I’ve tried everything …

    In the template file I’ve tried:
    <p><?php __(the_field ( 'description' )); ?></p>
    <p><?php _e(the_field ( 'description' )); ?></p>
    <p><?php the_field __(( 'description' )); ?></p>
    <p><?php the_field _e(( 'description' )); ?></p>

    IN the input field I’ve tried:
    <!–:en–>English Text<–:–><!–:de–>Deutsch Text<–:–>
    [:en]English Text[:de]Deutsch

    … but nothing works. So what should I do to make this custom field translatable with qTranslate X?

    https://wordpress.org/plugins/qtranslate-x/

Viewing 15 replies - 1 through 15 (of 34 total)
  • Plugin Author John Clause

    (@johnclause)

    Search their code and see what they are doing. I did a little bit, try using

    get_field( $field_key, $post_id = false, $format_value = true )

    with $format_value = false, which would make it for your example:

    <p><?php _e(the_field( 'description', false, false )); ?></p>

    If that does not help, I saw that they pass the value through a number of different filters in function get_field_object( $field_key, $post_id = false, $options = array() ) and you can enable applicable for you filters for translation via “Custom Filters” option of qTranslate-X.

    Let me know which way worked for you.

    Thread Starter m4j4

    (@m4j4)

    Hello, John,

    Your trick seems to work!

    If I write in the template file:

    <p><?php _e(get_field( 'description', false, false )); ?></p>

    (Please note that I’ve used get_field(), not the_field().)

    and in the input field one of these two options (they both work):

    [:en]English Text[:de]Deutsch text

    <!–:en–>English Text<!–:de–>Deutsch Text

    (Please note that at the second option I use only the opening <!–:en–> and skip the closing <–:–>. Why so? Because if <–:–> is written in the input field it is also displayed on the page, which is not what I want, off course. That is why I have left it out.)

    it works.

    The output is English Text when I’m on the English site and Deutsch Text when I’m on the Deutsch site!

    Great!!!

    Now, which option of the two do you think is better (more save) to use?

    • [:en]English Text[:de]Deutsch text
    • <!–:en–>English Text<!–:de–>Deutsch Text (Is it save to use it without the closing <–:–>. I have a lot to translate, so rather check first.)

    I’m really happy that this is going to work! Thank you so much for taking your time and helping me!

    Plugin Author John Clause

    (@johnclause)

    Wow, I did not know that <!--:en-->English Text<!--:de-->Deutsch Text without closing <!--:--> would work, but ok if it does, although I would not rely on this to keep working in the future updates, since it is not ‘by design’. I also do not understand why <!--:--> is left shown, it should not be. Probably ACF is doing something to it?

    Anyway if [:] works for you, then I would stick with that one.

    BTW, you may probably use qTranslate-X options “Custom Fields” to make your custom fields to respond to Language Switching Buttons, which would probably be nice for you. It should be one-time configuration task for you. Please, let me know if instructions and FAQ are good enough for you to figure it out.

    Thread Starter m4j4

    (@m4j4)

    As for the <!–:en–>English Text<!–:de–>Deutsch Text: It was me. I’ve written <–:–> instead of <!–:–> (note the exclamation mark). If I write <!–:–> it works.

    In this case which of the two options do you think it is better to use:

    • [:en]English Text[:de]Deutsch text
    • <!–:en–>English Text<!–:–><!–:de–>Deutsch Text<!–:–>

    As for the qTranslate-X options “Custom Fields” – why do you think it is necessary to configure anything? As I have it now (the code in the template file + the special syntax in the input field, as exmplained in the posts above) it does already respond to Language Switching Buttons. In any case I’ll have a look at this options (in the qTranslate X web page) and get back to you if I will not be able to configure it myself. Thanks for offering help.

    ***
    Please note that I’ve got the <–:–> (without the exclamation mark) at qTranslate X official site.
    Here: http://wordpress.org/plugins/qtranslate-x/faq/
    And also here: https://wordpress.org/plugins/qtranslate-x/

    Plugin Author John Clause

    (@johnclause)

    Thank you, @m4j4, for spotting the typos, I corrected FAQ and Description.

    If both ways work, then it does not really matter, it is up to your taste. I would use [:] in single line inputs and <:> in textareas, for example. Those two come into existence because in some places, the value gets through a filter which clears out the HTML comments and <:> gets broken, then [:] works instead. If both work, it does not matter.

    “Custom Fields” would allow you to make the fields configured to respond to Language Switching Buttons, and then you do not have to worry about [:] and <:>, you would edit the language as you do for standard Title and Description fields. It is a convenience feature, you do not need to do anything if you are fine with using [:]<:> in the custom inputs.

    Hello,

    I would like to add a question to this.

    I cant ask my clients to remeber to type [:en]English Text[:fr]french text in each of the custom fields. As a developer I can do it, but I wont ask an end user client to.

    So i created 2 custom fields, one french and one english and I am trying to display them as so

    _e(“<!–:en–>”.the_field(‘sale_type’ ).”<!–:–><!–:fr–>”.the_field(‘sale_type_french’ ).”<!–:–>”);

    Any idea how I can make this work? The methods above do not work.

    Plugin Author John Clause

    (@johnclause)

    Hi @justinblayney, please, give more accurate description. It seems that, first, it should work, and second you should not do it in this way. Could you name specific plugin/theme you are trying? Screenshots?

    Yes, I agree that you cannot ask client to do [:]. This thread is for developers. We discuss plugin/theme integration issues with @franky at https://wordpress.org/support/topic/plugin-integration-1. I guess, you can submit your more accurate description there to include in the discussion other interested people too.

    Hello John,

    I am using
    Advanced Custom Fields plugin
    within
    WCK – Custom Fields and Custom Post Types Creator

    In the old verions of qtranslate I could very simply in any template/wrapper file use

    _e(“<!–:en–>Display when English<!–:–><!–:fr–>Display when French<!–:–>”);

    What this does now is Dump both version of code on the page, which is the same issue the original thread poster had. Your solution works for him, but of course it is very limited.

    The problem is that “Display when English” & “Display when French” could be
    1. Hard coded text or image
    2. One Custom Field
    3. Two different custom fields
    4. a Purple monkey
    5. …anything

    So the solution above only works if the user enters
    [:en]Display when English[:fr]Display when French
    within a SINGLE custom field

    Suppose I want the content to come from different custom fields (e.g. English Custom Field, French Custom field)

    Or within my custom page/field loop I want to hard code some other template items which need to be language specific?

    Basically I need an IF statement or language swapping function for testing which language is active and display the proper content

    IF (Language is English) {
    Do this
    } elseif (Lanuage is French) {
    Do that
    }

    Here is a good example, I have 2 PDFs, one for each language. At this point in time I need to display them both because I dont have a way of testing langauges which could accomodate this

    `$pdf = get_field(‘pdf’);
    if( !empty($pdf) ):
    echo ‘<a href=”‘.$pdf[‘url’].'”>FULL DETAILS</a>’;
    endif;

    $pdf_french = get_field(‘pdf_french’);
    if( !empty($pdf_french) ):
    echo ‘<a href=”‘.$pdf_french[‘url’].'”>Détails</a>’;
    endif;`

    I hope that describes it well, thank you.

    Thread Starter m4j4

    (@m4j4)

    Hi, @justinblayney,

    I’m using qTranslate-X plugin and the following IF statement works for me:

    <?php if( $q_config[ 'language' ] == 'en'): ?>
        something in English
    <?php endif; ?>
    
    <?php if( $q_config[ 'language' ] == 'de'): ?>
        something in Deutsch
    <?php endif; ?>

    Plugin Author John Clause

    (@johnclause)

    @justinblayney, this must work: _e("<!--:en-->Display when English<!--:--><!--:fr-->Display when French<!--:-->"); Something is wrong. I am truly puzzled why it does not. Also, I still do not really understand why you need to modify the code in a so complex way.

    Do you mind giving me a login to your testing server, preferably a separate installation from your copy, so that we would not interfere with each other changes? You may contact me via qtranslateteam at gm a il d c om. I wish to understand what you are trying to achieve and I am not very well familiar with ACF. If I see an example of how you use it and what exactly not working, then I will be able to help efficiently. This sounds to me like integration issue with other plugin, which I am trying to design right now. Your problem seems like a good opportunity to test some ideas. Thank you.

    Hello All,

    Thank you for jumping on board.

    When setting up a test page for you I discovered it works, I think this is the difference

    get_field vs the_field

    My bad, so sorry

    Boolean_Type

    (@boolean_type)

    Hello there!
    I did the following steps, and they seem to have worked! It can be useful, if u don’t use ACF, but will not harm, if u are 🙂
    1. Add the key and value for field. Three variants, all of them work, for example:
    [:ru]Кошечка[:en]Kitty
    <!–:ru–>Кошечка<!–:–><!–:en–>Kitty<!–:–>
    <!–:ru–>Кошечка<!–:en–>Kitty
    2. In “functions.php”:
    add_filter(‘the_meta_key’, ‘qtranxf_gettext’);
    Cause qtranxf_gettext(), as I noticed, is a handler for ‘gettext’ hook, which is used in _e()-like functions. I.e., qtranxf_gettext(), roughly speaking, parses strings for translation.
    3. Output is via the_meta() or something like that.

    I dont know, if it works in any case, but looks like simple solution :))

    Plugin Author John Clause

    (@johnclause)

    There is now new plugin “ACF qTranslate“, which supports -X. Please, direct all question to its author.

    Nicoline2009

    (@nicoline2009)

    Hi

    I installed Qtranslate-X which works very well. The only issue I have is that it doesn´t translate the text from Theme Options.

    Do you know how to fix that?

    This is my website: http://www.houseofrecruitment.dk/en/

    B R Michael

    Plugin Author Gunu

    (@grafcom)

    it doesn´t translate the text from Theme Options.

    Which texts you mean?

Viewing 15 replies - 1 through 15 (of 34 total)
  • The topic ‘How to translate a custom field with qTranslate X?’ is closed to new replies.