English 中文(简体)
Implementing multi languages with CSS and PHP
原标题:

I m building a small site, that needs to support 2 languages for the same page.
Each language has different buttons on the page, the buttons are basically some images with text inside. The positioning of the buttons is done using a CSS file, one for every language. Question is how do I implement the changing of button for every image. I could put a php if statement, and use some other src if lang == English or something else if lang == Russian, Or could implement this with the css file (I could do this with a div and set it s background in the CSS)?
What would you recommend?

Cheers.

最佳回答

You could create three CSS files:

  • One for english buttons
  • One for russian buttons
  • One for the rest

This would basically work this way: You create your buttons with a fallback name, for example

<button id="button_ok">OK</button>

and define different background images in your english/russian CSS files:

(english.css)

#button_ok {
  background-image: url(images/buttons/eng/ok.gif);
}

and (russian.css)

#button_ok {
  background-image: url(images/buttons/rus/ok.gif);
}

All other elements that do not change when the language is chosen come in the third file:

p {
  font-size: 1em; /* whatever*/
}

The last step is to choose at the top of every page which file you want to load:

if ($_GET[ lang ] ==  eng )
    $cssFile =  english.css ;
elseif ($_GET[ lang ] ==  rus )
    $cssFile =  russian.css ;

and include the special and the general css file in your head:

<link rel="stylesheet" type="text/css" href="general.css" />
<link rel="stylesheet" type="text/css" href="<?php echo $cssFile; ?>" />
问题回答

I would recommend using CSS by setting the background and size of the button.

use < button> tag, its better because you can use css like "button{}" without having to put a class on it. Just a remind that < button> has a type like the input and it is "submit" as default if I remember correctly.

Btw, always try to depend on CSS instead of PHP, because on a next manutenance you will be glad that you did it.

or, if you going to use PHP, try using a function instead of hardcoding, even if it is just for one button, we never know if we will use it later.

hope it helped,
Joe





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签