* * *

Author Topic: The meaning of %s  (Read 1516 times)

nikel

  • Jr. Member
  • **
  • Posts: 58
The meaning of %s
« on: April 27, 2018, 07:37:16 am »
Hello, I found a lot of mistakes in Turkish translation and I'm trying to fix them. There are some wildcards like %s in the translation file. I want to ask what does the second %s mean here:

Quote
Unable to clean up "%s".%sPlease check permissions.

EDIT: And is there a tool for previewing language phrases?
« Last Edit: April 27, 2018, 07:49:40 am by nikel »

GetMem

  • Hero Member
  • *****
  • Posts: 3202
Re: The meaning of %s
« Reply #1 on: April 27, 2018, 07:58:49 am »
It means that %s will be replaced at run time by some string:
Code: Pascal  [Select]
  1. var
  2.   value1: string;
  3.   value2: string;
  4. begin
  5.   value1 := 'test';
  6.   value2 := 'You have insufficient privileges.';
  7.   ShowMessage(Format('Unable to clean up "%s".%sPlease check permissions.', [value1, value2]));
  8. end;

The final message would look like this: "Unable to cleanup "test".You have insufficient privileges.Please check permissions."
As a conclusion you should leave %s as it is.
« Last Edit: April 27, 2018, 08:04:10 am by GetMem »

nikel

  • Jr. Member
  • **
  • Posts: 58
Re: The meaning of %s
« Reply #2 on: April 27, 2018, 09:21:02 am »
Hmm thanks for the tip.

I want to advise some changes for translation files. I'm sure this is a neccessity for some other languages as well.

For example if the source string is:

Quote
There were errors while moving the file %s to directory %s

If someone wants to translate this string to Turkish, he needs to swap %s'  I mean this would be better:

Quote
There were errors while moving the file %s1% to directory %s2%

Where can I suggest this feature?

GetMem

  • Hero Member
  • *****
  • Posts: 3202
Re: The meaning of %s
« Reply #3 on: April 27, 2018, 09:35:05 am »
Quote
Where can I suggest this feature?
Lazarus mailing list. You can subscribe here: http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

wp

  • Hero Member
  • *****
  • Posts: 5035
Re: The meaning of %s
« Reply #4 on: April 27, 2018, 09:35:44 am »
In this case you should file a bug report because the format string is not correct, it needs to contain index parameters, like in
Code: Pascal  [Select]
  1.   s := Format('There were errors while moving the file %0:s to directory %1:s', [AFileName, ADirectory]);
The number between % and the colon refer to the index in the parameter list. This is a built-in feature of the Format() function (https://www.freepascal.org/docs-html/rtl/sysutils/format.html).

The Turkisch translation then would be (writing in English because I cannot speak Turkish):
Quote
'There were errors while moving to directory %1:s the file %0:s'

Or, if you did not work with BugTracker (https://bugs.freepascal.org/view_all_bug_page.php?project_id=1) before, please post here where this line is, I can change it for you.
« Last Edit: April 27, 2018, 09:37:46 am by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

nikel

  • Jr. Member
  • **
  • Posts: 58
Re: The meaning of %s
« Reply #5 on: April 27, 2018, 10:11:41 am »
Thanks for the quick replies. I just took a wild guess, there isn't such a line probably, I'm talking in general. However I must say I haven't come across a wildcard like %0:s, all the wildcards I've seen were %s (I call them wildcard and hope I'm right).
« Last Edit: April 27, 2018, 10:14:32 am by nikel »

nikel

  • Jr. Member
  • **
  • Posts: 58
Re: The meaning of %s
« Reply #6 on: April 27, 2018, 10:39:53 am »
Can I use %0:s and %1:s as if they exist in source strings?

wp

  • Hero Member
  • *****
  • Posts: 5035
Re: The meaning of %s
« Reply #7 on: April 27, 2018, 12:00:53 pm »
I think so, and from this POV it is not even required to change the sources at all.

Without the index parameters the values are inserted according to the order in the parameter list of the Format command. With the index parameters you instruct Format() to use them in a different order. The only important thing is that you keep the type and count of the parameters.

Example:
Code: Pascal  [Select]
  1. resourcestring
  2.   RSErrorMsg = 'Error #d (%s)';
  3. var
  4.   msg: String;
  5.   errCode: Integer;
  6.   errText: String;
  7. ...
  8.   errCode := 512;
  9.   errText: 'Disk full'
  10.   msg := Format(RSErrorMsg, [errCode, errText]); // --> 'Error #512 (Disk full)'
Here the first parameter passed is the integer errCode (--> %d for integer), the second parameter is the string errText (--> %s). If you want the errText to appear before the errCode just translate the RSErrMsg to 'Error "%1:s" (code: %0:d)' because the integer parameter is at index 0, the string parameter at index 1 of the parameter list in the square brackets.

'Error %s (code %d)' will not work because the first parameter to be used should be astring but above code has an integer as first parameter, the same arguments holds for the second parameter.

'Error %2:s (code %0:d)' will not work either because %2:s refers to a third parameter which does not exist.

'Error %d' or 'Error %1:s', however, will work because it is not required to refer to all parameters passed to Format().
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

nikel

  • Jr. Member
  • **
  • Posts: 58
Re: The meaning of %s
« Reply #8 on: April 27, 2018, 01:50:19 pm »
I searched a lot to find simple string to try if that worked. Finally I found this line with id 749:

Quote
(size: %d:%d, bpp: %d)

%0:d, %1:d and %2:d worked fine! Thanks for the help.


 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus