slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
474 stars 39 forks source link

Add lists to missing translations when length changed #131

Closed adil192 closed 1 year ago

adil192 commented 1 year ago

Motivation For my app Saber I have a list (array) of FAQ questions and answers. When I add more things to this list, the changes aren't reflected in the missing translations file.

{
  "somelist": [
    "one",
    "two",
    "NEW"
  ]
}

Developer Experience Running flutter pub run slang analyze should include lists in the missing translations file. This could be one of:

  1. Assuming the new entry to the list is always at the end, only add the last n entries to the missing translations file.
    {
      "fr": {
        "somelist": [
          "NEW"
        ]
      }
    }
  2. Add the whole list from the main translation file. It would be up to the translater to copy over the old translations from their file.
    {
      "fr": {
        "somelist": [
          "one",
          "two",
          "NEW"
        ]
      }
    }
  3. Add both translations to the list and expect the translator to delete the duplicates. We could benefit from a check when running slang apply that the list length <= the original translation's list length.

    {
      "fr": {
        "somelist": [
          "un",
          "deux",
    
          "one",
          "two",
          "NEW"
        ]
      }
    }
  4. Add a stub that tells the translator to edit the translation file directly.
    {
      "fr": {
        "somelist": ["Please edit strings.fr.i18n.json"]
      }
    }
Tienisto commented 1 year ago

Hi @adil192

a temporary workaround is to use normal objects. To make each of your q-a pair reusable, you can use "interfaces".

"faq(interface=FaqModel)": {
  "nextcloud": {
    "q": "What is Nextcloud?",
    "a": "Nextcloud is a..."
  },
  "encryptionPassword": {
    "q": "What is the encryption password?",
    "a": "..."
  }
}

Then you can create a widget that needs a FaqModel as constructor parameter.

List(
  children: [
    FaqItem(t.faq.nextcloud),
    FaqItem(t.faq.encryptionPassword),
    // ...
  ],
);

One drawback of this workaround is that you cannot iterate anymore.

adil192 commented 1 year ago

For that I'd need to manually edit every translation file, so just catching the missing translations in PR reviews seems easier. I'm happy to implement one of the above solutions (I prefer # 3) if you want me to btw :)

Tienisto commented 1 year ago

The "outdated" flag should be ok in most use cases.

The problem I still see is that sometimes the lists have different lengths on purpose. Maybe there needs to be another flag to tell slang analyze that it is ok that the list have different lengths.

adil192 commented 1 year ago

Yes the "outdated" flag works for me, I'm happy to close this issue if you are

Tienisto commented 1 year ago

Alright!