Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions lib/Conversion/ConversionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Richdocuments\Conversion;

use OCA\Richdocuments\Service\LanguageService;
use OCA\Richdocuments\Service\RemoteService;
use OCA\Richdocuments\Service\SecureViewService;
use OCP\Files\Conversion\ConversionMimeProvider;
Expand Down Expand Up @@ -56,6 +57,7 @@ public function __construct(
private LoggerInterface $logger,
IFactory $l10nFactory,
private SecureViewService $secureViewService,
private LanguageService $languageService,
) {
$this->l10n = $l10nFactory->get('richdocuments');
}
Expand Down Expand Up @@ -168,17 +170,10 @@ public function convertFile(File $file, string $targetMimeType): mixed {
return $this->remoteService->convertFileTo(
$file,
$targetFileExtension,
conversionOptions: ['lang' => $this->getConversionLanguage()]
conversionOptions: ['lang' => $this->languageService->getBCP47LanguageTag()]
);
}

private function getConversionLanguage(): string {
$locale = $this->l10n->getLocaleCode();
$language = $locale !== '' ? $locale : $this->l10n->getLanguageCode();

return str_replace('_', '-', $language);
}

private function getMimeProvidersFor(array $inputMimeTypes, string $outputMimeType): array {
$outputMimeInfo = $this->getMimeInfoFor($outputMimeType);
if ($outputMimeInfo === null) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
private TemplateManager $templateManager,
private CapabilitiesService $capabilitiesService,
private IConfig $config,
private LanguageService $languageService,
private ?string $userId,
) {
}
Expand All @@ -42,6 +43,7 @@ public function provideCapabilities(): void {
$this->initialState->provideInitialState('hasNextcloudBranding', $this->capabilitiesService->hasNextcloudBranding());
$this->initialState->provideInitialState('instanceId', $this->config->getSystemValue('instanceid'));
$this->initialState->provideInitialState('wopi_callback_url', $this->appConfig->getNextcloudUrl());
$this->initialState->provideInitialState('bcp47Language', $this->languageService->getBCP47LanguageTag());
$this->provideOptions();

$this->hasProvidedCapabilities = true;
Expand Down
58 changes: 58 additions & 0 deletions lib/Service/LanguageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Richdocuments\Service;

use OCA\Richdocuments\AppInfo\Application;
use OCP\L10N\IFactory;

class LanguageService {
private const LOCALE_OVERRIDES = [
'de' => [
'de_CH' => 'de-CH',
'gsw' => 'de-CH',
'gsw_CH' => 'de-CH',
],
'fr' => [
'fr_CH' => 'fr-CH',
],
'it' => [
'it_CH' => 'it-CH',
],
];

public function __construct(
private IFactory $l10nFactory,
) {
}

/**
* Converts the current user's Nextcloud language/locale settings into the
* BCP 47 language tag Collabora Online expects.
*/
public function getBCP47LanguageTag(): string {
$l10n = $this->l10nFactory->get(Application::APPNAME);

// getLanguageCode()/getLocaleCode() mirror @nextcloud/l10n's getLanguage()/getLocale()
$language = str_replace('_', '-', $l10n->getLanguageCode());
$locale = $l10n->getLocaleCode();

$language = match ($language) {
'de-DE' => 'de', // German formal should just be treated as 'de'
'es-419' => 'es-MX', // not a valid locale string in COOL
default => $language,
};

if ($language === 'en-GB' && $locale === 'en_AU') {
$language = 'en-AU';
}

return self::LOCALE_OVERRIDES[$language][$locale] ?? $language;
}
}
4 changes: 2 additions & 2 deletions src/components/CoolFrame.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<script>

import { generateCSSVarTokens, getCollaboraTheme, getUITheme } from '../helpers/coolParameters.js'
import { languageToBCP47 } from '../helpers/index.js'
import { loadState } from '@nextcloud/initial-state'
import PostMessageService from '../services/postMessage.tsx'

export default {
Expand Down Expand Up @@ -74,7 +74,7 @@ export default {
window.addEventListener('message', this.handlePostMessage)

if (this.iframeUrl.length > 0) {
this.formAction = this.iframeUrl + '?lang=' + languageToBCP47()
this.formAction = this.iframeUrl + '?lang=' + loadState('richdocuments', 'bcp47Language', '')
this.isIframeLoaded = true
} else {
return
Expand Down
47 changes: 0 additions & 47 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { getLanguage, getLocale } from '@nextcloud/l10n'

const languageToBCP47 = () => {
let language = getLanguage().replace(/_/g, '-')
const locale = getLocale()

// German formal should just be treated as 'de'
if (language === 'de-DE') {
language = 'de'
}

// es-419 should be mapped as this is not considered a valid locale string in COOL
if (language === 'es-419') {
language = 'es-MX'
}

// Australia
if (language === 'en-GB' && locale === 'en_AU') {
language = 'en-AU'
}

// special case where setting the bc47 region depending on the locale setting makes sense
const whitelist = {
de: {
de_CH: 'de-CH',
gsw: 'de-CH',
gsw_CH: 'de-CH',
},
fr: {
fr_CH: 'fr-CH',
},
it: {
it_CH: 'it-CH',
},
}
const matchingWhitelist = whitelist[language]
if (typeof matchingWhitelist !== 'undefined' && typeof matchingWhitelist[locale] !== 'undefined') {
return matchingWhitelist[locale]
}

// Collabora Online expects BCP47 language tag syntax.
// When the Nextcloud language consists of two parts, we send both,
// as the region is then provided by the language setting.
return language
}

const getNextcloudVersion = () => {
return parseInt(OC.config.version.split('.')[0])
}
Expand All @@ -68,7 +22,6 @@ const getRandomId = (length = 5) => {
}

export {
languageToBCP47,
getNextcloudVersion,
splitPath,
getRandomId,
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import { getRootUrl, generateUrl } from '@nextcloud/router'
import { getSharingToken } from '@nextcloud/sharing/public'
import { languageToBCP47 } from './index.js'
import Config from './../services/config.tsx'

const getSearchParam = (name) => {
Expand Down Expand Up @@ -39,7 +38,7 @@ const getWopiUrl = ({ fileId, readOnly, closeButton, revisionHistory, target = u
// https://<loolwsd-server>:9980/hosting/discovery
return Config.get('urlsrc')
+ 'WOPISrc=' + encodeURIComponent(getWopiSrc(fileId))
+ '&lang=' + languageToBCP47()
+ '&lang=' + Config.get('bcp47Language')
+ (closeButton ? '&closebutton=1' : '')
+ (revisionHistory ? '&revisionhistory=1' : '')
+ (readOnly ? '&permission=readonly' : '')
Expand Down
1 change: 1 addition & 0 deletions src/services/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ConfigService {
constructor() {
this.values = {
wopi_callback_url: loadState('richdocuments', 'wopi_callback_url', ''),
bcp47Language: loadState('richdocuments', 'bcp47Language', ''),
...loadState('richdocuments', 'document', {}),
}
}
Expand Down
12 changes: 8 additions & 4 deletions tests/lib/Conversion/ConversionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Tests\Richdocuments\Conversion;

use OCA\Richdocuments\Conversion\ConversionProvider;
use OCA\Richdocuments\Service\LanguageService;
use OCA\Richdocuments\Service\RemoteOptionsService;
use OCA\Richdocuments\Service\RemoteService;
use OCA\Richdocuments\Service\SecureViewService;
Expand All @@ -26,6 +27,7 @@ class ConversionProviderTest extends TestCase {
private IFactory&MockObject $l10nFactory;
private IL10N&MockObject $l10n;
private SecureViewService&MockObject $secureViewService;
private LanguageService&MockObject $languageService;
private ConversionProvider $provider;

protected function setUp(): void {
Expand All @@ -36,6 +38,7 @@ protected function setUp(): void {
$this->l10nFactory = $this->createMock(IFactory::class);
$this->l10n = $this->createMock(IL10N::class);
$this->secureViewService = $this->createMock(SecureViewService::class);
$this->languageService = $this->createMock(LanguageService::class);

$this->l10n->method('t')->willReturnCallback(static fn (string $text): string => $text);
$this->l10nFactory->method('get')
Expand All @@ -47,20 +50,21 @@ protected function setUp(): void {
$this->logger,
$this->l10nFactory,
$this->secureViewService,
$this->languageService,
);
}

public function testConvertFilePassesCurrentLocaleToCollabora(): void {
$file = $this->createMock(File::class);

$this->l10n->expects($this->once())
->method('getLocaleCode')
->willReturn('de_DE');
$this->languageService->expects($this->once())
->method('getBCP47LanguageTag')
->willReturn('de');
$this->secureViewService->method('isEnabled')
->willReturn(false);
$this->remoteService->expects($this->once())
->method('convertFileTo')
->with($file, 'pdf', RemoteOptionsService::REMOTE_TIMEOUT_DEFAULT, ['lang' => 'de-DE'])
->with($file, 'pdf', RemoteOptionsService::REMOTE_TIMEOUT_DEFAULT, ['lang' => 'de'])
->willReturn('pdf-content');

$result = $this->provider->convertFile($file, 'application/pdf');
Expand Down
61 changes: 61 additions & 0 deletions tests/lib/Service/LanguageServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Tests\Richdocuments\Service;

use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\Service\LanguageService;
use OCP\IL10N;
use OCP\L10N\IFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class LanguageServiceTest extends TestCase {
private IFactory&MockObject $l10nFactory;
private IL10N&MockObject $l10n;
private LanguageService $service;

protected function setUp(): void {
parent::setUp();

$this->l10nFactory = $this->createMock(IFactory::class);
$this->l10n = $this->createMock(IL10N::class);
$this->l10nFactory->method('get')
->with(Application::APPNAME)
->willReturn($this->l10n);

$this->service = new LanguageService($this->l10nFactory);
}

public static function languageProvider(): array {
return [
'plain pass-through' => ['en', 'en_US', 'en'],
'German formal is treated as plain German' => ['de_DE', 'de_DE', 'de'],
'es-419 is mapped to es-MX for COOL' => ['es_419', 'es_419', 'es-MX'],
'Australian English' => ['en_GB', 'en_AU', 'en-AU'],
'British English without Australian locale stays en-GB' => ['en_GB', 'en_GB', 'en-GB'],
'Swiss German locale' => ['de', 'de_CH', 'de-CH'],
'Swiss German (gsw locale)' => ['de', 'gsw', 'de-CH'],
'Swiss German (gsw_CH locale)' => ['de', 'gsw_CH', 'de-CH'],
'Swiss French locale' => ['fr', 'fr_CH', 'fr-CH'],
'Swiss Italian locale' => ['it', 'it_CH', 'it-CH'],
'German without Swiss locale stays de' => ['de', 'de_DE', 'de'],
];
}

/**
* @dataProvider languageProvider
*/
public function testGetBCP47LanguageTag(string $languageCode, string $localeCode, string $expected): void {
$this->l10n->method('getLanguageCode')->willReturn($languageCode);
$this->l10n->method('getLocaleCode')->willReturn($localeCode);

$this->assertSame($expected, $this->service->getBCP47LanguageTag());
}
}
Loading