How to Translate text without losing newline characters?

Viewed 563

I'm using Firebase ML Kit Translate to translate some text from Japanese to English.

I'm just using the example code they have in their documentation:

func translate (input: String) {

      print(input)
      translator.translate(input) { translatedText, error in
      guard error == nil, let translatedText = translatedText else { return }

      self.output = translatedText
      print(self.output)

      return
    }

When I input Japanese text, I notice all the newline characters are ignored and the English translation is in one block of text. Is there a setting to enable newlines or is this a bug?

1 Answers

This is known behavior of the underlying translation engine, which strips out any non-linguistic content.

In general, it's difficult to impossible to accurately preserve non-linguistic content in translation, as the grammatical structure of the translated sentence might be entirely different, so there is no natural place for the separators to go. For example, if I were to translate the sentence "I saw \n the dog" (with a newline in the middle) into German, the German version of the pure sentence is "Ich habe den Hund gesehen", with the predicate at the end of the sentence and the object in the middle. Should the newline go after the predicate ("Ich habe den Hund gesehen \n") or before the object ("Ich habe \n den Hund gesehen")?


If the text is broken into paragraphs (or other blocks) along sentences, you could translate each of them separately, and then join them again:

List<Task<String>> tasks = new ArrayList<>();
for (String paragraph : text.split("\n\n")) {
    tasks.add(translator.translate(paragraph));
}
Task<String> resultTask = Tasks.whenAll(tasks).onSuccessTask(aVoid -> {
    List<String> results = new ArrayList<>();
    for (Task<String> task : tasks) {
        results.add(task.getResult);
    }
    return Tasks.forResult(String.join("\n\n", results));
});

or in Swift:

let paragraphs = text.components(separatedBy: "\n\n")
var results = [String]()
for paragraph in paragraphs {
  translator.translate(paragraph) { result, error in
    guard let unwrappedResult = result else {
      // Handle error
      return
    }
    results.append(unwrappedResult)
    if (results.count == paragraphs.count) {
      let result = results.joined(separator: "\n\n")
      // Handle result
    }
  }
}
Related