Git Product home page Git Product logo

Comments (16)

Wruczek avatar Wruczek commented on June 20, 2024

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.

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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?

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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);

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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.

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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.

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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.

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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.

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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.

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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.

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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 !!

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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 :)

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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}

from ts-website.

Wruczek avatar Wruczek commented on June 20, 2024

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.

from ts-website.

cataalin avatar cataalin commented on June 20, 2024

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

from ts-website.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.