简体   繁体   English

PHP如何在配置文件中处理不同的语言?

[英]PHP How to handle different languages in config file?

I am working on a project and there are going to be 3 different languages: English, French and Spanish. 我正在做一个项目,将有3种不同的语言: 英语,法语和西班牙语。 This will be defined when the user signs up. 这将在用户注册时定义。

Now in my config file I have the following: 现在在我的配置文件中,我有以下内容:

define("DEFAULT_SLOGAN", "The default slogan will go here.");

Until I started realizing that I needed to accept different languages. 直到我开始意识到自己需要接受不同的语言。

The user has an assigned language code (EN, FR, SP). 用户具有指定的语言代码(EN,FR,SP)。 How would I go about having different language strings for each page? 如何为每个页面使用不同的语言字符串? Would I need to have something like this: 我需要这样的东西吗:

define("DEFAULT_SLOGAN_EN", "Slogan in english");
define("DEFAULT_SLOGAN_FR", "Slogan in french");
define("DEFAULT_SLOGAN_SP", "Slogan in spanish");

And for each string just have 3 different versions of it? 每个字符串只有3个不同的版本? Not too sure the best way to approach this. 不太确定解决此问题的最佳方法。

Thanks! 谢谢!

A simple strategy that is often used are arrays: 经常使用的简单策略是数组:

$lang['en'] = array(
    'DEFAULT_SLOGAN' => 'The default slogan will go here.',
);
// Same for other languages

Then, in your actual code, make sure that $lang is available (you can use the global keyword for this) and use these arrays. 然后,在实际代码中,确保$ lang可用(可以使用global关键字)并使用这些数组。

A better approach would be to create 更好的方法是创建

  • locale_definitions_EN.php containing locale_definitions_EN.php包含

    define("DEFAULT_SLOGAN", "The english default slogan will go here."); define(“ DEFAULT_SLOGAN”,“英语默认口号会在这里。”);

  • local_definitions_FR.php containing local_definitions_FR.php包含

    define("DEFAULT_SLOGAN", "The french default slogan will go here."); define(“ DEFAULT_SLOGAN”,“法语默认口号会在这里。”);

etc. and then do something like 等,然后做类似的事情

include "locale_definitions_$userLocale.php";

This has the advantage, that you don't need the memory to hold the unused other language constants. 这样做的好处是,您不需要内存来保存未使用的其他语言常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM