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

Possible custom string resolver node #170

Open
LennardDeurman opened this issue Oct 19, 2023 · 2 comments
Open

Possible custom string resolver node #170

LennardDeurman opened this issue Oct 19, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@LennardDeurman
Copy link

LennardDeurman commented Oct 19, 2023

Motivation
Within the application I'm currently developing we encounter some strings that are dependent on a count parameter but do not strictly adhere the pluralization structure as Slang is using it. Take the following example into account:

"streak_banner": {
      "l_one": "The algorithm favors active people",
      "l_two": "Amazing to see your enthusiasm",
      "l_three": "Your daily response improves matchmaking",
      "l_four": "We're putting a spotlight on your profile!",
      "l_five": "You're really here to take it offline!",
      "l_six": "One more day makes a week",
      "l_seven": "You're taking matters in your own hands",
      "l_eight": "Every day, we get to know your taste better",
      "l_nine": "Good to see you again!",
      "l_ten": "People who respond daily get shown more to others",
      "l_eleven": "Increased chances of finding a fun date",
      "l_twelve": "Our algorithm is in love with your activity",
      "l_thirteen": "Almost at the two-week mark",
      "l_fourteen": "You're on a roll, keep it up!"
    }

In this example above we're defining a set of strings that should be called based on the input parameter. Rather than defining custom conditions in the code I would prefer calling with the parameter, whereinafter slang decides on the correct key to use. This feature should support scenarios where the pluralization doesn't suffice.

Developer Experience
In my fork I've implemented this by allowing the node to include parameters, and a resolver attribute, as the following:
"resolver_example(resolver=numericResolver, params=languageCount)": {
params is a comma splitted string representing the arguments that can be used to determine the key that should be picked. Instead of the returning a string an enum representing the possible keys could also be used.

String? numericResolver({required Object languageCount}) {
  if (languageCount == 1) {
    return 'l_one';
  } else if (languageCount == 2) {
    return 'l_two';
  } else {
    return 'l_other';
  }
}

I'm curious about opinions about this potential feature, if in favor of implementing this I can submit a PR for this with my current implementation.

@LennardDeurman LennardDeurman added the enhancement New feature or request label Oct 19, 2023
@Tienisto
Copy link
Member

Tienisto commented Oct 19, 2023

Hello, the plural system is actually standardized (see https://www.unicode.org/cldr/charts/43/supplemental/language_plural_rules.html)

Since, you mentioned enums, have you considered the Enums Feature of slang?

In this example, you would have an enum like

enum StreakLevel {
  one,
  two,
  // ...
  fourteen,
}

@LennardDeurman
Copy link
Author

Hi, Thanks for the quick response!

I indeed looked into the enums feature as well, but the enum feature requires the group to have every enum and cannot exclude certain keys.

In my example I'm using all the keys, but I would like to have the possibility to leave out certain keys and dynamically based on the input arguments select the key / enum and have some flexibility here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants