<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Archives des traduction - La programmation sur le web</title>
	<atom:link href="https://programmation.surleweb-france.fr/tag/traduction/feed/" rel="self" type="application/rss+xml" />
	<link>https://programmation.surleweb-france.fr/tag/traduction/</link>
	<description>La programmation gratuite pour tous</description>
	<lastBuildDate>Sat, 08 May 2021 14:16:25 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://i0.wp.com/programmation.surleweb-france.fr/wp-content/uploads/2023/08/cropped-19518430-icone-de-programmation-pour-votre-site-web-mobile-presentation-et-conception-de-logo-gratuit-vectoriel.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>Archives des traduction - La programmation sur le web</title>
	<link>https://programmation.surleweb-france.fr/tag/traduction/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">176210735</site>	<item>
		<title>PHP, réaliser un script multilingue</title>
		<link>https://programmation.surleweb-france.fr/php-realiser-un-script-multilingue/</link>
					<comments>https://programmation.surleweb-france.fr/php-realiser-un-script-multilingue/#respond</comments>
		
		<dc:creator><![CDATA[Zigomato]]></dc:creator>
		<pubDate>Mon, 10 May 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[multilingue]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[traduction]]></category>
		<guid isPermaLink="false">https://programmation.surleweb-france.fr/?p=960</guid>

					<description><![CDATA[<p>Création d'un script en PHP multilingue en utilisant le module gettext, développé sous un environnement Linux</p>
<p class="continue-reading-button"> <a class="continue-reading-link" href="https://programmation.surleweb-france.fr/php-realiser-un-script-multilingue/">Continuer la lecture<i class="crycon-right-dir"></i></a></p>
<p>L’article <a href="https://programmation.surleweb-france.fr/php-realiser-un-script-multilingue/">PHP, réaliser un script multilingue</a> est apparu en premier sur <a href="https://programmation.surleweb-france.fr">La programmation sur le web</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>PHP et Python étant les deux langages que je connais le mieux, dans la lignée de la réalisation d&rsquo;un application Python en multilingue (disponible <a href="https://programmation.surleweb-france.fr/python-creer-des-traductions-pour-son-application/" target="_blank" rel="noreferrer noopener">ici</a>), nous allons voir rapidement la même procédure mais en PHP.</p>



<p>Nous allons utiliser xgettext qui fonctionne sensiblement de la même manière.</p>



<p>Tout d&rsquo;abord, les différents dossiers, un par langue, dans mon cas, l&rsquo;anglais et le français :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mkdir -p locales/fr/LC_MESSAGES
mkdir -p locales/en/LC_MESSAGES
mkdir src</pre>



<p>Maintenant le fichier <strong><em>main.php</em></strong> qui sera enregistré dans le dossier <strong><em>src</em></strong>, je mets l&rsquo;ensemble y compris l&rsquo;exemple sur la pluralité :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
// Mise en place de la langue pour le script
setlocale(LC_ALL, "en_US.utf8");
// Définition du domaine pour la traduction
bindtextdomain("main", "locales");
// Sélection du domaine de texte
textdomain("main");

function print_some_strings(){
        echo _("Bonjour le monde\n");
        echo _("Ceci est une phrase traduite\n");
}

function print_some_plural_strings($num){
        $message1 = ngettext("%d homme", "%d hommes",$num);
        $message2 = ngettext("Je possede %d portables", "Je possede %d portables",$num);

        printf($message1."\n", $num);
        printf($message2."\n", $num);
}

print_some_strings();
print_some_plural_strings(1);
print_some_plural_strings(5);

?></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Dans notre script (par exemple une page web), il faudra venir modifier le paramètre « en_US.utf8 » en fonction soit du choix de l&rsquo;utilisateur, soit de la langue du navigateur.</p></blockquote>



<p>Pour générer le template (<strong><em>.pot</em></strong>) :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xgettext --add-comments src/main.php -o locales/main.pot</pre>



<p>Une fois le template créé, nous allons créer les fichiers de traduction (.po) :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">msginit -l en -i locales/main.pot -o locales/en/LC_MESSAGES/main.po
msginit -l fr -i locales/main.pot -o locales/fr/LC_MESSAGES/main.po</pre>



<p>Les traductions seront à mettre dans les fichiers <strong><em>.po</em></strong>.</p>



<p>Une fois les traductions inscrites dans le marbre, il nous reste à générer le fichier compréhensible par le script :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">msgfmt locales/fr/LC_MESSAGES/main.po -o locales/fr/LC_MESSAGES/main.mo
msgfmt locales/en/LC_MESSAGES/main.po -o locales/en/LC_MESSAGES/main.mo</pre>



<p>Une fois cela fait, quand tu lances ton script, tu vois bien ta traduction.</p>



<p>Pour la mise à jour, on regénère le template :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xgettext --add-comments src/main.php -o locales/main.pot</pre>



<p>Cette fois-ci, on vient fusionner avec le fichier <strong><em>.mo</em></strong> existant (pour ne pas perdre ce qui a déjà été traduit :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">msgmerge --update locales/en/LC_MESSAGES/main.po locales/main.pot
msgmerge --update locales/en/LC_MESSAGES/main.po locales/main.pot</pre>



<p>On mets à jours les traductions et on finit par : </p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">msgfmt locales/fr/LC_MESSAGES/main.po -o locales/fr/LC_MESSAGES/main.mo
msgfmt locales/en/LC_MESSAGES/main.po -o locales/en/LC_MESSAGES/main.mo</pre>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Que ce soit en Python ou PHP, la mise en place de traduction pour son script est très accessible.</p>
<p>L’article <a href="https://programmation.surleweb-france.fr/php-realiser-un-script-multilingue/">PHP, réaliser un script multilingue</a> est apparu en premier sur <a href="https://programmation.surleweb-france.fr">La programmation sur le web</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://programmation.surleweb-france.fr/php-realiser-un-script-multilingue/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">960</post-id>	</item>
	</channel>
</rss>
