Created
May 22, 2012 12:57
-
-
Save fregante/2768880 to your computer and use it in GitHub Desktop.
Minisito WordPress multilingua
This file contains 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 | |
/* | |
Struttura del sito: | |
• home <- home del sito italiano | |
• chi siamo | |
• contatti | |
• english home <- home del sito inglese (id: 15) | |
• • about us <- sottopagina | |
• • contacts <- sottopagina | |
• french home <- home del sito francese (id: 30) | |
• • about us <- sottopagina | |
• • contacts <- sottopagina | |
bsl() imposta le pagine "home" delle lingue passandogli una serie di id | |
non è necessario passare la lingua italiana visto che non è in una sottopagina | |
uso: bsl(15); //italiano, inglese | |
uso: bsl(15, 30); //italiano, inglese, francese | |
*/ | |
function bsl() { //bfr_set_language | |
global $l, $ids, $post; | |
$l = 0; //0, default, italian | |
$ids = func_get_args(); | |
foreach ($ids as $key => $page) { | |
if(is_page($page) || $post->post_parent === $page) { | |
$l = $key+1; | |
break; | |
} | |
} | |
return $l; | |
} | |
/* | |
bla() sceglie la stringa corretta in base alla lingua, mantenendo l'ordine impostato da bsl(), la prima dovrebbe essere la lingua base del sito, generalmente italiano | |
bla() può scegliere anche tra array | |
uso: echo bla('Contattaci','Contact us'); | |
uso: echo bla(10, 30); | |
uso: $dati = bla( array('Noi', 3) , array('Us', 5) ); | |
*/ | |
function bla() { //bfr_localize_array | |
global $l; | |
$localization = func_get_args(); | |
return $localization[$l]; | |
} | |
/* | |
Questo crea un menu per ogni lingua, da impostare separatamente in Aspetto > Menu | |
*/ | |
add_action( 'init', 'bfr_init'); | |
function bfr_init(){ | |
unregister_nav_menu( 'primary' ); | |
register_nav_menu( 'italian', 'Italiano' ); | |
register_nav_menu( 'english', 'Inglese' ); | |
} | |
/* | |
questo è un extra, aggiunge automaticamente la classe "home" anche alle pagine "english home" e "french home" | |
ovviamente bls() deve essere chiamata prima della funzione body_class(), che è generalmente usata cosi: <body <? body_class(); ?>> | |
*/ | |
add_filter('body_class','set_localized_home'); | |
function set_localized_home($classes) { | |
global $ids; | |
$is_home = false; | |
foreach ($ids as $id) { | |
$is_home = $is_home || is_page($id); | |
} | |
if($is_home) { | |
$classes[] = 'home'; | |
} | |
return $classes; | |
} | |
/*esempio d'uso*/ | |
//lasciare le funzioni bla e bsl in functions.php | |
//header.php: | |
global $is_english; | |
bsl(15); | |
?><!DOCTYPE html> | |
<html dir="ltr" lang="<?php | |
echo bsl('it-IT','en-UK'); //imposta la lingua della pagina | |
?>"> | |
<h1><?php echo bla( 'Benvenuti', 'Welcome' ); ?></h1> | |
<?php wp_nav_menu( array( | |
'theme_location' => bla('italian','english') //imposta il menu corretto | |
) ); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment