Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init custom translations #8790

Merged
merged 11 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
7 changes: 7 additions & 0 deletions changelog/unreleased/enhancement-custom-translations
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Custom translations

We have added the possibility to include own translations to override existing translations.
To inject custom translations add the following property to your `config.json`, `"customTranslation": [{ "url": "https://localhost:9200/translations.json" }]`.

https://github.com/owncloud/web/pull/8790
https://github.com/owncloud/web/issues/8791
2 changes: 2 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Please refer to the [build documentation for Web]({{< ref "./building.md" >}}).

Depending on the backend you are using, there are sample config files provided in the [config folder](https://github.com/owncloud/web/tree/master/config) of the ownCloud Web git repository. See below for available backends. Also, find some of the configuration details below.

- `customTranslations` You can specify one or multiple files to overwrite existing translations. For example set this option to `[{url: "https://localhost:9200/customTranslations.json"}]`.

#### Options
- `options.homeFolder` You can specify a folder that is used when the user navigates `home`. Navigating home gets triggered by clicking on the `All files`
menu item. The user will not be jailed in that directory. It simply serves as a default location. You can either provide a static location, or you can use
Expand Down
10 changes: 10 additions & 0 deletions packages/web-pkg/src/configuration/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CustomTranslation,
OAuth2Configuration,
OIDCConfiguration,
OptionsConfiguration,
Expand All @@ -14,6 +15,7 @@ export interface RawConfig {
auth?: any
openIdConnect?: any
options?: OptionsConfiguration
customTranslations?: Array<CustomTranslation>
}

export class ConfigurationManager {
Expand All @@ -35,6 +37,7 @@ export class ConfigurationManager {
? (rawConfig.openIdConnect as OIDCConfiguration)
: null
this.logoutUrl = rawConfig.options?.logoutUrl
this.customTranslations = rawConfig.customTranslations
}

set logoutUrl(url: string) {
Expand All @@ -56,6 +59,13 @@ export class ConfigurationManager {
return this.runtimeConfiguration.serverUrl
}

set customTranslations(customTranslations: Array<CustomTranslation>) {
this.runtimeConfiguration.customTranslations = customTranslations || []
}
get customTranslations(): Array<CustomTranslation> {
return this.runtimeConfiguration.customTranslations
}

get isOAuth2(): boolean {
return !isNil(this.oAuth2Configuration)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/web-pkg/src/configuration/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export interface CustomTranslation {
url: string
}
export interface RuntimeConfiguration {
serverUrl: string
customTranslations?: Array<CustomTranslation>
}

export interface RoutingOptionsConfiguration {
Expand Down
29 changes: 29 additions & 0 deletions packages/web-runtime/src/helpers/customTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ConfigurationManager } from 'web-pkg'
import { v4 as uuidV4 } from 'uuid'
import merge from 'lodash-es/merge'

export const loadCustomTranslations = async ({
configurationManager
}: {
configurationManager: ConfigurationManager
}): Promise<unknown> => {
const customTranslations = {}
for (const customTranslation of configurationManager.customTranslations) {
const customTranslationResponse = await fetch(customTranslation.url, {
headers: { 'X-Request-ID': uuidV4() }
})
if (customTranslationResponse.status !== 200) {
console.error(
`translation file ${customTranslation} could not be loaded. HTTP status-code ${customTranslationResponse.status}`
)
continue
}
try {
const customTranslationJSON = await customTranslationResponse.json()
merge(customTranslations, customTranslationJSON)
} catch (e) {
console.error(`translation file ${customTranslation} could not be parsed. ${e}`)
}
}
return customTranslations
}
10 changes: 9 additions & 1 deletion packages/web-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { configurationManager } from 'web-pkg/src/configuration'
import { createHead } from '@vueuse/head'
import { abilitiesPlugin } from '@casl/vue'
import { createMongoAbility } from '@casl/ability'
import merge from 'lodash-es/merge'
import { v4 as uuidV4 } from 'uuid'

import {
announceConfiguration,
Expand Down Expand Up @@ -32,6 +34,7 @@ import {
isPublicSpaceResource,
Resource
} from 'web-client/src/helpers'
import { loadCustomTranslations } from 'web-runtime/src/helpers/customTranslations'
import { WebDAV } from 'web-client/src/webdav'
import { DavProperty } from 'web-client/src/webdav/constants'
import { createApp } from 'vue'
Expand Down Expand Up @@ -60,8 +63,13 @@ export const bootstrapApp = async (configurationPath: string): Promise<void> =>
})
const themePromise = announceTheme({ store, app, designSystem, runtimeConfiguration })
await Promise.all([applicationsPromise, themePromise])
const customTranslations = await loadCustomTranslations({ configurationManager })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last nitpick: tiny performance improvement possible by splitting this into promise creation and awaiting the resolved promise together with other promises. This is a good thing because the promise execution starts immediately with creation, so this enables parallel processing instead of awaiting promises sequentially.

In other words:
Please split this line into:

const customTranslationsPromise = loadCustomTranslations({ configurationManager })

for promise creation and add that to the Promise.all. You can access the resolved custom translations like this:

const [customTranslations] = await Promise.all([customTranslationsPromise, applicationsPromise, themePromise])


announceTranslations({ app, availableLanguages: supportedLanguages, translations })
announceTranslations({
app,
availableLanguages: supportedLanguages,
translations: merge(translations, customTranslations)
})
announceClientService({ app, runtimeConfiguration, configurationManager, store })
announceUppyService({ app })
announceLoadingService({ app })
Expand Down