Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #14976, #14975, #14973 - Translation strings aren't always working #14981

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/Services/SnipeTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use App\Helpers\Helper;
use Illuminate\Translation\Translator;

/***************************************************************
Expand All @@ -17,6 +18,10 @@
***************************************************************/
class SnipeTranslator extends Translator {

static $legacy_translation_namespaces = [
"backup::" //Spatie backup uses 'legacy' locale names
];

//This is copied-and-pasted (almost) verbatim from Illuminate\Translation\Translator
public function choice($key, $number, array $replace = [], $locale = null)
{
Expand All @@ -39,4 +44,28 @@ public function choice($key, $number, array $replace = [], $locale = null)
);
}

public function get($key, array $replace = [], $locale = null, $fallback = true)
{
$modified_locale = $locale;
$changed_fallback = false;
$previous_fallback = $this->fallback;
// 'legacy' translation directories tend to be two-char ('en'), not 5-char ('en-US').
// Here we try our best to handle that.
foreach (self::$legacy_translation_namespaces as $namespace) {
if (preg_match("/^$namespace/", $key)) {
$modified_locale = Helper::mapBackToLegacyLocale($locale);
$changed_fallback = true;
$this->fallback = 'en'; //TODO - should this be 'env-able'? Or do we just put our foot down and say 'en'?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we maybe want to use the normal Laravel FALLBACK_APP_LOCALE env variable here? It seems like a potential footgun to invent our own override here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think so. The FALLBACK_APP_LOCALE is the fallback locale for Snipe-IT - and so it's going to be something like en-US or pt-PT. Whereas, in this code, we are dropping back to 'legacy' locales - en or pt or whatever. I could maybe see adding yet another (UGH) .env var to specify what 'legacy locale' you want to fall-back to when your main language isn't there - and since Snipe-IT is translated into many more languages than Spatie backup is, that's going to happen for sure.

I feel like 'hiding' the legacy locale situation from the users seems best, and picking US English as a fallback, when your language isn't there, is a pretty reasonable choice.

Some very weird situations could be created if we allow users to change this - Having a 'locale' of en-US and a 'legacy fallback locale' of pt for example would be extremely weird. Or if a user naively picked for their "legacy fallback locale" af - for Afrikaans - and it doesn't exist at all (in Spatie) - then they'd just get raw translation strings back. So I'd rather avoid those kinds of scenarios and stick to something that makes the most sense, and we know is going to work. It's also possible that Spatie could change their mind and start using the ISO-compliant localization names too in the future - and now we'd have this silly exposed environment variable that doesn't make sense anymore.

All that being said, I am not 100% sure of that decision, hence the TODO - I can be convinced.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this as-is I think. We can always look into it again if it becomes an issue.

break;
}
}

$result = parent::get($key, $replace, $modified_locale, $fallback);
if ($changed_fallback) {
$this->fallback = $previous_fallback;
}
return $result;
}


}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
|
*/

'fallback_locale' => env('FALLBACK_APP_LOCALE', 'en'),
'fallback_locale' => env('FALLBACK_APP_LOCALE', 'en-US'),

/*
|--------------------------------------------------------------------------
Expand Down
45 changes: 0 additions & 45 deletions resources/lang/vendor/backup/ar/notifications.php

This file was deleted.

45 changes: 0 additions & 45 deletions resources/lang/vendor/backup/bg/notifications.php

This file was deleted.

45 changes: 0 additions & 45 deletions resources/lang/vendor/backup/bn/notifications.php

This file was deleted.

45 changes: 0 additions & 45 deletions resources/lang/vendor/backup/cs/notifications.php

This file was deleted.

45 changes: 0 additions & 45 deletions resources/lang/vendor/backup/da/notifications.php

This file was deleted.

Loading
Loading