Skip to content

Commit

Permalink
feat: allow to translate resources in resource options
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Jul 25, 2023
1 parent d121121 commit 1c2d27f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
3 changes: 0 additions & 3 deletions src/adminjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ class AdminJS {

this.resolveBabelConfigPath()

// To be removed when Login page will be renedered on client side
this.locale = defaultLocale

const { databases, resources } = this.options

this.componentLoader = options.componentLoader ?? new ComponentLoader()
Expand Down
7 changes: 7 additions & 0 deletions src/backend/decorators/resource/resource-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CurrentAdmin } from '../../../current-admin.interface.js'
import BaseResource from '../../adapters/resource/base-resource.js'
import ViewHelpers from '../../utils/view-helpers/view-helpers.js'
import { SearchActionResponse } from '../../actions/search/search-action.js'
import { LocaleTranslationsBlock } from '../../../index.js'

/**
* @alias HrefContext
Expand Down Expand Up @@ -129,4 +130,10 @@ export interface ResourceOptions {
} | {
[key: string]: Partial<Action<ActionResponse>>;
};
/**
* Resource-specific translations
*/
translations?: {
[language: string]: LocaleTranslationsBlock;
}
}
2 changes: 1 addition & 1 deletion src/backend/utils/build-feature/build-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function mergeActionHooks<T>(
return hooks.length ? { [key]: hooks } : {}
}

const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation', 'titleProperty'] as const
const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation', 'titleProperty', 'translations'] as const
const listOptions = [
'listProperties', 'showProperties', 'editProperties', 'filterProperties',
] as const
Expand Down
36 changes: 35 additions & 1 deletion src/backend/utils/options-parser/options-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,41 @@ export const getLocales = async (admin: AdminJS, currentAdmin?: CurrentAdmin): P
const { locale = {} } = admin.options || {}
const computed = typeof locale === 'function' ? await locale(currentAdmin) : locale

return flat.unflatten(merge({}, flat.flatten(defaultLocale), flat.flatten(computed)))
let baseLocale: Locale = merge(
{} as Partial<Locale>,
flat.flatten(defaultLocale) as Locale,
flat.flatten(computed) as Locale,
)

if (!baseLocale.translations) {
baseLocale.translations = {}
}

// Merging translations defined in resource options
admin.resources.forEach((baseResource) => {
const decorated = baseResource._decorated ?? baseResource.decorate()

const { translations: resourceTranslations } = decorated.options

if (resourceTranslations) {
// Assure that translations object structure is consistent so we can use lodash#merge
const resourceLocale: Omit<Locale, 'language'> = {
translations: {},
}

Object.keys(resourceTranslations).forEach((language) => {
resourceLocale.translations![language] = {
resources: {
[decorated.id()]: resourceTranslations[language],
},
}
})

baseLocale = merge(baseLocale, resourceLocale)
}
})

return flat.unflatten(baseLocale)
}

export const getTheme = async (
Expand Down

0 comments on commit 1c2d27f

Please sign in to comment.