Wruczek / ts-website

A website for your TeamSpeak 3 server
https://ts.wruczek.tech
GNU General Public License v3.0
333 stars 80 forks source link

how can i translate this date? #219

Closed cataalin closed 1 year ago

cataalin commented 1 year ago

How can I make it detect the user's language and display the row with its respective language id? For the moment I managed to display the row by selecting its id, but I would like it to display the row according to the defined language id.

<?php

use Wruczek\TSWebsite\Utils\DatabaseUtils;
use Wruczek\TSWebsite\Utils\TemplateUtils;

require_once __DIR__ . "/private/php/load.php";

$db = DatabaseUtils::i()->getDb();
$qa = $db->select("translations", "*", ["id" => 16086]);

$data = [
    "pagetitle" => __get("RULES_TITLE"),
    "paneltitle" => '<i class="fas fa-book"></i>' . __get("RULES_PANEL_TITLE"),
    "qa" => $qa
];

TemplateUtils::i()->renderTemplate("rules", $data);
{extends "body.latte"}
{var title = $pagetitle}
{var $metaDescription = "Rules."}

{block content}
<div class="card card-accent">
    <div class="card-header bigger-title">
        {$paneltitle|noescape}
    </div>
    <div class="card-body">
    {_"REGLAS"}
    {foreach $qa as $id => $value}
    <p class="card-fecha-editada">{_"NEW_EDITED"} {strtotime($value["edited"])|fuzzyDateAbbr}</p>
    {/foreach}
    </div>
</div>
{/block}

dsada

Wruczek commented 1 year ago

You could define separate rules using identifiers ending with language code, for example: rules_en, rules_pl, rules_de etc., and then fetch it like this:

$userLang = LanguageUtils::i()->getLanguageById(USER_LANGUAGE_ID);
$rulesText = Config::get("rules_" . $userLang->getLanguageCode());

Remember to import LanguageUtils and Config.

cataalin commented 1 year ago

You could define separate rules using identifiers ending with language code, for example: rules_en, rules_pl, rules_de etc., and then fetch it like this:

$userLang = LanguageUtils::i()->getLanguageById(USER_LANGUAGE_ID);
$rulesText = Config::get("rules_" . $userLang->getLanguageCode());

Remember to import LanguageUtils and Config.

Any example of how it would be?, I have tried and the page gives me an error 500, unless I put it like this according to the languageutils:

$userLang = LanguageUtils::i()->getLanguageById(USER_LANGUAGE_ID);

to

$userLang = LanguageUtils::i()->getLanguageById(@$_SESSION["userlanguageid"]);

anyway I can't get the desired result, what would the latte file look like?

Wruczek commented 1 year ago

My bad - USER_LANGUAGE_ID is only defined on the latest TS-website version (commit 83d7d0b) that I have just released. Your version with the session variable is correct.

With that, you get a $rulesText variable that you can pass to the template (in this case, we can simply replace the panelcontent with this variable).

Here's an exmaple of rules.php file for TS-website versions before dev-2.1.0. Note: I have renamed $rulesText to $rulesHtml, as it's a better name for this variable.

<?php

use Wruczek\TSWebsite\Utils\TemplateUtils;

require_once __DIR__ . "/private/php/load.php";

$userLang = LanguageUtils::i()->getLanguageById(@$_SESSION["userlanguageid"]);
$rulesHtml = Config::get("rules_" . $userLang->getLanguageCode());

$data = [
    "pagetitle" => __get("RULES_TITLE"),
    "navActiveIndex" => 4,
    "paneltitle" => '<i class="fas fa-book"></i>' . __get("RULES_PANEL_TITLE"),
    "panelcontent" => $rulesHtml
];

TemplateUtils::i()->renderTemplate("simple-page", $data);
cataalin commented 1 year ago

My bad - USER_LANGUAGE_ID is only defined on the latest TS-website version (commit 83d7d0b) that I have just released. Your version with the session variable is correct.

With that, you get a $rulesText variable that you can pass to the template (in this case, we can simply replace the panelcontent with this variable).

Here's an exmaple of rules.php file for TS-website versions before dev-2.1.0. Note: I have renamed $rulesText to $rulesHtml, as it's a better name for this variable.

<?php

use Wruczek\TSWebsite\Utils\TemplateUtils;

require_once __DIR__ . "/private/php/load.php";

$userLang = LanguageUtils::i()->getLanguageById(@$_SESSION["userlanguageid"]);
$rulesHtml = Config::get("rules_" . $userLang->getLanguageCode());

$data = [
    "pagetitle" => __get("RULES_TITLE"),
    "navActiveIndex" => 4,
    "paneltitle" => '<i class="fas fa-book"></i>' . __get("RULES_PANEL_TITLE"),
    "panelcontent" => $rulesHtml
];

TemplateUtils::i()->renderTemplate("simple-page", $data);

I understood the php part, but I don't understand how the latte would show the language of that column.

Wruczek commented 1 year ago

Template simple-page.latte is a very simple page that displays the title, paneltitle and panelcontent that is passed to it (via $data). https://github.com/Wruczek/ts-website/blob/2.0/src/private/templates/simple-page.latte

cataalin commented 1 year ago

La plantilla simple-page.lattees una página muy simple que muestra el title, paneltitley panelcontentse le pasa (a través de $data). https://github.com/Wruczek/ts-website/blob/2.0/src/private/templates/simple-page.latte

Yes, I don't think I explained myself well, everything works fine for me and it shows what column I want because I told it in the php through the id, but I wanted it to show the column based on the user's language, that is, if you have 3 or 4 columns that you want to show there depending on the language of the user, Imagine, I have 3 rows in the translations db, so instead of doing:

$db = DatabaseUtils::i()->getDb();
$qa = $db->select("translations", "*", ["id" => 16086]);

that shows only the row with that id, then make it show one of the 3 or 4 with the language according to the user, I do not know if I explained well, It will remain something like in your first example, something like this:

$db = DatabaseUtils::i()->getDb();
$qa = $db->select("translations", "*", ["id" => 16086], ["langid", $userLang]);

$userLang = LanguageUtils::i()->getLanguageById(@$_SESSION["userlanguageid"]);

With this I want to get it to show that specific row but only show the user's language, but since they are different ids I don't know how to reframe it to have this result.

Wruczek commented 1 year ago

So you simply want to get a translated phrase? You can see that the rules.php file already does that with: __get("RULES_TITLE").

To get a translated phrase in PHP, use __get(phrase) or __get(phrase, ['array', 'with', 'arguments']) (definition). It automatically returns given phrase in user's language, or error/null if it does not exist.

cataalin commented 1 year ago

So you simply want to get a translated phrase? You can see that the rules.php file already does that with: __get("RULES_TITLE").

To get a translated phrase in PHP, use __get(phrase) or __get(phrase, ['array', 'with', 'arguments']) (definition). It automatically returns given phrase in user's language, or error/null if it does not exist.

I have added a new column that has a timestamp to the translations db, basically and more directly, what I am looking for is to select two or 3 specific rows from the translations table for example langid 1, 2, 6 and show it according to the language that the user, if the user has language with id 2, show the timestam column of the langid 2 but only the timestam column not the whole row.

Wruczek commented 1 year ago

I have no idea what you are trying to achieve, and there's probably a better way to do it. But to get some data from translations table by user's language:

$db = DatabaseUtils::i()->getDb();
$translations = $db->select("translations", "*", [
  "id" => [1, 2, 3], // get rows with IDs: 1, 2 ,3
  "langid" => @$_SESSION["userlanguageid"],
]);
var_dump($translations);

Object returned by getDb() is simply a Medoo instance. See: https://medoo.in/api/select and https://medoo.in/api/where

cataalin commented 1 year ago

I have no idea what you are trying to achieve, and there's probably a better way to do it. But to get some data from translations table by user's language:

$db = DatabaseUtils::i()->getDb();
$translations = $db->select("translations", "*", [
  "id" => [1, 2, 3], // get rows with IDs: 1, 2 ,3
  "langid" => @$_SESSION["userlanguageid"],
]);
var_dump($translations);

Object returned by getDb() is simply a Medoo instance. See: https://medoo.in/api/select and https://medoo.in/api/where

I'm not explaining myself well, my apologies, what I want to achieve is to put in the rules latte the date of the last edition of the row, nothing more, but for that you have to select only the date column and the langid, and then display it according to the language of the user.

Wruczek commented 1 year ago

To get a specific column only, you can replace "*" with the column name: "timestamp". Please take a look at the linked documentation above. Object returned by getDb() is simply a Medoo instance. It still feels like you should not use the ID. If you want to get the date of a specific row, you should use it's identifier. To get a single result, you can replace select with get.

$lastEdited = $db->get("translations", "timestamp", [
  "identifier" => "RULES_TITLE",
  "langid" => @$_SESSION["userlanguageid"],
]);

$lastEdited will be a string containing the timestamp column of row, where identifier = RULES_TITLE, and langid = user's language.

cataalin commented 1 year ago

Thank you very much, that's what I was looking for, that the edited date appears with in the news, in my latte it was like this to show the date:

PHP

<?php

use Wruczek\TSWebsite\Utils\DatabaseUtils;
use Wruczek\TSWebsite\Utils\TemplateUtils;

require_once __DIR__ . "/private/php/load.php";

$db = DatabaseUtils::i()->getDb();
$edited = $db->get("translations", "edited", [
  "identifier" => "REGLAS",
  "langid" => @$_SESSION["userlanguageid"],
]);

$data = [
    "pagetitle" => __get("RULES_TITLE"),
    "paneltitle" => '<i class="fas fa-book"></i>' . __get("RULES_PANEL_TITLE"),
    "edited" => $edited
];

TemplateUtils::i()->renderTemplate("reglas", $data);

LATTE

{strtotime($edited)|fuzzyDateAbbr}

I think everything is fine, correct me if I'm wrong, anyway thank you very much !!

Wruczek commented 1 year ago

Looks good, but I would first check in latte if the value is not null (it will be null if it was not found in DB).

{if $edited !== null}
    {strtotime($edited)|fuzzyDateAbbr}
{/if}

I will close the issue :)

cataalin commented 1 year ago

Looks good, but I would first check in latte if the value is not null (it will be null if it was not found in DB).

{if $edited !== null}
    {strtotime($edited)|fuzzyDateAbbr}
{/if}

I will close the issue :)

I verified it this way, what is the difference between one and the other?

{if empty(!$edited)}
       {strtotime($edited)|fuzzyDateAbbr}
{/if}
Wruczek commented 1 year ago

Your if statement is not valid, you first convert $edited into a boolean and invert it, then check if that is empty. It might technically work, but is not good. empty also treats some strings as empty ("0" would be empty). My approach only checks if the variable is NULL.

cataalin commented 1 year ago

Ah okay, perfect, thanks again, everything is solved!