Translate or hide
-
Hi,
Hi,
Is there a way to translate TopBar within function.php?
I’m using wpml and have used code to translate other features on my website. I just need the code that shows the text of the top bar. Can you please tell me if possible?Or else, is it possible to hide the Top bar when you have e.g. English and show it only in another language?
Thanks in advance,
George
-
Hi,
were you able to make the translation work? I am trying to do the same thing.
Thanks
MaxNop
Hi there,
I completely didn’t see this topic/question, only got the notifications for the last two answers. It seems that we weren’t notified from WordPress before for some reasons…
We haven’t done anything to make the Top Bar message text (and button) translatable yet but we are rewriting our plugins (a few more to go) and Top Bar is in the list.
We will look into this. Have you tried using WPML shortcodes for this along with the String Translation add-on? https://wpml.org/documentation/support/wpml-coding-api/shortcodes/
I haven’t tried it myself, but I think it’s the best option since the Top Bar message/button text is a special kind of content (not a Post/Page/Menu/Custom type/Widget kind of content).
Sorry for not responding earlier and for the inconvenience caused.
- This reply was modified 6 years, 11 months ago by WP Darko.
Ok no problem for the delay.
I’m not using String translation add-on. Should it be easier if we could have more than one top bars at the same time? We could be easy create two for two languages.
Hi,
I’m using a different plugin but my “fix” might be working with this one too.
I’ve added something in my functions.php so that the WPML language code is added as a body class. So for example, my code ends up looking like this in french :<body class="fr"> <div class="topbar"> <span class="fr">FR content</span> <span class="en">EN content</span> </div> </body>
Then with some css you can display none the non-current language content
body.fr span.en, body.en span.fr {display:none;}
Code to append language to body class to include in functions.php
// Add wpml body class add_filter('body_class', 'wpml_body_class'); function wpml_body_class($classes) { $classes[] = ICL_LANGUAGE_CODE; return $classes; }
I know that this is far from ideal solution, but it works for me as a quick solution for a temporary promotion with topbar. For this to work, the topbar message field must accept html so you can wrap the content in span with a class.
Thanks!
- This reply was modified 6 years, 11 months ago by maxstjb. Reason: typo
Hi and thanks for the reply. I’m using already code with ICL_LANGUAGE_CODE from WPML to do other stuff, but I don’t know how to use it with this plugin.
Could the author help me please?
Hi there,
I’m not familiar with WPML and ICL_LANGUAGE_CODE is a PHP constant from what I understand and you cannot use it in the Top Bar message field since it’s PHP.
The best way in my opinion would be to declare the string with something like this:
[wpml-string context="my-domain" name="my-name"]My string[/wpml-string]
And then translate it from within WPML with the string translation module.
See more info here:
https://wpml.org/documentation/support/wpml-coding-api/shortcodes/#wpml-stringI don’t have any other solution for the moment, we’ll be working on this plugin soon and hopefully I’ll have something better for this!
Sorry for the inconvenience caused.
- This reply was modified 6 years, 11 months ago by WP Darko.
As I said in my previous reply, I haven’t purchased the string translation module. If you could give me the code for changing the message and the button message of the top bar I believe can do it easily.
Example:
if ( ICL_LANGUAGE_CODE==’en’ ) {
// code for english message and button message of top bar
}
else {
// code for greek message and button message of top bar
}Thanks
Sorry, I don’t understand what you ask me to do. You cannot insert PHP code in the message field. Are you trying to do this or something else?
I’m not a native English speaker, please give me more details so I can understand.
I understand this
if ( ICL_LANGUAGE_CODE==’en’ ) { // code for english message and button message of top bar } else { // code for greek message and button message of top bar }
But you can’t paste this in the field.
I want to edit/change the Message, Button Text and Button URL in functions.php.
Where I say “// code for…” is the code for the fields i want to change on runtime.Hi there,
There’s no “code for changing the message and the button message of the top bar” to insert in your functions.php file that we can provide you with.
if ( ICL_LANGUAGE_CODE==’en’ ) { // code for english message and button message of top bar } else { // code for greek message and button message of top bar }
This code is just a conditional statement that checks what language is currently displayed on your site.
The plugin retrieves its options in the topbar.php file and the bar itself is displayed using JavaScript. We do not have a pre-made code to edit the message like this.
As I said earlier we will implement a way to facilitate translation in the future but for now the best is to use string translation. I’m not saying you cannot achieve this in PHP, but you’d have to be familiar with the language and what you do might not be future-update-proof.
Sorry for the inconvenience caused.
Ok thanks.
Hi there,
It’s important for us that you know that the previous message is not some bu****** so this topic can be closed. We’ll be thinking about this and we know it’s an issue, and that it has to be fixed.
In the meantime we’ll take a look at the code and come back to you if we find something that works the way you asked.
Seriously sorry for the inconvenience.
Ok, let me know. Thank you very much.
Alright, I haven’t tested this and I cannot say for sure that it will work, it depends on a lot of things. If I was to create a PHP snippet that does what you want, this is where I would start:
// English language if ( ICL_LANGUAGE_CODE == 'en' ) { // Top Bar message update_option( 'tpbr_message','Your message here' ); // Top Bar button text update_option( 'tpbr_btn_text','Button text here' ); // Top Bar button URL update_option( 'tpbr_btn_url','https://button-url-here.com' ); // (Example) French language } else if ( ICL_LANGUAGE_CODE == 'fr' ) { update_option( 'tpbr_message','Votre message ici' ); // message update_option( 'tpbr_btn_text','Texte du bouton ici' ); // button text update_option( 'tpbr_btn_url','https://url-du-boutton-ici.fr' ); // button url // START — Duplicate this block for adding new languages. ——————— } else if ( ICL_LANGUAGE_CODE == 'your-language-code-here' ) { update_option( 'tpbr_message','your-message' ); // message update_option( 'tpbr_btn_text','your-button-text' ); // button text update_option( 'tpbr_btn_url','https://your-button-url' ); // button url } // END — Duplicate this block for adding new languages. ——————— // For any language that isn't specified above. else { update_option( 'tpbr_message','default-message' ); // message update_option( 'tpbr_btn_text','default-button-text' ); // button text update_option( 'tpbr_btn_url','https://default-button-url' ); // button url }
Hope it helps you get started, as said above, I have not tested this, it’s just a one-shot snippet (I commented it so it’s clearer, I don’t know your PHP skill level).
I have made sure it doesn’t crash your site though… no worries.
- The topic ‘Translate or hide’ is closed to new replies.