From 3244d7b0641375f7b0b001c13994e0fa164b48dd Mon Sep 17 00:00:00 2001 From: "Morten D. Hansen" Date: Wed, 9 Mar 2022 13:38:06 +0100 Subject: [PATCH] Do not create groups from paragraphs --- src/DatabaseTranslationsTranslator.php | 6 ++++++ tests/Feature/DatabaseTranslationsTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/DatabaseTranslationsTranslator.php b/src/DatabaseTranslationsTranslator.php index 9a6942b..452ac77 100644 --- a/src/DatabaseTranslationsTranslator.php +++ b/src/DatabaseTranslationsTranslator.php @@ -63,6 +63,12 @@ public function get($key, array $replace = [], $locale = null, $fallback = true) $group = '*'; } + // The group should be just a word. Sometimes translation key is an entire paragraph. We put that into ungrouped + if (!(preg_match('/^[\pL\pM\pN_-]+$/u', $group) > 0)) { + $group = '*'; + $item = $key; + } + if (!array_key_exists($item, $this->loadedFromDb['*'][$group][$passedLocale])) { $this->createMissingKey($group, $item, $passedLocale); } diff --git a/tests/Feature/DatabaseTranslationsTest.php b/tests/Feature/DatabaseTranslationsTest.php index f58a61f..3f9858e 100644 --- a/tests/Feature/DatabaseTranslationsTest.php +++ b/tests/Feature/DatabaseTranslationsTest.php @@ -354,5 +354,15 @@ public function itCreatesMissingKeysInDifferentLocales() $this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'color', 'locale' => 'de']); } + /** + * @test + * @return void + */ + public function itCanHandleChunksOfText() + { + __("Sometimes, translations are in fact a chunk of text. It can be very long and it makes no sense to treat it as a grouped key."); + $this->assertEquals('*', DatabaseLangItem::all()->first()->group); + } + } \ No newline at end of file