Skip to content

Commit

Permalink
develop/v2.2.0: Updated the readme.md again to mention view caching
Browse files Browse the repository at this point in the history
  • Loading branch information
wazzac committed Jul 15, 2024
1 parent 0f46761 commit 7348efa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<a href="https://github.com/wazzac/domTranslate/issues"><img alt="GitHub issues" src="https://img.shields.io/github/issues/wazzac/domTranslate"></a>
<a href="https://github.com/WarrenGIT/domTranslate/stargazers"><img alt="GitHub stars" src="https://img.shields.io/github/stars/wazzac/domTranslate"></a>
<a href="https://github.com/WarrenGIT/domTranslate/blob/main/LICENSE"><img alt="GitHub license" src="https://img.shields.io/github/license/WarrenGIT/domTranslate"></a>
<a href="https://github.com/WarrenGIT/domTranslate"><img alt="GitHub version" src="https://img.shields.io/github/v/tag/WarrenGIT/domTranslate?label=version&sort=semver"></a>
</p>


# Laravel Translate Package

A library that leverages Laravel Directives to provide automated translations for all your Blade phrases or words.
Expand All @@ -15,6 +17,7 @@ _Example: Write HTML static data in English and display it in a different langua
The library uses three database tables (_domt_phrases_, _domt_translations_, and _domt_languages_) to manage translations efficiently.

1. On page load, the system searches for a specific translation using the provided phrase in the `@transl8()` directive from the _domt_translations_ table.
> Laravel generally cache views, so if the content of the entire page didn't change, steps 1 - 4 will not fire as the cached view will simply load.
2. If the translation is found, it is returned and displayed on the page without making an API call.
3. If the translation is not found _(not translated yet)_, the Google Translate API (or another defined provider) is called to retrieve the new translation.
4. The newly translated text is then inserted into the database to avoid future API calls for the same phrase.
Expand Down Expand Up @@ -60,7 +63,7 @@ DOM_TRANSLATE_LANG_DEST=af
- If no translations are found in the session, or if `DOM_TRANSLATE_USE_SESSION` is `false`, translations will be retrieved from the database, provided they have been previously stored there.
- If translations are still not found, or if both `DOM_TRANSLATE_USE_SESSION` and `DOM_TRANSLATE_USE_DATABASE` are `false`, translations will be sourced from a third-party translation service (e.g., Google Translate).
- Depending on whether `DOM_TRANSLATE_USE_SESSION` and `DOM_TRANSLATE_USE_DATABASE` are `true`, the retrieved translation will be saved to either the session or the database.
- We strongly recommend setting `DOM_TRANSLATE_USE_DATABASE` to `true` _(default is `true` if not specified in your .env)_ to ensure we don't make repeated API calls _(also it's slower calling the API verses db/session lookup)_.
- We strongly recommend setting `DOM_TRANSLATE_USE_DATABASE` to `true` _(default is `true` if not specified in your .env)_ to ensure we don't make repeated API calls _(also it's slower calling the API verses db/session lookup)_.

> **Note:** If you don't have a [Google Cloud Platform](https://cloud.google.com/gcp) account, sign up and create a new project. Add the _Cloud Translation API_ to it. You can use [Insomnia](https://insomnia.rest/download) to test your API key.
Expand Down
14 changes: 8 additions & 6 deletions src/Controllers/TranslateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,24 @@ public static function phrase(?string $string = null)
*/
public static function translate(?string $srcPhrase = null, ?string $destCode = null, ?string $srcCode = null)
{
LogController::log('notice', 1, '----- Translation request start -----');
LogController::log('notice', 1, 'New phrase to translate.'); // high-level = 1

// sanitise the phrase
$srcPhrase = phraseHelper::sanitise($srcPhrase);
LogController::log('notice', 3, 'Phrase Sanitised: ' . $srcPhrase); // low-level = 3
LogController::log('notice', 3, 'Phrase Sanitised ............... : ' . $srcPhrase); // low-level = 3

// hash the phrase
$srcHash = phraseHelper::hash($srcPhrase);
LogController::log('notice', 3, 'Phrase Hashed: ' . $srcHash);
LogController::log('notice', 3, 'Phrase Hashed .................. : ' . $srcHash);

// do we have a destination language defined
$destCode = phraseHelper::prepDestLanguage($destCode);
LogController::log('notice', 2, 'Destination language code set as: ' . $destCode);
LogController::log('notice', 2, 'Destination language code set as : ' . $destCode);

// do we have a source language defined
$srcCode = phraseHelper::prepSrcLanguage($srcCode);
LogController::log('notice', 2, 'Source language code set as: ' . $srcCode);
LogController::log('notice', 2, 'Source language code set as .... : ' . $srcCode);

// ok, ready to rock-and-roll...
try {
Expand All @@ -75,7 +76,7 @@ public static function translate(?string $srcPhrase = null, ?string $destCode =

if (session()->has($srcHash . $srcCode . $destCode)) {
// session translation located
LogController::log('notice', 1, 'Translation located in Session. Return translation...');
LogController::log('notice', 1, 'Translation located in Session. Returning translation...');
return session()->get($srcHash . $srcCode . $destCode);
}
} else {
Expand Down Expand Up @@ -104,7 +105,8 @@ public static function translate(?string $srcPhrase = null, ?string $destCode =
LogController::log('notice', 1, 'Translation located in DB. Return translation...');

// save to session so that future request (for this session) can be returned at step 1 above
if (config('dom_translate.use_session', false) === true) {
if (config('dom_translate.use_session', false) === true && !session()->has($srcHash . $srcCode . $destCode)) {
LogController::log('notice', 2, 'DB Located translation saved to Session.');
session()->put($srcHash . $srcCode . $destCode, $translation->value);
}

Expand Down

0 comments on commit 7348efa

Please sign in to comment.