简体   繁体   中英

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.

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.

So everything in my database is in English but gets added to my PO file. 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:

$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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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