Skip to content

Commit 66e39f4

Browse files
authored
Merge pull request #21552 from nextcloud/backport/21535/stable17
[stable17] Fix language in share notes email for users
2 parents 4b0baf1 + 046fdc4 commit 66e39f4

3 files changed

Lines changed: 62 additions & 39 deletions

File tree

lib/private/Share20/DefaultShareProvider.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
use OC\Files\Cache\Cache;
3333
use OCP\Defaults;
3434
use OCP\Files\Folder;
35+
use OCP\IConfig;
3536
use OCP\IL10N;
3637
use OCP\IURLGenerator;
3738
use OCP\IUser;
39+
use OCP\L10N\IFactory;
3840
use OCP\Mail\IMailer;
3941
use OCP\Share\IShare;
4042
use OCP\Share\IShareHelper;
@@ -79,41 +81,34 @@ class DefaultShareProvider implements IShareProvider {
7981
/** @var Defaults */
8082
private $defaults;
8183

82-
/** @var IL10N */
83-
private $l;
84+
/** @var IFactory */
85+
private $l10nFactory;
8486

8587
/** @var IURLGenerator */
8688
private $urlGenerator;
8789

88-
/**
89-
* DefaultShareProvider constructor.
90-
*
91-
* @param IDBConnection $connection
92-
* @param IUserManager $userManager
93-
* @param IGroupManager $groupManager
94-
* @param IRootFolder $rootFolder
95-
* @param IMailer $mailer ;
96-
* @param Defaults $defaults
97-
* @param IL10N $l
98-
* @param IURLGenerator $urlGenerator
99-
*/
90+
/** @var IConfig */
91+
private $config;
92+
10093
public function __construct(
10194
IDBConnection $connection,
10295
IUserManager $userManager,
10396
IGroupManager $groupManager,
10497
IRootFolder $rootFolder,
10598
IMailer $mailer,
10699
Defaults $defaults,
107-
IL10N $l,
108-
IURLGenerator $urlGenerator) {
100+
IFactory $l10nFactory,
101+
IURLGenerator $urlGenerator,
102+
IConfig $config) {
109103
$this->dbConn = $connection;
110104
$this->userManager = $userManager;
111105
$this->groupManager = $groupManager;
112106
$this->rootFolder = $rootFolder;
113107
$this->mailer = $mailer;
114108
$this->defaults = $defaults;
115-
$this->l = $l;
109+
$this->l10nFactory = $l10nFactory;
116110
$this->urlGenerator = $urlGenerator;
111+
$this->config = $config;
117112
}
118113

119114
/**
@@ -1317,46 +1312,59 @@ private function propagateNote(IShare $share) {
13171312
*/
13181313
private function sendNote(array $recipients, IShare $share) {
13191314

1320-
$toList = [];
1315+
$toListByLanguage = [];
13211316

13221317
foreach ($recipients as $recipient) {
13231318
/** @var IUser $recipient */
13241319
$email = $recipient->getEMailAddress();
13251320
if ($email) {
1326-
$toList[$email] = $recipient->getDisplayName();
1321+
$language = $this->config->getSystemValue('force_language', false);
1322+
$language = \is_string($language) ? $language : $this->config->getUserValue($recipient->getUID(), 'core', 'lang', null);
1323+
$language = $language ?? $this->config->getSystemValue('default_language', 'en');
1324+
1325+
if (!isset($toListByLanguage[$language])) {
1326+
$toListByLanguage[$language] = [];
1327+
}
1328+
$toListByLanguage[$language][$email] = $recipient->getDisplayName();
13271329
}
13281330
}
13291331

1330-
if (!empty($toList)) {
1332+
if (empty($toListByLanguage)) {
1333+
return;
1334+
}
1335+
1336+
foreach ($toListByLanguage as $l10n => $toList) {
13311337

13321338
$filename = $share->getNode()->getName();
13331339
$initiator = $share->getSharedBy();
13341340
$note = $share->getNote();
13351341

1342+
$l = $this->l10nFactory->get('lib', $l10n);
1343+
13361344
$initiatorUser = $this->userManager->get($initiator);
13371345
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
13381346
$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
1339-
$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1340-
$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
1347+
$plainHeading = $l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1348+
$htmlHeading = $l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
13411349
$message = $this->mailer->createMessage();
13421350

13431351
$emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
13441352

1345-
$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
1353+
$emailTemplate->setSubject($l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
13461354
$emailTemplate->addHeader();
13471355
$emailTemplate->addHeading($htmlHeading, $plainHeading);
13481356
$emailTemplate->addBodyText(htmlspecialchars($note), $note);
13491357

13501358
$link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
13511359
$emailTemplate->addBodyButton(
1352-
$this->l->t('Open »%s«', [$filename]),
1360+
$l->t('Open »%s«', [$filename]),
13531361
$link
13541362
);
13551363

13561364

13571365
// The "From" contains the sharers name
13581366
$instanceName = $this->defaults->getName();
1359-
$senderName = $this->l->t(
1367+
$senderName = $l->t(
13601368
'%1$s via %2$s',
13611369
[
13621370
$initiatorDisplayName,

lib/private/Share20/ProviderFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ protected function defaultShareProvider() {
8686
$this->serverContainer->getLazyRootFolder(),
8787
$this->serverContainer->getMailer(),
8888
$this->serverContainer->query(Defaults::class),
89-
$this->serverContainer->getL10N('sharing'),
90-
$this->serverContainer->getURLGenerator()
89+
$this->serverContainer->getL10NFactory(),
90+
$this->serverContainer->getURLGenerator(),
91+
$this->serverContainer->getConfig()
9192
);
9293
}
9394

tests/lib/Share20/DefaultShareProviderTest.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OCP\Defaults;
2626
use OCP\Files\File;
2727
use OCP\Files\Folder;
28+
use OCP\IConfig;
2829
use OCP\IDBConnection;
2930
use OCP\IGroup;
3031
use OCP\IL10N;
@@ -34,8 +35,10 @@
3435
use OCP\IGroupManager;
3536
use OCP\Files\IRootFolder;
3637
use OC\Share20\DefaultShareProvider;
38+
use OCP\L10N\IFactory;
3739
use OCP\Mail\IMailer;
3840
use OCP\Share\IShare;
41+
use PHPUnit\Framework\MockObject\MockObject;
3942

4043
/**
4144
* Class DefaultShareProviderTest
@@ -63,6 +66,9 @@ class DefaultShareProviderTest extends \Test\TestCase {
6366
/** @var \PHPUnit_Framework_MockObject_MockObject|IMailer */
6467
protected $mailer;
6568

69+
/** @var IFactory|MockObject */
70+
protected $l10nFactory;
71+
6672
/** @var \PHPUnit_Framework_MockObject_MockObject|IL10N */
6773
protected $l10n;
6874

@@ -78,9 +84,11 @@ public function setUp() {
7884
$this->groupManager = $this->createMock(IGroupManager::class);
7985
$this->rootFolder = $this->createMock(IRootFolder::class);
8086
$this->mailer = $this->createMock(IMailer::class);
87+
$this->l10nFactory = $this->createMock(IFactory::class);
8188
$this->l10n = $this->createMock(IL10N::class);
8289
$this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
8390
$this->urlGenerator = $this->createMock(IURLGenerator::class);
91+
$this->config = $this->createMock(IConfig::class);
8492

8593
$this->userManager->expects($this->any())->method('userExists')->willReturn(true);
8694

@@ -94,8 +102,9 @@ public function setUp() {
94102
$this->rootFolder,
95103
$this->mailer,
96104
$this->defaults,
97-
$this->l10n,
98-
$this->urlGenerator
105+
$this->l10nFactory,
106+
$this->urlGenerator,
107+
$this->config
99108
);
100109
}
101110

@@ -431,8 +440,9 @@ public function testDeleteSingleShare() {
431440
$this->rootFolder,
432441
$this->mailer,
433442
$this->defaults,
434-
$this->l10n,
435-
$this->urlGenerator
443+
$this->l10nFactory,
444+
$this->urlGenerator,
445+
$this->config
436446
])
437447
->setMethods(['getShareById'])
438448
->getMock();
@@ -525,8 +535,9 @@ public function testDeleteGroupShareWithUserGroupShares() {
525535
$this->rootFolder,
526536
$this->mailer,
527537
$this->defaults,
528-
$this->l10n,
529-
$this->urlGenerator
538+
$this->l10nFactory,
539+
$this->urlGenerator,
540+
$this->config
530541
])
531542
->setMethods(['getShareById'])
532543
->getMock();
@@ -2469,8 +2480,9 @@ public function testGetSharesInFolder() {
24692480
$rootFolder,
24702481
$this->mailer,
24712482
$this->defaults,
2472-
$this->l10n,
2473-
$this->urlGenerator
2483+
$this->l10nFactory,
2484+
$this->urlGenerator,
2485+
$this->config
24742486
);
24752487

24762488
$password = md5(time());
@@ -2566,8 +2578,9 @@ public function testGetAccessListNoCurrentAccessRequired() {
25662578
$rootFolder,
25672579
$this->mailer,
25682580
$this->defaults,
2569-
$this->l10n,
2570-
$this->urlGenerator
2581+
$this->l10nFactory,
2582+
$this->urlGenerator,
2583+
$this->config
25712584
);
25722585

25732586
$u1 = $userManager->createUser('testShare1', 'test');
@@ -2661,8 +2674,9 @@ public function testGetAccessListCurrentAccessRequired() {
26612674
$rootFolder,
26622675
$this->mailer,
26632676
$this->defaults,
2664-
$this->l10n,
2665-
$this->urlGenerator
2677+
$this->l10nFactory,
2678+
$this->urlGenerator,
2679+
$this->config
26662680
);
26672681

26682682
$u1 = $userManager->createUser('testShare1', 'test');

0 commit comments

Comments
 (0)