-
kcs Member
  
-
Posts: 78
Threads: 19
Joined: Sep 2019
Reputation:
5
Hi,
I am adding a language to an existing multi-lingual website, and I am looking for the best way to update existing records in database with the content for the added language. I have several tables that need additional content. One has for example fields such as title_en, content_en, title_nl, content_nl and I am adding title_fr and content_fr. Migration is done so existing records have the 2 added columns. Now I want to populate the content for French.
I have been using seeding to insert content into empty tables very easily and fast, and was wondering if there is an option to update existing content too?
In other words, instead of using in a seeder file something like this for my table 'blocks'
PHP Code: $this->db->table("blocks")->insertBatch($data);
Is there an updateBatch($data) or saveBatch($data) equivalent I could use? I check the documentation but could not find it.
What best approach would you recommend to do this? (I'd like to be able to prepare all the content for the additional language and add it super fast just like through a db: seed command)
Thanks
-
InsiteFX Super Moderator
     
-
Posts: 6,714
Threads: 343
Joined: Oct 2014
Reputation:
246
InsertBatch should work for what you want.
If you need it I have a Google Translate Library that I have converted over to CodeIgniter 4 and php 8+.
It uses CURL and takes a string and translates it to another language.
Let me know if you need it.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
kcs Member
  
-
Posts: 78
Threads: 19
Joined: Sep 2019
Reputation:
5
11-26-2024, 04:59 PM
(This post was last modified: 11-26-2024, 05:37 PM by kcs.)
(11-25-2024, 11:12 PM)InsiteFX Wrote: If you need it I have a Google Translate Library that I have converted over to CodeIgniter 4 and php 8+.
It uses CURL and takes a string and translates it to another language.
Let me know if you need it.
Thanks for offering, I'd definitely be interested in looking at your library: I have a few thousands of comments that need translation in my todo list
(11-25-2024, 11:12 PM)InsiteFX Wrote: InsertBatch should work for what you want.
I managed to find how
I needed to change insertBatch in updateBatch, and add the constraint to target the correct record. In case that is useful to anyone trying to do the same, it looks like this:
PHP Code: $this->db->table("blocks")->updateBatch($data, ['bloc_name']);
-
InsiteFX Super Moderator
     
-
Posts: 6,714
Threads: 343
Joined: Oct 2014
Reputation:
246
I'm still working with it, it's not my work I just modified it to work with CodeIgniter 4.
It does have a maxed character limit of 5000 characters per translation.
Here it is.
ThirdParty\GoogleTranslate.php
PHP Code: <?php
declare(strict_types=1);
/** * GoogleTranslate.class.php * * Class to talk with Google Translator for free. * * @package PHP Google Translate Free; * @category Translation * @author Adrián Barrio Andrés * @author Paris N. Baltazar Salguero <[email protected]> * @copyright 2016 Adrián Barrio Andrés * @license https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0 * @version 2.0 * @link https://statickidz.com/ */
/** * Main class GoogleTranslate * * @package GoogleTranslate * * Modified by Insitefx - For CodeIgniter 4 & PHP 8+ - Date: 08-07-2024 * * Install in CodeIgniter 4.x.x Config/Autoload.php * * public $classmap = [ * 'GoogleTranslate' => APPPATH . 'ThirdParty/GoogleTranslate.php', * ]; * * ----------------------------------------------------------------------- * * EXAMPLE: * * // Translate en -> English to ja -> Japanese * * $source = 'en'; * * $target = 'ja'; * * --------------------------------------- * * $text = <<<EOT * * Hi guys * * I’m looking to learn how to build simple UI’s for web and mobile, so I can try out a couple app of ideas I’ve had. * The only problem is that I have no mental capacity to visualise haha * * https://en.m.wikipedia.org/wiki/Aphantasia * * I’m a devops / platform engineer and can code just fine but need a little help when it comes to putting together * a UI that doesn’t look terrible. What is the best drag and drop tool for building a web UI in bootstrap, so I can * wrap my brain around which elements make a “good” UI? * * Thanks!'; * EOT; * * --------------------------------------- * * $result = gTranslate($source, $target, $text); * * // Good morning * $this->viewData['ja'] = $result; * * * // Translate en -> English to de -> German * $target = 'de'; * * $result = gTranslate($source, $target, $text); * * // Good morning * $this->viewData['de'] = "$result"; * * //dd($this->viewData); * * return view('template', $this->viewData); * */ class GoogleTranslate { /** * Retrieves the translation of a text. 5000 - character limit per translate! * You can get around it by saving the text to a file then opening it in Google Chrome. * * @param string $source Original language of the text on notation xx. For example: es, en, it, fr... * @param string $target Language to which you want to translate the text in format xx. For example: es, en, it, fr... * @param string $text Text that you want to translate * * @return string a simple string with the translation of the text in the target language * @throws Exception */ public static function translate(string $source, string $target, string $text): string { // Request translation $response = self::requestTranslation($source, $target, $text);
// Clean translation return self::getSentencesFromJSON($response); }
/** * Internal function to make the request to the translator service * * @param string $source Original language taken from the 'translate' function * @param string $target Target language taken from the ' translate' function * @param string $text Text to translate taken from the 'translate' function * * @return bool|string The response of the translation service in JSON format * @throws Exception * @internal */ protected static function requestTranslation(string $source, string $target, string $text): bool|string { // 5000 is the Google Translate character limit if (strlen($text) >= 5000) { throw new Exception("Maximum number of characters exceeded: 5000"); }
// Google translate URL $url = "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t";
$fields = array( 'sl' => urlencode($source), 'tl' => urlencode($target), 'q' => urlencode($text) );
// URL-ify the data for the POST $fields_string = "";
foreach ($fields as $key => $value) { $fields_string .= '&' . $key . '=' . $value; }
// Open connection $ch = curl_init();
// Set the url, number of POST vars and POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($fields_string, '&')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Execute post $result = curl_exec($ch);
// Close connection curl_close($ch);
return $result; }
/** * Dump of the JSON's response in an array * * @param string $json The JSON object returned by the request function * * @return string A single string with the translation * @throws Exception */ protected static function getSentencesFromJSON(string $json): string { $sentencesArray = json_decode($json, true); $sentences = "";
if (! $sentencesArray || ! isset($sentencesArray[0])) { throw new Exception("Google detected unusual traffic from your computer network, try again later (2 - 48 hours)"); }
foreach ($sentencesArray[0] as $s) { $sentences .= $s[0] ?? ''; }
return $sentences; }
} // End of GoogleTranslate Class.
/** * ----------------------------------------------------------------------- * Filename: GoogleTranslate.php * Location: ./app/ThirdParty/GoogleTranslate.php * ----------------------------------------------------------------------- */
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
davis.lasis Member
  
-
Posts: 64
Threads: 3
Joined: Oct 2018
Reputation:
4
11-27-2024, 03:43 PM
(This post was last modified: 11-27-2024, 03:54 PM by davis.lasis.)
CI 4.5.5
PHP 8.3
Ignore my AlertLibrary, you can handle alert notifications as you fancy and/or need
Usage
PHP Code: service('GoogleTranslateLibrary')->translate('en', 'fr', 'Hello world!');
env
PHP Code: google_translate_endpoint = 'https://translate.googleapis.com/translate_a/single' google_translate_limit = 5000
Init Service
PHP Code: <?php namespace Modules\Main\Config;
use CodeIgniter\Config\BaseService; use Modules\Main\Libraries\GoogleTranslateLibrary;
class Services extends BaseService { /** * Google Translate library * @param bool $getShared * @return object */ public static function GoogleTranslateLibrary(bool $getShared = true): object { return ($getShared) ? static::getSharedInstance('GoogleTranslateLibrary') : new GoogleTranslateLibrary(); } }
Library (ThirdParty) class
PHP Code: <?php namespace Modules\Main\Libraries;
use Config\Services;
class GoogleTranslateLibrary { private string $url = ''; private int $limit = 0; // Google Translate character limit
public function __construct() { $this->url = env('google_translate_endpoint'); $this->limit = env('google_translate_limit'); }
/** * Retrieves the translation of a text. 5000 - character limit per translate! * You can get around it by saving the text to a file then opening it in Google Chrome. * https://github.com/ssut/py-googletrans/issues/268 * * @param string $source Original language of the text on notation xx. For example: es, en, it, fr... * @param string $target Language to which you want to translate the text in format xx. For example: es, en, it, fr... * @param string $text Text that you want to translate * * @return string a simple string with the translation of the text in the target language */ public function translate(string $source, string $target, string $text): string { $output = '';
if (strlen($text) >= $this->limit) { MainLibrary::AlertLibrary()->set('danger', 'Google Translate: '.lang('Main.alert.max_char_exceeded', [$this->limit])); } else { $params = [ 'client' => 'gtx', 'dt' => 't', 'sl' => $source, 'tl' => $target, 'q' => $text, ];
$url = $this->url.'?'.http_build_query($params);
$response = Services::curlrequest()->post($url);
$data = json_decode($response->getBody() ?? '', true);
if (!empty($data[0]) && is_array($data[0])) { foreach ($data[0] as $result) { if (!empty($result[0]) && is_string($result[0])) { $output .= $result[0].' '; } } } }
return trim($output); }
}
|