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

Translation of text with spaces is taken from messages.local.neon #26

Closed
TomasHalasz opened this issue Jun 11, 2020 · 7 comments
Closed

Comments

@TomasHalasz
Copy link

I've issue with translation of text with spaces. For example $translator->translate('Kočka leze dírou, pes oknem'); is looking for string "Kočka leze dírou, pes oknem" in default messages.local.neon even if I hade set different domain by $translator->setPrefix(['customModule.status']); I don't know if it is error or behavior by design.

My idea is to replace spaces with underscores in messages, because it's still readable in case of missing translate in neon.
And the best way in my opinion is in Translator.php :

public function translate($message, ...$parameters): string // @comment

{

        $message = str_ireplace(" ","_",$message);


Nice would be if user can set on or off this replacing space by underscore. What do you think?

@TomasHalasz TomasHalasz changed the title Ttranslation of text with spaces is taken from messages.local.neon Translation of text with spaces is taken from messages.local.neon Jun 11, 2020
@aleswita
Copy link
Member

If message contain dot and space, translator use message domain by default. Its expected behaviour.
https://github.com/contributte/translation/blob/master/src/Helpers.php#L26

Try this:
$translator->translate('My message with spaces.', null, [], 'my_domain');

@TomasHalasz
Copy link
Author

Thanks but it doesn't help. Tracy said that this translation is missing:

ID: applicationModule.invoice.Forma úhrady: 
Domain: messages
Count:  1

It's in form:

    protected function createComponentEdit($name)
    {	
        $form = new Form($this, $name);
        $form->setTranslator($this->translator);
        $this->translator->setPrefix(['applicationModule.invoice']);
        $arrPay = $this->PaymentTypesManager->findAll()->order('name')->fetchPairs('id','name');
        $form->addSelect('cl_payment_types_id',$this->translator->translate('Forma úhrady:'),$arrPay)
            ->setTranslator(NULL)
            ->setHtmlAttribute('class','form-control chzn-select input-sm');

And lang file is applicationModule.cs_CZ.neon:

invoice:
	EditDocNum: "upravit číslo dokladu"
	Forma úhrady:: "Forma úhrady:"

Result of this in html is: applicationModule.invoice.applicationModule.invoice.Forma úhrady:
The same result I get if I remove: $this->translator->setPrefix(['applicationModule.invoice']);
and use in form: $form->addSelect('cl_payment_types_id',$this->translator->translate('Forma úhrady:', null, [], 'applicationModule.invoice'),$arrPay)

So I don't realy know why is my translation domain ignored....

@aleswita
Copy link
Member

Code bellow works for me:

$translator->setPrefix(['a']);
var_dump($translator->translate('Ahoj jak se mas:', [], 'my_domain'));
a:
	Ahoj jak se mas:: "hmm"

@TomasHalasz
Copy link
Author

Ok, and in your example, neon file with translation has name messages.cs_CZ.neon? If yes, it works and its correct. But in my case I need to have translation file named according to domain name.

In next example is translator still looking for translation into messages.cs_CZ.neon:

$translator->setPrefix(['applicationModule.invoice']);
var_dump($translator->translate('Forma úhrady:'));

applicationModule.cs_CZ.neon:

invoice:
	Forma úhrady:: "Forma úhrady:"

And I think that reason is in Helpers.php:

public static function extractMessage(string $message): array
	{
		if (strpos($message, '.') !== false && strpos($message, ' ') === false) {
			[$domain, $message] = explode('.', $message, 2);

		} else {
			$domain = 'messages';
		}

		return [$domain, $message];
	}

Reason is directly in first line with condition. Because exploding string by '.' is done only if $messsage don't contain space.

strpos($message, ' ') === false

And in my message is space, so domain is automaticly given

		} else {
			$domain = 'messages';
		}

Is there some reason to have this part of condition ?
strpos($message, ' ') === false

Wouldn't be better only this ?

        if (strpos($message, '.') !== false ) {		    
			[$domain, $message] = explode('.', $message, 2);
		} else {
			$domain = 'messages';
		}

Thanks for your help! :-)

@aleswita
Copy link
Member

My code works for every domain name, look on third parameter. If you set domain in parameter, extract message will be skip -> this is solution for you.

But no idea why strpos($message, ' ') === false is used. You can try send PR with solution without BC breaks.

@TomasHalasz
Copy link
Author

ok I will send PR. Thanks

@aleswita
Copy link
Member

@TomasHalasz can you try "space" branch?

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

No branches or pull requests

2 participants