php-gettext / Gettext

PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
MIT License
687 stars 134 forks source link

Way to generate string location path relative? #208

Open devnix opened 5 years ago

devnix commented 5 years ago

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)

oscarotero commented 5 years ago

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.

devnix commented 5 years ago

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.

oscarotero commented 5 years ago

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

devnix commented 5 years ago

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 commented 5 years ago

This could be a good usage?

$translations->addFromJsCodeFile($file, [
    'base_path' => '/var/www/html'
]);
oscarotero commented 5 years ago

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.

maicol07 commented 4 years ago

Any news on this one?

mjrider commented 4 years ago

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