diff --git a/core/modules/locale/src/LocaleLookup.php b/core/modules/locale/src/LocaleLookup.php
index a534d193a3e2ee105105c3a593c828f92539c195..ab1401b35e210443c841d170a049c7ecf0871cdd 100644
--- a/core/modules/locale/src/LocaleLookup.php
+++ b/core/modules/locale/src/LocaleLookup.php
@@ -113,7 +113,12 @@ protected function getCid() {
       // for example, strings for admin menu items and settings forms are not
       // cached for anonymous users.
       $user = \Drupal::currentUser();
-      $rids = $user ? implode(':', $user->getRoles()) : '';
+      $rids = '';
+      if ($user) {
+        $roles = $user->getRoles();
+        sort($roles);
+        $rids = implode(':', $roles);
+      }
       $this->cid = "locale:{$this->langcode}:{$this->context}:$rids";
 
       // Getting the roles from the current user might have resulted in t()
diff --git a/core/modules/locale/tests/src/Unit/LocaleLookupTest.php b/core/modules/locale/tests/src/Unit/LocaleLookupTest.php
index c515f802f1093cf01630a2f6d2282d0af6b66e4b..03c0f725ecd90f7f63d5a9582c20320101d0bb9d 100644
--- a/core/modules/locale/tests/src/Unit/LocaleLookupTest.php
+++ b/core/modules/locale/tests/src/Unit/LocaleLookupTest.php
@@ -338,4 +338,48 @@ public function providerFixOldPluralTranslationProvider() {
     ];
   }
 
+  /**
+   * @covers ::getCid
+   *
+   * @dataProvider getCidProvider
+   */
+  public function testGetCid(array $roles, $expected) {
+    $this->user = $this->createMock('Drupal\Core\Session\AccountInterface');
+    $this->user->expects($this->any())
+      ->method('getRoles')
+      ->will($this->returnValue($roles));
+
+    $container = new ContainerBuilder();
+    $container->set('current_user', $this->user);
+    \Drupal::setContainer($container);
+
+    $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
+      ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack])
+      ->getMock();
+
+    $o = new \ReflectionObject($locale_lookup);
+    $method = $o->getMethod('getCid');
+    $method->setAccessible(TRUE);
+    $cid = $method->invoke($locale_lookup, 'getCid');
+
+    $this->assertEquals($expected, $cid);
+  }
+
+  /**
+   * Provides test data for testGetCid().
+   */
+  public function getCidProvider() {
+    return [
+      [
+        ['a'], 'locale:en:irrelevant:a',
+      ],
+      [
+        ['a', 'b'], 'locale:en:irrelevant:a:b',
+      ],
+      [
+        ['b', 'a'], 'locale:en:irrelevant:a:b',
+      ],
+    ];
+  }
+
 }