PHP中根据语言动态更改图像目录?

发布于 2024-12-12 11:45:39 字数 982 浏览 2 评论 0原文

我正在制作一个框架,用它来构建多种语言的中小型网站。我正在使用本教程中的方法:

http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html

我知道这是 2009 年的,但我喜欢他们的做法,这将是最简单的为了让我的同事们理解,特别是因为我自己已经从文件中评论了天哪。

这是替换网站中相关文本的一个很好的解决方案。我现在面临的问题源于这样一个事实:并非任何给定网站上的所有单词都一定是文本。还有大量图像、导航按钮等也需要更换。

我的想法是进入 common.php (如教程中所述)并添加一个条件来更改图像目录(img/ 或 image/)。例如,如果我的文件结构如下所示:

www

<块引用>

index.php
common.php
/img
/jp

<块引用>

lang.jp.php
/img

如果用户选择日语作为其语言,并且运行脚本以从 lang.jp.php 获取所有文本,我也希望使用 /jp 中 /img 目录中的图像。

所以我的问题是,如何将 src 更改为 html 中每个图像标签的正确 /img 目录?是否可以使用 php 开关并将 src 设置为变量,然后在声明图像名称之前调用该变量,例如 ,并将路径动态分配给$imgsrc?如果是这样,设置该变量的最佳方法是什么?

I'm working on making a skeleton from which to build small to medium-sized sites in multiple languages. I'm using the method from this tutorial:

http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html

I know it's from 2009 but I like how they did it, and it will be the easiest to understand for my coworkers, especially since I've commented the bejeezus out of the files myself.

This is a good solution for replacing relevant text in the site. The problem I'm facing now stems from the fact that not all the words on any given site are necessarily text. There are plenty of images, navigation buttons, and so on that would need to be replaced as well.

My thought is to go into common.php (as described in the tutorial) and add a conditional that would change the image directory (img/ or image/) as well. For example, if my file structure looked like this:

www

index.php
common.php
/img
/jp

lang.jp.php
/img

If the user selects Japanese as their language and the script runs to get all the text from lang.jp.php, I would want to be using the images in the /img directory within /jp as well.

So my question is, how would I change the src to the proper /img directory for each image tag within the html? Is it possible to use a php switch and set the src to a variable and just call that variable before stating the image name, like <img src="<?php echo($imgsrc);?>/image.jpg" />, and assign the path dynamically to $imgsrc? If so, what's the best way to set that variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无言温柔 2024-12-19 11:45:39

@OpenCode 的想法是正确的,但继续使用您拥有的 img 文件夹并在其中为每种语言创建一个新文件夹可能会更容易。

/img
    /jp
    /en
    /es
        /image.jpg

然后使用:

<img src="/img/<?php echo $lang; ?>/image.jpg" />

这样,如果您决定稍后添加更多语言,您的网络根目录就不会变得太混乱。

@OpenCode had the right idea, but it might be easier to, instead, keep using the img folder you have and make a new folder within it for each language.

/img
    /jp
    /en
    /es
        /image.jpg

Then use:

<img src="/img/<?php echo $lang; ?>/image.jpg" />

This way your webroot won't become too messy should you decide to add more languages later on.

隔岸观火 2024-12-19 11:45:39

$lang 变量保存短语言代码(语言名称)

如果“jp”目录中有“jp”目录和“img”目录,那么您也可以执行以下操作。

<img src="<?php echo $lang; ?>/img/image.jpg" />

使它成为

<img src="jp/img/image.jpg" />

The $lang variable holds the short-language-code (language name)

If you have "jp" directory and "img" directory inside "jp" directory then you can do the following too.

<img src="<?php echo $lang; ?>/img/image.jpg" />

so that it becomes

<img src="jp/img/image.jpg" />
究竟谁懂我的在乎 2024-12-19 11:45:39

很抱歉,您所依赖的教程只是冰山一角。

如果你想制作 100% 开放、简单、快速的东西,请像我一样(厚颜无耻):

1 - 制作非常简单的语言文件,例如仅包含关联的“ini”文件,例如:

text1 = my text
text2 = my text2
text3 = my text3
email_customer = Dear customer
...

这样你就可以很容易地翻译它了。

2 - 然后创建一个“php”缓存文件,该文件创建您将包含的“类”文件。
3 - 创建一个包含翻译内容的翻译器“php”类文件。您可以在其中编写如下代码:

if language file more recent than cache file
  => create cache file
...
code to generate "language cache file"
...
include "language cache file"
...

3 - 将您的语言文件基于主机:使用 $_SERVER['HTTP_HOST']。我的做法如下:

- if it's "fr.mysite.com" => it's french language
- if it's "es.mysite.com" => it's spanish language
- ... and so on ...
- if it's "www.mysite.com" => it's default language (your choice)

第 1 步非常非常重要,因为只需向译员发送适当的文件即可节省大量时间。不再头痛了。您将花费 2-3 天来编写它,每种新语言将为您节省 4-5 天的时间。

对于最后一个(也是最难的)问题:(仅供参考,这是我第四次重写整个框架以包含这些内容,我在这里解释的内容代表了许多个月的开发,尽管它看起来很简单):多语言文件。
我的目录是这样的:

templates/
|-- common
|   |-- css
|   |-- htm
|   |   `-- intranet
|   |-- img
|   |-- js
|   |   `-- openid
|   |-- pdf
|   `-- txt
|-- fr
|   |-- css
|   |-- htm
|   |-- img
|   |-- js
|   |-- pdf
|   `-- txt
`-- us
    |-- css
    |-- htm
    |-- img
    |-- js
    |-- pdf
    `-- txt

在您的“翻译器类文件”代码中执行以下操作:

...
include "language cache file"
...
**after the include**
...
function get_expanded_path_file() {
  if file exists in language template directory
    return full "language" path + filename
  if file exists in "common" template directory
    return full "common" path + filename

  return "/" filename
}
...

现在我希望您了解整个想法。如果您想了解此类框架的实际操作,请查看我的最新网站(如果您认为它是垃圾邮件或其他内容,请删除我的链接):我的网站
如果你明智地使用我的建议来处理像 Smarty 这样的东西,你可能会像我一样:只做 Php 基本文件,把一切交给翻译人员和网页设计师。仅供参考,这个网站花了我两周的时间来开发,1 天的时间翻译成法语和西班牙语,然后...... 3 周的时间来同意网页设计(哈哈(或讽刺谁知道))!

希望这有帮助

I'm sorry but the tutorial you're relying on is just the top of the iceberg.

If you want to make something that is 100% open, easy and fast, do like me (cheekyness taken apart):

1 - make very simple languages file, like "ini" files containing only associations, e.g.:

text1 = my text
text2 = my text2
text3 = my text3
email_customer = Dear customer
...

This way you will be able to translate it very easily.

2 - Then make a "php" cache file that creates a "class" file you'll include.
3 - Make a translator "php" class file that will include translation stuff. In it you may write code like that:

if language file more recent than cache file
  => create cache file
...
code to generate "language cache file"
...
include "language cache file"
...

3 - Base your language file on the host: use $_SERVER['HTTP_HOST']. Here's how I do:

- if it's "fr.mysite.com" => it's french language
- if it's "es.mysite.com" => it's spanish language
- ... and so on ...
- if it's "www.mysite.com" => it's default language (your choice)

The #1 step is very important because you'll gain a lot of time by just sending appropriate files to the translators. No headaches anymore. You'll spend 2-3 days to write it, you'll gain 4-5 days for each new language.

And for the final (and hardest) problem: (FYI it's the 4th time I rewrite my whole framework to include those things, and what I'm explaining here represents many months of developpement even though it seems simple): multilangage files.
Here's how is my directory:

templates/
|-- common
|   |-- css
|   |-- htm
|   |   `-- intranet
|   |-- img
|   |-- js
|   |   `-- openid
|   |-- pdf
|   `-- txt
|-- fr
|   |-- css
|   |-- htm
|   |-- img
|   |-- js
|   |-- pdf
|   `-- txt
`-- us
    |-- css
    |-- htm
    |-- img
    |-- js
    |-- pdf
    `-- txt

And in your "translator class file" code do something like:

...
include "language cache file"
...
**after the include**
...
function get_expanded_path_file() {
  if file exists in language template directory
    return full "language" path + filename
  if file exists in "common" template directory
    return full "common" path + filename

  return "/" filename
}
...

Now I hope you get the whole idea. If you want to see real action of such a framework take a look at my latest website (remove my link if you think it's spam or whatever): mysite
If you use my advice wisely with something like Smarty, you may end up like me: just doing Php base file, and let everything to translators, and to the webdesigner. FYI this website took me two weeks to develop, 1 day to translate in French and Spanish, and... 3 weeks to agree with the webdesign (lol (or sarcasm who knows))!!!

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文