|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { useObjectTranslation } from '@object-ui/react'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Create a safe translation hook with fallback to defaults. |
| 13 | + * Follows the same pattern as useGridTranslation / useListViewTranslation. |
| 14 | + * |
| 15 | + * @param defaults - Fallback English translations keyed by i18n key |
| 16 | + * @param testKey - A key to test if i18n is properly configured |
| 17 | + */ |
| 18 | +export function createSafeTranslationHook( |
| 19 | + defaults: Record<string, string>, |
| 20 | + testKey: string, |
| 21 | +) { |
| 22 | + return function useSafeTranslation() { |
| 23 | + try { |
| 24 | + const result = useObjectTranslation(); |
| 25 | + const testValue = result.t(testKey); |
| 26 | + if (testValue === testKey) { |
| 27 | + return { |
| 28 | + t: (key: string, options?: Record<string, unknown>) => { |
| 29 | + let value = defaults[key] || key; |
| 30 | + if (options) { |
| 31 | + for (const [k, v] of Object.entries(options)) { |
| 32 | + value = value.replace(`{{${k}}}`, String(v)); |
| 33 | + } |
| 34 | + } |
| 35 | + return value; |
| 36 | + }, |
| 37 | + }; |
| 38 | + } |
| 39 | + return { t: result.t }; |
| 40 | + } catch { |
| 41 | + return { |
| 42 | + t: (key: string, options?: Record<string, unknown>) => { |
| 43 | + let value = defaults[key] || key; |
| 44 | + if (options) { |
| 45 | + for (const [k, v] of Object.entries(options)) { |
| 46 | + value = value.replace(`{{${k}}}`, String(v)); |
| 47 | + } |
| 48 | + } |
| 49 | + return value; |
| 50 | + }, |
| 51 | + }; |
| 52 | + } |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Default English translations for detail view components. |
| 58 | + * Used as fallback when no I18nProvider is available. |
| 59 | + */ |
| 60 | +export const DETAIL_DEFAULT_TRANSLATIONS: Record<string, string> = { |
| 61 | + 'detail.back': 'Back', |
| 62 | + 'detail.edit': 'Edit', |
| 63 | + 'detail.editInline': 'Edit inline', |
| 64 | + 'detail.save': 'Save', |
| 65 | + 'detail.saveChanges': 'Save changes', |
| 66 | + 'detail.editFieldsInline': 'Edit fields inline', |
| 67 | + 'detail.share': 'Share', |
| 68 | + 'detail.duplicate': 'Duplicate', |
| 69 | + 'detail.export': 'Export', |
| 70 | + 'detail.viewHistory': 'View history', |
| 71 | + 'detail.delete': 'Delete', |
| 72 | + 'detail.moreActions': 'More actions', |
| 73 | + 'detail.addToFavorites': 'Add to favorites', |
| 74 | + 'detail.removeFromFavorites': 'Remove from favorites', |
| 75 | + 'detail.previousRecord': 'Previous record', |
| 76 | + 'detail.nextRecord': 'Next record', |
| 77 | + 'detail.recordOf': '{{current}} of {{total}}', |
| 78 | + 'detail.recordNotFound': 'Record not found', |
| 79 | + 'detail.recordNotFoundDescription': 'The record you are looking for does not exist or may have been deleted.', |
| 80 | + 'detail.goBack': 'Go back', |
| 81 | + 'detail.details': 'Details', |
| 82 | + 'detail.related': 'Related', |
| 83 | + 'detail.relatedRecords': '{{count}} records', |
| 84 | + 'detail.relatedRecordOne': '{{count}} record', |
| 85 | + 'detail.noRelatedRecords': 'No related records found', |
| 86 | + 'detail.loading': 'Loading...', |
| 87 | + 'detail.copyToClipboard': 'Copy to clipboard', |
| 88 | + 'detail.copied': 'Copied!', |
| 89 | + 'detail.deleteConfirmation': 'Are you sure you want to delete this record?', |
| 90 | + 'detail.editRecord': 'Edit record', |
| 91 | + 'detail.viewAll': 'View All', |
| 92 | + 'detail.new': 'New', |
| 93 | + 'detail.emptyValue': '—', |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Translation hook for detail view components. |
| 98 | + * Falls back to DETAIL_DEFAULT_TRANSLATIONS when no I18nProvider is available. |
| 99 | + */ |
| 100 | +export const useDetailTranslation = createSafeTranslationHook( |
| 101 | + DETAIL_DEFAULT_TRANSLATIONS, |
| 102 | + 'detail.back', |
| 103 | +); |
0 commit comments