Skip to content

Instantly share code, notes, and snippets.

@mingodev
Created January 3, 2020 22:03
Show Gist options
  • Save mingodev/a590ef874f0439c817f86da85db4e3d2 to your computer and use it in GitHub Desktop.
Save mingodev/a590ef874f0439c817f86da85db4e3d2 to your computer and use it in GitHub Desktop.
Hard-coded custom traductions in WP (for when WPML isn't enough)
<?php
// Custom translations
add_action('wp_enqueue_scripts', function(){
wp_enqueue_script('translations-js', get_stylesheet_directory_uri() . '/assets/js/translations.js');
});
?>
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