How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Having a question about translating phpBB 3.3? Want to discuss and collaborate with people currently translating phpBB 3.3? Here would be the correct place to do so.
User avatar
scootergrisen
Translator
Posts: 257
Joined: Thu Aug 25, 2011 2:25 pm

How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by scootergrisen »

How can I change %2$.2f in strings like the following string so that I get a "," in the number instead of a "."?

Code: Select all

	'MIGRATION_DATA_DONE'				=> 'Installed Data: %1$s; Time: %2$.2f seconds',
It might say "1.23 seconds" but I want "1,23 seconds".
User avatar
Mike-on-Tour
Registered User
Posts: 538
Joined: Wed Jan 15, 2020 3:51 pm
Location: Germany
Name: Michael

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by Mike-on-Tour »

Have you tried %2$,2f?
Watz fo lunch?
If you like my extensions or my support please consider a donation: Image
User avatar
scootergrisen
Translator
Posts: 257
Joined: Thu Aug 25, 2011 2:25 pm

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by scootergrisen »

Yes.
User avatar
halil16
Registered User
Posts: 1448
Joined: Fri Jul 24, 2020 11:30 pm
Location: Turkiye
Name: Halil

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by halil16 »

Then did you clear the cache?
Buy me a coffee ☕
Hire me for your phpBB board. 🚩
Introducing Mobile Upgrade! *Make your phpBB board like an app! 📱
O BeldeThatTowns*for sale*$2250 🛒
"The day we'll need ideas more than possessions, we'll find the secret to true wealth." - Peyami Safa /peˈjɑːmi saˈfɑː/
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 622
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by danieltj »

Someone with more php knowledge should be able to advise you better but it's my understanding that this string is specifically used for outputting floats which should always be X.YZ in formation, such as 1.23 or 45.67 or 789.00.

This language string relies on the php sprintf function which allows you to specify a string of text and include placeholders for dynamic data where $1%s is the first placeholder expecting a string (ie. Hello World) and $2%.2f is the second and is expecting a float (ie. 12.34).

Therefore you can't simply change this from a period to a comma because that's not going to be accepted by the php function when it comes to replacing the placeholders with the data.

Your best bet is to use something like number_format after the text has been replaced, not by trying to update the string in a language file.
User avatar
scootergrisen
Translator
Posts: 257
Joined: Thu Aug 25, 2011 2:25 pm

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by scootergrisen »

Are there any way for translators to choose the value for setlocale?
Anișor
Translator
Posts: 338
Joined: Tue Jan 08, 2013 9:36 pm
Location: Arbroath, Angus, Scotland

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by Anișor »

Maybe you can use regex with javascript. So you don’t need to change any core files.
rxu
Extensions Development Team
Posts: 3954
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by rxu »

scootergrisen wrote: Mon Aug 19, 2024 5:31 am Are there any way for translators to choose the value for setlocale?
I guess it's impossible using translation. Changing locale would require core code modification or creating some phpBB extension.
User avatar
scootergrisen
Translator
Posts: 257
Joined: Thu Aug 25, 2011 2:25 pm

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by scootergrisen »

I notice the translator is able to make the code/page fail if for example the translator changes "%2$.2f" to "%2$,2f".
I don't like that.
I guess the translation checker might catch it.
But if there is always suppose to be 2 decimals in all languages then maybe better to just have a simple string like "%2$s" or "%s" or whatever it is suppose to be.
Then translator don't have to deal with decimals.
And then the people progamming can decide on how many decimals there should be. Maybe they want to change from 2 to 1 decimal or removed the decimal instead of having to ask all the translators to change the strings.
rxu
Extensions Development Team
Posts: 3954
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by rxu »

Just a bit of clarifying.
%2$.2f can't be changed to %2$,2f because it's PHP syntax error: sprintf format requires dot followed by a digit to set up a floating point precision.
Also I think translators should be able to change that format to what they think is correct for their language. F.e. using %2$.1f will give 1-digit presicion for the float or using %2$d will print integer non-float value.
But changing the floating separator from dot to comma etc. can't be done in translation, that is by design.
User avatar
Terceirense
Registered User
Posts: 56
Joined: Mon Oct 09, 2006 1:19 pm
Location: Açores Ilha Terceira

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by Terceirense »

You can use the NumberFormatter class in PHP to format numbers with commas. Here’s a quick example of how to do it:
$formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
$formatter->setSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, ',');

$number = 1.23;
$formattedNumber = $formatter->format($number);

echo sprintf('Installed Data: %1$s; Time: %s seconds', 'Data', $formattedNumber);

This will format 1.23 as 1,23. Just replace en_US with your locale if needed.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6423
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by thecoalman »

I may be mistaken but official translations can't have php added.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 622
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: How do I get "%2$.2f seconds" to show "1,23" instead of "1.23"

Post by danieltj »

thecoalman wrote: Thu Aug 22, 2024 6:27 pm I may be mistaken but official translations can't have php added.
You’re correct. You can’t use php functions inside phpBB language strings.

Return to “[3.3.x] Translations”