Skip to content

Conversation

@HastagGuigui
Copy link
Contributor

@HastagGuigui HastagGuigui commented Oct 13, 2022

Formattable strings can be used to change dynamically parts of strings depending on different parameters.

Basic usage

To use it in any commands, just call it by formatstring() It takes two arguments as well as a third optional one for plural.

For example, let's take a string that can be formatted using this function:

<@{1}> successfully put {2} {3} **{4}** card{3s} into storage.

This will be lang.stored_message.

Then, let's also take some arguments. We can see here that we need 4 arguments (the user id, their pronoun "their", the amount of cards stored, and the card(s) stored.)
These will be in order: "448560475987509268", "his", an integer (4), and "Samurai (Rare)"

But wait, isn't there 5 arguments?
You're right, but the fifth argument is the plural s and is only put if the amount of cards being stored at a time is not equal to 1, so I added it in! This is what the {Xs} is for. If the parameter provided as argument X in the table is an integer and is not equal to 1, it will write the parameter provided as the "plural s"

Now, here's the function in action:

print(formatstring(lang.stored_message, {"448560475987509268", "his", 4, "Samurai (Rare)"}, lang.plural_s))
-- Output: <@448560475987509268> successfully put his 4 **Samurai (Rare)** cards into storage.

Isn't that great?

Why adding it?

It makes translating easier, because you don't have to have a set order for variables, and you won't need a ton of if else per language.

How do I try it out?

c!runlua print(formatstring("bogo{1s} {2}" {7, "binted"}, "s"))

If it prints out "bogos binted" in the console, it works!

TODO

  • Implement the actual function
  • Write an overly complicated pull request message
  • Change all existing commands to use this new method

hopefully this is a good enough pull request message :edegabudgetcuts:

HastagGuigui and others added 2 commits October 13, 2022 13:26
It takes in a base string with formattable tags, the values in question, and then optionally a plural s when needed
HastagGuigui added a commit to HastagGuigui/rdtradingcards that referenced this pull request Dec 30, 2022
formatstring() is not yet present in RDcards, i really need to finish DPS2004#50
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

Successfully merging this pull request may close these issues.

3 participants