So I need to translate the records from the database, Im searching a way to do this 'automatic' way but I cannot find any solution. I'm using astrotomic/laravel-translatable and I created a new Provider for the 'Food' in en_US and in hr_HR. So I seed the database with both en and hr, but they give random stuff where the translations are not correct. For example.... Food name is 'pasta' in en(english), and 'pasta' in hr(croatian) is 'tjestenina'... Can i seed the database with just 'pasta' and do some stuff to translate it automatically or I need to do this by hand. Here's the code....
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Category;
class MealFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
# When installed via composer
require_once 'vendor/autoload.php';
$en_faker = \Faker\Factory::create('en_US');
$hr_faker = \Faker\Factory::create('hr_HR');
$title_en = $en_faker->unique()->food;
$title_hr = $hr_faker->unique()->food;
$desc_en = $en_faker->text;
$desc_hr = $hr_faker->text;
return [
'category_id' => rand(null, 5),
'en' => [
'title' => $title_en,
'description' => $desc_en
],
'hr' => [
'title' => $title_hr,
'description' => $desc_hr
],
'status' => 'created'
];
}
}
and this is the seeder...
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Meal;
use App\Models\Tag;
use App\Models\Ingredient;
class MealSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Meal::factory()->count(10)->create();
foreach (Meal::all() as $meal) {
$tags = Tag::inRandomOrder()->take(rand(1, 8))->pluck('id');
$ingredients = Ingredient::inRandomOrder()->take(rand(1, 6))->pluck('id');
$meal->tags()->attach($tags);
$meal->ingredients()->attach($ingredients);
}
}
}
CodePudding user response:
If you don't want to store your translated record on database at all your data is dynamic You can use a third-party api for translation realtime and you may create a Controller for using that translation api and then show it to user.
If your data is static you can also use a translation api on your seeder
, by reading English words statically from database and translate it to Croatian using api and store it on database
Otherwise you should you need to do it by hand.
In case you are not familiar with using a third-party this link might help you
CodePudding user response:
Why don't you just initialize the database with the boostrap English data and write a snippet (probably using a translation library) to translate your English data to Croation (and store it in the correponding database field)?
BTW, it might be better practice to store these data in e.g., POT files (used in Wordpress etc.) for more flexible internationalization (i18n).