在 PHP 中管理向用户显示的文本的最佳方法
好吧,这肯定已经被问过并回答了,但我不知何故找不到合适的教程。 我想将文本保留在其他地方向用户显示,并防止我的代码变得太大且不可读。 我的网站不会国际化。我只想拥有某种具有键值结构的文件并从那里获取文本。我想将文本保存在文件中,而不是像一些教程建议的那样保存在数据库中。 我找到了一个可行的解决方案,但我不确定这是否是一个好方法。 我正在考虑使用 parse_ini_file 并将我的文本保存在 .ini 文件中。这种做法有什么问题吗?你能建议更好的东西吗?
Ok for sure this has been asked and answered already but i somehow can't find a proper tutorial.
I want to keep the text displayed to users somewhere else and to prevent my code from becoming too large and unreadable.
My site won't be internationalized. I just want to have some kind of file with key-value structure and get the text from there. I want to keep the text in files, not in the database as some tutorials suggest.
I found a solution which will work but i am not sure whether this is a good approach.
I am thinking of using parse_ini_file and to keep my texts in .ini file. Is there something wrong with this approach? Could you suggest something better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将所有语言数据放入数组中。这很简单,我们还可以添加多语言支持
lang/en.php
lang/ru.php
然后我们可以加载语言:
毕竟 $lang 看起来像:
我们可以非常简单地使用它:
在网站模板的某处:
I put all language data in arrays. Its easy and also we can add multi-language support
lang/en.php
lang/ru.php
Then we can load languages:
After all it $lang will look like:
And we can very simple use it:
Somewhere in the site template:
为什么不使用带有逗号或一些不常见字符的纯文本文件来保存此数据?你可以读取它并将其解析为一个数组
,然后你应该有一个像
$messages[3] = "No soup for you!";
这样的数组,该文件可能看起来像:(
我可能有一些这些函数中放错位置的参数 - 我总是忘记哪个是针,哪个是大海捞针。)
why not use a plain text file with commas or some uncommon character to hold this data? you can read it and parse it into an array with
then you should have an array like
$messages[3] = "No soup for you!";
the file might look like:
(I probably have some of the arguments misplaced in those functions - i always forget which is the needle and which the haystack.)
您可以在脚本中处理数据。在此脚本中,您调用某个源(例如您建议的ini 文件)。然后您使用模板引擎。对于此引擎,您指向模板文件并为模板提供所有变量。
该模板生成 html 并将变量插入到正确的位置。这样,您可以保持 php(业务逻辑)代码干净,远离演示文稿(模板)。您还可以管理一个文件中的变量(ini/xml,但这可能是完全不同的东西)。
对于模板引擎来说,Smarty 是最著名的。还有纯基于 php 的模板系统,只需 Google 即可找到适合您需求的模板系统。
You can process your data in a script. In this script, you call a certain source (e.g. the ini file you suggest). Then you use a template engine. For this engine, you point towards a template file and give the template all the variables.
The template generates the html and inserts the variables at the right place. This way, you keep you php (business logic) code clean, away from the presentation (the template). Also you can manage the variables in one file (ini/xml but this can be something completely different).
For template engines, Smarty is the most known of all. There are also pure php-based template systems, just Google for them to find one that suits your needs.
我确实喜欢这样:
你这样翻译:
function is:
// 注意该函数在那里被简化
正如你所看到的默认情况 deosnt' 需要加载任何东西或执行任何操作,该函数只是返回传递的参数
I do like this:
you translate like this:
function is:
// Note the function is simplified up there
As you can see the default case deosnt' need to load anything or do any operation, the function just returns back the argument passed
我喜欢 lang/en.php 文件的答案。但我不是为每种语言使用一个文件,而是为每个网页(或类等)使用一个文件。这可以降低文件大小,并创建一个 3D 数组:
`return array( "EN" => array( "title" => "Welcome - Good Morning", ...),
“TG”=> array( "title" => "Mabuhay - Magandang Umaga Po", ...)
);'
添加新语言字符串也非常容易...
这使得语言翻译承包商变得非常容易,因为他们可以在 1 个编辑器中看到与外语非常接近的母语,,,
i like the answer with the lang/en.php file. but instead of a file for each language, i use a file for each web page (or class, etc). this keeps file sizes lower and i create a 3D array:
`return array( "EN" => array( "title" => "Welcome - Good Morning", ...),
"TG" => array( "title" => "Mabuhay - Magandang Umaga Po", ...)
);'
Real easy to add new language strings too...
This makes it real easy for language translation contractors since they can see the native language in close proximity to the foreign in 1 editor,,,