Home > OS >  how to replace some string from outside in json file
how to replace some string from outside in json file

Time:04-15

I have a localization map like:

  'en': {
    "timeIsUpTitle": "Time is Up!",

    "workingTimeText": "You are working for{{}} {{}} {{}}.."
  },
  'tr': {
    'timeIsUpTitle': 'Zaman doldu!',

    "workingTimeText": "{}{}{}çalışıyorsun..."
  },

how can I fill these {} sections on the outside? I have some getters for these strings such as,

  String  workingTimeText(String hours, String minutes, String seconds) => localeValues['workingTimeText']!;

  String get timeIsUpTitle => localeValues['timeIsUpTitle']!;

CodePudding user response:

Here is a simple example of how to update the Map. You can create your own function based on your preferences.

void main() {
  for (var key in data.keys) {
    Map<String, String>? value = data[key];
    if (value == null) return;
    value.update("workingTimeText", (value) => 'New Data');
  }

  data.values.forEach(print);
}

var data = {
  "en": {
    "timeIsUpTitle": "Time is Up!",
    "workingTimeText": "You are working for{{}} {{}} {{}}.."
  },
  "tr": {
    "timeIsUpTitle": "Zaman doldu!",
    "workingTimeText": "{}{}{}çalışıyorsun..."
  }
};

Output:

{timeIsUpTitle: Time is Up!, workingTimeText: New Data}
{timeIsUpTitle: Zaman doldu!, workingTimeText: New Data}

CodePudding user response:

I solved with format package in pub dev (it's the same as python's format funct.)

  • Related