开发者

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 changi开发者_如何学Pythonng 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜