简体   繁体   English

PHP-SQL - 国际化和本地化 - Gettext-POedit

[英]PHP-SQL - Internationalization and Localization - Gettext-POedit

I'm creating a site that I want to display in multiple languages for different countries, but this is my first time doing this and not sure what best practices are.我正在创建一个网站,我想以多种语言为不同的国家/地区显示,但这是我第一次这样做,不确定最佳做法是什么。

So for all my static text in my code I just wrap it in the translate function and POedit is able to extract the strings.因此,对于代码中的所有静态文本,我只需将其包装在 translate 函数中,POedit 就能够提取字符串。

Then for all my dynamic text that is being generated via PHP from my DB, when I add something new to my DB on the back end, I also do a file_put_contents() into the PO file as well so I have those strings to be translated.然后,对于从我的数据库通过 PHP 生成的所有动态文本,当我在后端向我的数据库添加新内容时,我也会在 PO 文件中执行file_put_contents()以便我有这些字符串要翻译.

So everything in my database is in English but gets added to my PO file.所以我数据库中的所有内容都是英文的,但会添加到我的 PO 文件中。 The issue is I also have a search bar.问题是我也有一个搜索栏。 This searches my DB for various items but can only search in English.这会在我的数据库中搜索各种项目,但只能用英语搜索。

What I am trying to do at the moment is something like this, where I translate the actual input using Google Translate API and search in English:我目前正在尝试做的是这样的事情,我使用 Google Translate API 翻译实际输入并用英语搜索:

$term = $_POST['search_text'];
$result = $translate->translate($term, [
    'target' => 'en'
]);
$translation = $result['text'];
$new_term = '%'.$translation.'%';
$params = [$new_term];
$sql = "SELECT * FROM products WHERE product_name LIKE ?";
$stmt = DB::run($sql,$params);

Obviously this isn't perfect because I'm relying on Google to do translate the search terms, but it is picking up the right results in general.显然这并不完美,因为我依靠谷歌来翻译搜索词,但它总体上能获得正确的结果。

Is there a better way of doing this?有没有更好的方法来做到这一点? This seems like I'm going about it the wrong way.这似乎是我走错了路。

我最终做的是用英语搜索并搜索翻译并组合结果,因此它有望获得一切。

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

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