AlwaysStudent
AlwaysStudent

Reputation: 1374

Language translate grammar

I am trying to add another language for my project.

I am using database table for words. Like this screenshot.

We know that languages can show differences in subject and predicate. For example:

English: Mustafa went to the cinema with his friend ahmet today.

Turkish: Mustafa bugün arkadaşı ahmet ile birlikte sinemaya gitti.

English - Turkish

today => bugün

sinema => cinema

birlikte => with

gitti => went

Mustafa and ahmet is username from users table. My question is how can I show the words with good grammars?

If I want to add my example word in my language table what should I do? I am asking this because of this example:

$username => Mustafa,

$friendname => ahmet

English: <?php echo $username;?> went to the cinema with his friend <?php echo $friendname;?> today.

Turkish: <?php echo $username;?> bugün arkadaşı <?php echo $friendname;?> ile birlikte sinemaya gitti.

As you can see, the special names are in different places. How can I show the usernames from the table without echoing it?

Upvotes: 2

Views: 78

Answers (1)

Eddie
Eddie

Reputation: 26854

Why dont use placeholder on defined sentences.

$sentences = array();
$sentences[ "en" ] = "{{USERNAME}} went to the cinema with his friend {{FRIENDNAME}} today.";
$sentences[ "tu" ] = "{{USERNAME}} bugün arkadaşı {{FRIENDNAME}} ile birlikte sinemaya gitti.";

Put the name and placeholders on array

$placeHolder    = [ "{{USERNAME}}", "{{FRIENDNAME}}" ];
$name           = [ "Mustafa", "ahmet" ];

If you choose to print EN

echo str_replace($placeHolder, $name, $sentences[ "en" ]);

Will result to: Mustafa went to the cinema with his friend ahmet today.

If you choose to print TU

echo str_replace($placeHolder, $name, $sentences[ "tu" ]);

Will result to: Mustafa bugün arkadaşı ahmet ile birlikte sinemaya gitti.

Upvotes: 5

Related Questions