0

I have a Json file (rawResult) that contains few elements that needs to be updated.

Basically any element key in the json that contains "|translate", then the value part should be replaced with the text from the respective dictionary (Spanish or English).

var rawResult = "{"fields": [{
  "fieldGroup": [
    {
      "key": "Task",
      "type": "input",
      "templateOptions": {
        "label|translate": "task_templateoptions_label",
        "placeholder|translate": "task_templateoptions_placeholder"
      },
      "validation": {
        "messages": {
          "required|translate": "task_validation_messages_required",
        }
      }
    },
    {
      "key": "Sub Task",
      "type": "input",
      "templateOptions": {
        "label|translate": "sub_task_templateOptions_label",
        "placeholder|translate": "sub_task_templateOptions_placeholder"
      }
    }
  ]
}]}"

//Dictionary for Spanish

Dictionary<string, string> dcSpanish = new Dictionary<string, string>();
dcSpanish.Add("task_templateoptions_label", "Tarea");
dcSpanish.Add("task_templateoptions_placeholder", "Ingrese el nombre de la tarea");
dcSpanish.Add("task_validation_messages_required", "El nombre de la tarea es obligatorio");
dcSpanish.Add("sub_task_templateOptions_label", "Sub tarea");
dcSpanish.Add("sub_task_templateOptions_placeholder", "Ingrese el nombre de la subtarea");

//Dictionary for English

Dictionary<string, string> dcEnglish = new Dictionary<string, string>();
dcEnglish.Add("task_templateoptions_label", "Task");
dcEnglish.Add("task_templateoptions_placeholder", "Enter the task name");
dcEnglish.Add("task_validation_messages_required", "Task name is required");
dcEnglish.Add("sub_task_templateOptions_label", "Sub task");
dcEnglish.Add("sub_task_templateOptions_placeholder", "Enter the sub task name");

So in case the Json result is requested in Spanish, then the output should be,

var result = "{"fields": [{
  "fieldGroup": [
    {
      "key": "Task",
      "type": "input",
      "templateOptions": {
        "label": "Tarea",
        "placeholder": "Ingrese el nombre de la tarea"
      },
      "validation": {
        "messages": {
          "required": "El nombre de la tarea es obligatorio",
        }
      }
    },
    {
      "key": "Sub Task",
      "type": "input",
      "templateOptions": {
        "label": "Sub tarea",
        "placeholder": "Ingrese el nombre de la subtarea"
      }
    }
  ]
}]}"

1 Answer 1

1

You could query the Json with Linq and update the values where the JProperty.Name contains the required substring (translate). For example

var jobject = JObject.Parse(rawResult);
new json = Translate(jobject,dcSpanish);

Where Translate is defined as

string Translate(JObject jo, Dictionary<string,string> translationDictionary)
{
    var keys = jo
                .DescendantsAndSelf()
                .OfType<JProperty>()
                .Where(x=>x.Name.Contains("|translate")).ToList();

    foreach(var item in keys)
    {
        item.Replace(new JProperty(item.Name.Replace("|translate",""), translationDictionary[item.Value.ToString()]));
    }
    return jo.ToString();
}

Demo Code

Sample Output

{
  "fields": [
    {
      "fieldGroup": [
        {
          "key": "Task",
          "type": "input",
          "templateOptions": {
            "label": "Tarea",
            "placeholder": "Ingrese el nombre de la tarea"
          },
          "validation": {
            "messages": {
              "required": "El nombre de la tarea es obligatorio"
            }
          }
        },
        {
          "key": "Sub Task",
          "type": "input",
          "templateOptions": {
            "label": "Sub tarea",
            "placeholder": "Ingrese el nombre de la subtarea"
          }
        }
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works for .NET framework but not for .NET core 3. But still this was helpful and I managed to get it done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.