Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to generate string location path relative? #208

Open
devnix opened this issue Jun 5, 2019 · 8 comments
Open

Way to generate string location path relative? #208

devnix opened this issue Jun 5, 2019 · 8 comments

Comments

@devnix
Copy link

devnix commented Jun 5, 2019

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
Copy link
Member

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
Copy link
Author

devnix commented Jun 6, 2019

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
Copy link
Member

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

@devnix
Copy link
Author

devnix commented Jun 7, 2019

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
Copy link
Author

devnix commented Jun 7, 2019

This could be a good usage?

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

@oscarotero
Copy link
Member

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
Copy link

maicol07 commented Mar 7, 2020

Any news on this one?

@mjrider
Copy link

mjrider commented May 26, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants