conedevelopment / i18n

Push your Laravel translations to the front-end and use them easily with JavaScript.
https://pineco.de/using-laravels-localization-js/
MIT License
129 stars 21 forks source link

feat: Adds support for packages translation #5

Closed sardoj closed 5 years ago

sardoj commented 5 years ago

It would be useful to use translation also with custom packages.

This pull request adds support for packages translation.

iamgergo commented 5 years ago

@sardoj Hey! Thanks for the PR (and sorry for the late answer)! I really like the idea!

Please let me review the code and work on a little, then I will return to you!

Thanks a lot!

sardoj commented 5 years ago

Thank you !

Maybe we can also add support for a fallback locale.

if (!is_null($dirpath)) {
   if (strpos($dirpath, base_path()) === false) {
        $dirpath = base_path($dirpath.'/'.App::getLocale());
        // ===> If this locale is not supported by the package, it there will be no translations.
        // Maybe we can use config('app.fallback_locale') if '$dirpath' doesn't exist.
    }
} else {
    $dirpath = resource_path('lang/'.App::getLocale());
}
iamgergo commented 5 years ago

@sardoj I really appreciate your idea!

I chose another approach since you can reach all the registered package translations with their path, there is no need to pass any extra parameter to the @translations directive, it can be done behind the scenes. Also, for the fallback locale, I used a similar approach, I just saw your comment after.

Also, for changing the separator, I think it's the best if we stick with the default separator. No confusion then.

So if you don't mind I close this PR, but in the readme and the changelog, where I document the package part, I will mention you in the very first.

sardoj commented 5 years ago

Hello, Thank you for your new release! But i've a real problem with the separator, because I use dot character in my translations :

// resources/lang/en/default.php
return [
    'test' => 'test1',
    'field.test' => 'test2',
];

It works well with Laravel if we call trans('default.test') or trans('default.field.test'). Could we do the same with your package please?

iamgergo commented 5 years ago

@sardoj Hi!

I see. But having a structure like this, would solve your problem, no? Or it's a bit more complicated than this?

// resources/lang/en/default.php
return [
    'test' => 'test1',
    'field' => ['test' => 'test2'],
];
sardoj commented 5 years ago

@iamgergo You're true, it works well like this. Thank you!