Gettext icon indicating copy to clipboard operation
Gettext copied to clipboard

Way to generate string location path relative?

Open devnix opened this issue 6 years ago • 12 comments

I was wondering if there is any possibility of generating a relative path when I'm generating a .pot file when scanning files. Por example, not having lines like

#: /var/www/html/httpdocs/index.php:539
msgid "Hello world"
msgstr ""

and getting something more like

#: ../index.php:539
msgid "Hello world"
msgstr ""

if the pot file is being generated on /var/www/html/httpdocs/translations (just a fast example)

devnix avatar Jun 05 '19 13:06 devnix

This is not implemented currently. To do it, we should edit this line: https://github.com/oscarotero/Gettext/blob/master/src/Extractors/Extractor.php#L17 For example, we could provide an option to define the base path used to calculate the relative path of all files, like this:

$translations = Translations::fromPhpCode($file, ['basepath' => __DIR__]);

I don't think to have time to do this, if you want to help with a pull request, I'll happily review it.

oscarotero avatar Jun 05 '19 16:06 oscarotero

I would like to see if I can get some time for this. What would be the best default option? I personally would find the relative path the best option.

devnix avatar Jun 06 '19 07:06 devnix

For backward compatibility, the default option should be absolute path. Relative path will be generated only if basepath option is passed.

oscarotero avatar Jun 06 '19 09:06 oscarotero

That's what I was figuring. Let's see if I can make a PR this weekend.

So, we are limited to passing the base path to calculate a relative one, right? There is this neat method from symfony/filesystem but I dont know If you are willing to include another dependency just for this method.

If we are not going to add that component as a dependency, we would be breaking any license if we just copy and paste that method?

devnix avatar Jun 07 '19 08:06 devnix

This could be a good usage?

$translations->addFromJsCodeFile($file, [
    'base_path' => '/var/www/html'
]);

devnix avatar Jun 07 '19 08:06 devnix

This could be a good usage?

Yes, perfect.

...I dont know If you are willing to include another dependency just for this method.

No, this should be implemented without include any dependency

we would be breaking any license if we just copy and paste that method?

I think we can simplify this method, not only copy and paste as is. Anyway, the license is MIT so I think there's no problem, but I'd include a php comment referencing to the original method.

oscarotero avatar Jun 07 '19 09:06 oscarotero

Any news on this one?

maicol07 avatar Mar 07 '20 12:03 maicol07

for ppl wanting this, this is the code we have running for a while now

$finder = new Symfony\Component\Finder\Finder();

$finder
    ->files()
    ->exclude('vendor')
    ->ignoreDotFiles(true)
    ->ignoreUnreadableDirs()
    ->in('.')
    ->sortByName()
    ->name('*.php');

foreach ($finder as $file) {
    // dumps the relative path to the file
    printf("%s\n", $file->getRelativePathname());
    $phpScanner->scanFile((string)$file->getRelativePathname());
}

which gives us relative filenames

mjrider avatar May 26 '20 09:05 mjrider