How to set WP locale based on Weglot current language

Below an example of how to set WP local programmatically

add_filter( 'locale', 'weglot_set_locale' );

function weglot_set_locale( $lang ) {
	if ( function_exists( 'weglot_get_current_language' ) ) {
		$current_language = weglot_get_current_language();
		switch ( $current_language ) {
			case 'sv':
				return 'sv-SE';
				break;
			case 'en':
				return 'en_US';
				break;
			case 'no':
				return 'nb_NO';
				break;
			default:
				return $lang;
		}
	}

	return $lang;
}

Last updated