forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended translations system for arrays & extension
Extended the base Laravel translation system to allow a locale to be based upon another. Also adds functionality to take base & fallback locales into account when fetching an array of translations. Related to work done in BookStackApp#1159
- Loading branch information
1 parent
0e3d507
commit 323bff7
Showing
16 changed files
with
185 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php namespace BookStack\Providers; | ||
|
||
|
||
use BookStack\Translation\Translator; | ||
|
||
class TranslationServiceProvider extends \Illuminate\Translation\TranslationServiceProvider | ||
{ | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->registerLoader(); | ||
|
||
$this->app->singleton('translator', function ($app) { | ||
$loader = $app['translation.loader']; | ||
|
||
// When registering the translator component, we'll need to set the default | ||
// locale as well as the fallback locale. So, we'll grab the application | ||
// configuration so we can easily get both of these values from there. | ||
$locale = $app['config']['app.locale']; | ||
|
||
$trans = new Translator($loader, $locale); | ||
|
||
$trans->setFallback($app['config']['app.fallback_locale']); | ||
|
||
return $trans; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php namespace BookStack\Translation; | ||
|
||
|
||
class Translator extends \Illuminate\Translation\Translator | ||
{ | ||
|
||
/** | ||
* Mapping of locales to their base locales | ||
* @var array | ||
*/ | ||
protected $baseLocaleMap = [ | ||
'de_informal' => 'de', | ||
]; | ||
|
||
/** | ||
* Get the translation for a given key. | ||
* | ||
* @param string $key | ||
* @param array $replace | ||
* @param string $locale | ||
* @return string|array|null | ||
*/ | ||
public function trans($key, array $replace = [], $locale = null) | ||
{ | ||
$translation = $this->get($key, $replace, $locale); | ||
|
||
if (is_array($translation)) { | ||
$translation = $this->mergeBackupTranslations($translation, $key, $locale); | ||
} | ||
|
||
return $translation; | ||
} | ||
|
||
/** | ||
* Merge the fallback translations, and base translations if existing, | ||
* into the provided core key => value array of translations content. | ||
* @param array $translationArray | ||
* @param string $key | ||
* @param null $locale | ||
* @return array | ||
*/ | ||
protected function mergeBackupTranslations(array $translationArray, string $key, $locale = null) | ||
{ | ||
$fallback = $this->get($key, [], $this->fallback); | ||
$baseLocale = $this->getBaseLocale($locale ?? $this->locale); | ||
$baseTranslations = $baseLocale ? $this->get($key, [], $baseLocale) : []; | ||
|
||
return array_replace_recursive($fallback, $baseTranslations, $translationArray); | ||
} | ||
|
||
/** | ||
* Get the array of locales to be checked. | ||
* | ||
* @param string|null $locale | ||
* @return array | ||
*/ | ||
protected function localeArray($locale) | ||
{ | ||
$primaryLocale = $locale ?: $this->locale; | ||
return array_filter([$primaryLocale, $this->getBaseLocale($primaryLocale), $this->fallback]); | ||
} | ||
|
||
/** | ||
* Get the locale to extend for the given locale. | ||
* | ||
* @param string $locale | ||
* @return string|null | ||
*/ | ||
protected function getBaseLocale($locale) | ||
{ | ||
return $this->baseLocaleMap[$locale] ?? null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<?php | ||
$de_formal = (include resource_path() . '/lang/de/' . basename(__FILE__)); | ||
|
||
$de_informal = [ | ||
|
||
]; | ||
|
||
return array_replace($de_formal, $de_informal); | ||
// Extends 'de' | ||
return [ | ||
// | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
<?php | ||
$de_formal = (include resource_path() . '/lang/de/' . basename(__FILE__)); | ||
|
||
$de_informal = [ | ||
// Extends 'de' | ||
return [ | ||
/** | ||
* Email Content | ||
*/ | ||
'email_action_help' => 'Sollte es beim Anklicken der Schaltfläche ":action_text" Probleme geben, öffne die folgende URL in Deinem Browser:', | ||
]; | ||
|
||
return array_replace($de_formal, $de_informal); | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
<?php | ||
$de_formal = (include resource_path() . '/lang/de/' . basename(__FILE__)); | ||
|
||
$de_informal = [ | ||
// Extends 'de' | ||
return [ | ||
/** | ||
* Image Manager | ||
*/ | ||
'image_delete_confirm' => 'Bitte klicke erneut auf löschen, wenn Du dieses Bild wirklich entfernen möchtest.', | ||
'image_dropzone' => 'Ziehe Bilder hierher oder klicke hier, um ein Bild auszuwählen', | ||
]; | ||
|
||
return array_replace($de_formal, $de_informal); | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
<?php | ||
$de_formal = (include resource_path() . '/lang/de/' . basename(__FILE__)); | ||
|
||
$de_informal = [ | ||
// Extends 'de' | ||
return [ | ||
// Pages | ||
'permission' => 'Du hast keine Berechtigung, auf diese Seite zuzugreifen.', | ||
'permissionJson' => 'Du hast keine Berechtigung, die angeforderte Aktion auszuführen.', | ||
|
||
// Auth | ||
'email_already_confirmed' => 'Die E-Mail-Adresse ist bereits bestätigt. Bitte melde dich an.', | ||
'email_confirmation_invalid' => 'Der Bestätigungslink ist nicht gültig oder wurde bereits verwendet. Bitte registriere dich erneut.', | ||
'social_account_in_use' => 'Dieses :socialAccount-Konto wird bereits verwendet. Bitte melde dich mit dem :socialAccount-Konto an.', | ||
'social_account_email_in_use' => 'Die E-Mail-Adresse ":email" ist bereits registriert. Wenn Du bereits registriert bist, kannst Du Dein :socialAccount-Konto in Deinen Profil-Einstellungen verknüpfen.', | ||
'social_account_not_used' => 'Dieses :socialAccount-Konto ist bisher keinem Benutzer zugeordnet. Du kannst das in Deinen Profil-Einstellungen tun.', | ||
'social_account_register_instructions' => 'Wenn Du bisher kein Social-Media Konto besitzt, kannst Du ein solches Konto mit der :socialAccount Option anlegen.', | ||
|
||
// System | ||
'path_not_writable' => 'Die Datei kann nicht in den angegebenen Pfad :filePath hochgeladen werden. Stelle sicher, dass dieser Ordner auf dem Server beschreibbar ist.', | ||
'cannot_create_thumbs' => 'Der Server kann keine Vorschau-Bilder erzeugen. Bitte prüfe, ob die GD PHP-Erweiterung installiert ist.', | ||
'server_upload_limit' => 'Der Server verbietet das Hochladen von Dateien mit dieser Dateigröße. Bitte versuche es mit einer kleineren Datei.', | ||
|
||
// Pages | ||
'page_draft_autosave_fail' => 'Fehler beim Speichern des Entwurfs. Stelle sicher, dass Du mit dem Internet verbunden bist, bevor Du den Entwurf dieser Seite speicherst.', | ||
'page_custom_home_deletion' => 'Eine als Startseite gesetzte Seite kann nicht gelöscht werden.', | ||
|
||
// Users | ||
'users_cannot_delete_only_admin' => 'Du kannst den einzigen Administrator nicht löschen.', | ||
'users_cannot_delete_guest' => 'Du kannst den Gast-Benutzer nicht löschen', | ||
|
||
// Error pages | ||
'sorry_page_not_found' => 'Entschuldigung. Die Seite, die Du angefordert hast, wurde nicht gefunden.', | ||
]; | ||
|
||
return array_replace($de_formal, $de_informal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<?php | ||
$de_formal = (include resource_path() . '/lang/de/' . basename(__FILE__)); | ||
|
||
$de_informal = [ | ||
|
||
]; | ||
|
||
return array_replace($de_formal, $de_informal); | ||
// Extends 'de' | ||
return [ | ||
// | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<?php | ||
$de_formal = (include resource_path() . '/lang/de/' . basename(__FILE__)); | ||
|
||
$de_informal = [ | ||
|
||
]; | ||
|
||
return array_replace($de_formal, $de_informal); | ||
// Extends 'de' | ||
return [ | ||
// | ||
]; |
Oops, something went wrong.