diff --git a/modules/widgets/google-translate.php b/modules/widgets/google-translate.php new file mode 100644 index 0000000000000..d56d0859caaba --- /dev/null +++ b/modules/widgets/google-translate.php @@ -0,0 +1,105 @@ + __( 'Automatic translation of your site content', 'jetpack' ) ) + ); + wp_register_script( 'google-translate-init', plugins_url( 'google-translate/google-translate.js', __FILE__ ) ); + wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', [ 'google-translate-init' ] ); + } + + /** + * Display the Widget. + * + * @see WP_Widget::widget() + * + * @param array $args Display arguments. + * @param array $instance The settings for the particular instance of the widget. + */ + public function widget( $args, $instance ) { + // We never should show more than 1 instance of this. + if ( null === self::$instance ) { + /** This filter is documented in core/src/wp-includes/default-widgets.php */ + $title = apply_filters( 'widget_title', $instance['title'] ); + echo $args['before_widget']; + if ( ! empty( $title ) ) { + echo $args['before_title'] . esc_html( $title ) . $args['after_title']; + } + wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) ); + wp_enqueue_script( 'google-translate-init' ); + wp_enqueue_script( 'google-translate' ); + echo '
'; + echo $args['after_widget']; + self::$instance = $instance; + // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath. + // This is a hack to show google translate bar a bit lower. + if ( is_admin_bar_showing() ) { + echo ''; + } + } + } + + /** + * Widget form in the dashboard. + * + * @see WP_Widget::form() + * + * @param array $instance Previously saved values from database. + */ + public function form( $instance ) { + if ( isset( $instance['title'] ) ) { + $title = $instance['title']; + } else { + $title = ''; + } + ?> +

+ + +

+ Widgets. + */ +function jetpack_google_translate_widget_init() { + register_widget( 'Google_Translate_Widget' ); +} +add_action( 'widgets_init', 'jetpack_google_translate_widget_init' ); diff --git a/modules/widgets/google-translate/google-translate.js b/modules/widgets/google-translate/google-translate.js new file mode 100644 index 0000000000000..a192979ae818e --- /dev/null +++ b/modules/widgets/google-translate/google-translate.js @@ -0,0 +1,20 @@ +/*global google:true*/ +/*global _wp_google_translate_widget:true*/ +/*exported googleTranslateElementInit*/ +function googleTranslateElementInit() { + var lang = 'en'; + var langParam; + var langRegex = /[?&#]lang=([a-z]+)/; + if ( typeof _wp_google_translate_widget === 'object' && typeof _wp_google_translate_widget.lang === 'string' ) { + lang = _wp_google_translate_widget.lang; + } + langParam = window.location.href.match( langRegex ); + if ( langParam ) { + window.location.href = window.location.href.replace( langRegex, '' ).replace( /#googtrans\([a-zA-Z|]+\)/, '' ) + '#googtrans(' + lang + '|' + langParam[ 1 ] + ')'; + } + new google.translate.TranslateElement( { + pageLanguage: lang, + layout: google.translate.TranslateElement.InlineLayout.SIMPLE, + autoDisplay: false + }, 'google_translate_element' ); +}