Way to programmatically add string translations? is there a way (snippet) to programmatically add translations for strings (user interface translation), e.g. in hook_update_N()?

A way in Drupal 7 was http://dropbucket.org/node/323. 

/**
 * Helper to manually add a single translation string.
 *
 * After adding strings use locale_clear_cache() for clearing caches.
 */
function locale_add_translation($source, $langcode, $translation, $context = '', $textgroup = 'default') {
  require_once DRUPAL_ROOT . '/includes/locale.inc';

  $report = &amp;drupal_static(__FUNCTION__, array('additions' =&gt; 0, 'updates' =&gt; 0, 'deletes' =&gt; 0, 'skips' =&gt; 0));
  _locale_import_one_string_db($report, $langcode, $context, $source, $translation, $textgroup, 'Manual import via helper ' . __FUNCTION__ .'().', LOCALE_IMPORT_OVERWRITE);
}

/**
 * Helper to clear the locale cache.
 */
function locale_clear_cache() {
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  // Clear locale cache.
  _locale_invalidate_js();
  cache_clear_all('locale:', 'cache', TRUE);
}


Is there anything like this for Drupal 8?
Put the following helper function in your custom module:

use \Drupal\locale\SourceString;

function mymodule_add_translation($source_string, $translated_string, $langcode) {
  // Find existing source string.
  $storage = \Drupal::service('locale.storage');
  $string = $storage-&gt;findString(array('source' =&gt; $source_string));
  if (is_null($string)) {
    $string = new SourceString();
    $string-&gt;setString($source_string);
    $string-&gt;setStorage($storage);
    $string-&gt;save();
  }
  // Create translation. If one already exists, it will be replaced.
  $translation = $storage-&gt;createTranslation(array(
    'lid' =&gt; $string-&gt;lid,
    'language' =&gt; $langcode,
    'translation' =&gt; $translated_string,
  ))-&gt;save();
}