Created
January 3, 2020 22:03
-
-
Save mingodev/a590ef874f0439c817f86da85db4e3d2 to your computer and use it in GitHub Desktop.
Hard-coded custom traductions in WP (for when WPML isn't enough)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Custom translations | |
add_action('wp_enqueue_scripts', function(){ | |
wp_enqueue_script('translations-js', get_stylesheet_directory_uri() . '/assets/js/translations.js'); | |
}); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var project = project || {}; | |
project.translations = function ($) { | |
"use strict"; | |
let initialize = function () { | |
// Call functions here | |
translateOneItemInCart($('.woocommerce-cart-form h2')); | |
}; | |
// Put custom translation functions here. | |
// - Example: | |
let translateOneItemInCart = function (selector) { | |
if (!selector.length) return; | |
if ('You Have 1 Item In Your Cart' !== selector.html()) return; | |
selector.each(function () { | |
translate($(this), 'You Have 1 Item In Your Cart', 'Vous avez 1 article dans votre panier'); | |
}); | |
}; | |
// The actual translation process | |
let translate = function (selector, oldText, newText) { | |
let content = selector.html(); | |
let newContent = content.replace(oldText, newText); | |
selector.html(newContent); | |
}; | |
return { | |
init: function () { | |
initialize(); | |
} | |
}; | |
}(jQuery); | |
jQuery(document).ready(function () { | |
project.translations.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment