Skip to content

Commit 0efd516

Browse files
author
Arif Hoque
committed
v2.0.5: translation issue fix for package development
1 parent 99889e9 commit 0efd516

File tree

6 files changed

+82
-24
lines changed

6 files changed

+82
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Doppar is a PHP framework built to revolutionize the way developers create robus
1919

2020
## Contributing
2121

22-
Thank you for considering contributing to the Doppar framework! The contribution guide can be found in the [Doppar documentation](https://doppar.com/docs/contributions).
22+
Thank you for considering contributing to the Doppar framework! The contribution guide can be found in the [Doppar documentation](https://doppar.com/docs/3.x/contributions.html).
2323

2424
## Code of Conduct
2525

26-
In order to ensure that the Doppar community is welcoming to all, please review and abide by the [Code of Conduct](https://doppar.com/docs/contributions#code-of-conduct).
26+
In order to ensure that the Doppar community is welcoming to all, please review and abide by the [Code of Conduct](https://doppar.com/versions/3.x/contributions.html#code-of-conduct).
2727

2828
## Security Vulnerabilities
2929

src/Phaseolies/Console/Commands/VendorPublishCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ protected function publishPaths(array $paths, OutputInterface $output, bool $for
122122

123123
protected function publishDirectory(string $from, string $to, OutputInterface $output, bool $force = false)
124124
{
125-
$output->writeln("<comment>Copying directory:</comment> {$from} <comment>to</comment> {$to}");
126-
127125
if (!is_dir($to)) {
128126
mkdir($to, 0755, true);
129127
}
@@ -149,7 +147,7 @@ protected function publishDirectory(string $from, string $to, OutputInterface $o
149147
protected function publishFile(string $from, string $to, OutputInterface $output, bool $force = false)
150148
{
151149
if (file_exists($to) && !$force) {
152-
$output->writeln("<fg=yellow>Skipping:</> File already exists");
150+
$output->writeln("<error>Skipping: File already exists </error>");
153151
return;
154152
}
155153

src/Phaseolies/Http/Controllers/Controller.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ protected function findView(string $view): string
103103

104104
$viewPath = str_replace('.', DIRECTORY_SEPARATOR, $viewName);
105105

106+
// First check published views in resources/views/vendor/{namespace}
107+
$publishedPath = base_path('resources/views/vendor/' . $namespace);
108+
if (is_dir($publishedPath)) {
109+
$possiblePaths = [
110+
$publishedPath . DIRECTORY_SEPARATOR . $viewPath . $this->fileExtension,
111+
$publishedPath . DIRECTORY_SEPARATOR . $viewPath . '.blade.php',
112+
$publishedPath . DIRECTORY_SEPARATOR . $viewPath . '.php',
113+
];
114+
115+
foreach ($possiblePaths as $fullPath) {
116+
if (file_exists($fullPath)) {
117+
return $fullPath;
118+
}
119+
}
120+
}
121+
122+
// Then check package views
106123
foreach ($this->factory->namespaces[$namespace] as $basePath) {
107124
$possiblePaths = [
108125
$basePath . DIRECTORY_SEPARATOR . $viewPath . $this->fileExtension,
@@ -113,15 +130,14 @@ protected function findView(string $view): string
113130
foreach ($possiblePaths as $fullPath) {
114131
if (file_exists($fullPath)) {
115132
return $fullPath;
116-
}else{
117-
throw new \RuntimeException("View [{$fullPath}] not found in namespace [{$namespace}]");
118133
}
119134
}
120135
}
121136

122137
throw new \RuntimeException("View [{$view}] not found in namespace [{$namespace}]");
123138
}
124139

140+
// Handle non-namespaced views
125141
$viewPath = str_replace('.', DIRECTORY_SEPARATOR, $view);
126142
$fullPath = base_path($this->viewFolder) . DIRECTORY_SEPARATOR .
127143
$viewPath . $this->fileExtension;

src/Phaseolies/Providers/ServiceProvider.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace Phaseolies\Providers;
44

5-
use Phaseolies\Database\Migration\Migrator;
6-
use Phaseolies\Database\Migration\MigrationRepository;
75
use Phaseolies\Database\Migration\Migration;
86
use Phaseolies\Application;
9-
use Phaseolies\Database\Database;
107

118
abstract class ServiceProvider
129
{
@@ -115,6 +112,20 @@ public function loadViews(string $path, string $namespace): void
115112
}
116113
}
117114

115+
/**
116+
* Register translation files from the given path.
117+
*
118+
* @param string $path
119+
* @param string $namespace
120+
* @return void
121+
*/
122+
public function loadTranslations(string $path, string $namespace): void
123+
{
124+
if ($this->app->has('translator')) {
125+
$this->app['translator']->addNamespace($namespace, $path);
126+
}
127+
}
128+
118129
/**
119130
* Register paths to be published by the publish command.
120131
*/

src/Phaseolies/Translation/FileLoader.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,35 @@ public function __construct($path)
4545
*/
4646
public function load($locale, $group, $namespace = null)
4747
{
48-
// Handle namespaced translations (package translations)
49-
if ($namespace && isset($this->hints[$namespace])) {
50-
$lines = $this->loadPath($this->hints[$namespace], $locale, $group);
51-
52-
// Check for and apply any namespace overrides
53-
return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace);
48+
try {
49+
$lines = $this->loadPath($this->path, $locale, $group);
50+
return $lines;
51+
} catch (\RuntimeException $e) {
52+
// If not found in main app, try package locations
53+
if ($namespace) {
54+
// Try registered package path first
55+
if (isset($this->hints[$namespace])) {
56+
try {
57+
return $this->loadPath($this->hints[$namespace], $locale, $group);
58+
} catch (\RuntimeException $e) {
59+
// Continue to try vendor path
60+
}
61+
}
62+
63+
// Try published vendor path
64+
$vendorPath = $this->path . '/vendor/' . $namespace;
65+
if (file_exists($vendorPath)) {
66+
try {
67+
return $this->loadPath($vendorPath, $locale, $group);
68+
} catch (\RuntimeException $e) {
69+
// Continue to throw original exception
70+
}
71+
}
72+
}
73+
74+
// If all fails, rethrow the original exception
75+
throw $e;
5476
}
55-
56-
// Load regular application translations
57-
return $this->loadPath($this->path, $locale, $group);
5877
}
5978

6079
/**

src/Phaseolies/Translation/Translator.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Phaseolies\Translation;
44

5-
class Translator
5+
use Phaseolies\Translation\FileLoader;
6+
7+
class Translator extends FileLoader
68
{
79
/**
810
* The file loader instance responsible for loading translation files.
@@ -73,7 +75,7 @@ public function get($key, array $replace = [], $locale = null)
7375
return $key;
7476
}
7577

76-
$this->load($namespace, $group, $locale);
78+
$this->loadTranslations($namespace, $group, $locale);
7779

7880
$line = $this->getLine(
7981
$namespace,
@@ -102,15 +104,27 @@ public function get($key, array $replace = [], $locale = null)
102104
*/
103105
protected function parseKey($key)
104106
{
107+
// Handle package translations (namespace::group.item)
105108
if (strpos($key, '::') !== false) {
106-
return $this->parseNamespacedKey($key);
109+
$segments = explode('::', $key, 2);
110+
$namespace = $segments[0];
111+
$rest = $segments[1];
112+
113+
if (strpos($rest, '.') !== false) {
114+
list($group, $item) = explode('.', $rest, 2);
115+
return [$namespace, $group, $item];
116+
}
117+
118+
return [$namespace, '*', $rest];
107119
}
108120

121+
// Handle regular translations (group.item)
109122
if (strpos($key, '.') !== false) {
110123
list($group, $item) = explode('.', $key, 2);
111124
return [null, $group, $item];
112125
}
113126

127+
// Fallback for simple keys
114128
return [null, '*', $key];
115129
}
116130

@@ -150,7 +164,7 @@ protected function parseNamespacedKey($key)
150164
*/
151165
protected function getLine($namespace, $group, $locale, $item, array $replace)
152166
{
153-
$this->load($namespace, $group, $locale);
167+
$this->loadTranslations($namespace, $group, $locale);
154168

155169
$keys = explode('.', $item);
156170
$line = $this->loaded[$namespace][$group][$locale] ?? null;
@@ -199,7 +213,7 @@ public function makeReplacements($line, array $replace)
199213
* @param string $locale The locale
200214
* @return void
201215
*/
202-
public function load($namespace, $group, $locale)
216+
public function loadTranslations($namespace, $group, $locale)
203217
{
204218
if ($this->isLoaded($namespace, $group, $locale)) {
205219
return;

0 commit comments

Comments
 (0)