How to i internationalize php?

I know I could use an array, or a program like gettext.
But I don’t know how to make an array and I don’t think gettext is working on shared hosting.

Gettext should just work on our hosting. The extension is enabled at least.

By “internationalize PHP”, I presume you mean “internationalize a PHP application” (not the language itself)?

1 Like

yes i want to internationalize my website.

You may use a variable that holds the language desired. According this value you may visualize in your php code one text in one language or another. It may works fine in the case of very few different languages, in other case, the proper use would be insert the diferent texts in each language en a db and read from it according the language used.

1 Like

how would i do this?
i know how to make individual variables.
$variable = Welcome;
$anothervariable = Hello;

you can read about here :grinning:
https://www.php.net/manual/en/book.gettext.php

1 Like

You should do a variable that holds the language, for example:

$Lang. If “0”, by default, English; if “1” Spanish.

Then you should define some arrays for the text

$MyText = array (
array (“Hello”, “Hola”),
array (“Good bye”, “Adiós”)
);

$MyText[0][0] would gives you “Hello”.
$MyText[0][1] would gives you “Hola”.

1 Like

Why not $MyText[0]?

In the way you ask, you will need one variable for each text, using a 2-d array, you will put all the text in both idioms in one variable. As you wish.

2 Likes

I’ve found a easier way:

$MyText = array ( “Hello”=> “Hola”,“Good bye”=> “Adiós”);

$MyText[“Hello”] Will out put Hola :grinning:

2 Likes

How would i Instruct this what language the page is?

confusion. which array uses?

it’s spanish

I know it’s spanish but how would php know which language to display? would i do an if statement or something else?

which language you put them into the array. PHP itself can’t know what language is it!

1 Like

so i want a user to be able to click a button, and using a cookie save that choice, and have the website shown using the language they’ve selected (en,es etc)

can users register your site? not sure about cookie but SESSION would be easier :slight_smile:

theres no registering…

Browsers send an “Accept-Language” header with every request, which indicates the preferred language as the visitor has configured in their web browsers. You could check and use that to decide which language to use.

1 Like

When the server is up again, you can see my own web page

http://abreojosensamblador.epizy.com/

You can see a flag button at the right upper corner, when you click on it you will se a variable appearing in the navigation bar: Lang=1 or Lang=0 that you have to recover to decide which language you will show.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.