Bluesky app fork with some witchin' additions 馃挮
at main 70 lines 2.3 kB view raw
1import {type Messages} from '@lingui/core' 2 3import * as persisted from '#/state/persisted' 4 5// Helper to apply the replacement to a single string 6function replaceInString(text: string): string { 7 const {postName, postsName, enabled} = persisted.get('postReplacement') 8 if (!enabled) return text 9 10 const singular = postName?.length ? postName : 'skeet' 11 const plural = postsName?.length ? postsName : 'skeets' 12 13 // Capitalize first letter for proper noun replacements 14 const singularCapitalized = singular[0].toUpperCase() + singular.slice(1) 15 const pluralCapitalized = plural[0].toUpperCase() + plural.slice(1) 16 17 return text 18 .replaceAll('Posts', pluralCapitalized) 19 .replaceAll('posts', plural) 20 .replaceAll('Post', singularCapitalized) 21 .replaceAll('post', singular) 22} 23 24// Recursive helper to traverse and replace strings in nested structures 25function traverseAndReplace(value: any): any { 26 if (typeof value === 'string') { 27 return replaceInString(value) 28 } 29 if (Array.isArray(value)) { 30 return value.map(item => traverseAndReplace(item)) 31 } 32 if (typeof value === 'object' && value !== null) { 33 const newObject: Record<string, any> = {} 34 for (const key in value) { 35 if (Object.prototype.hasOwnProperty.call(value, key)) { 36 newObject[key] = traverseAndReplace(value[key]) 37 } 38 } 39 return newObject 40 } 41 return value 42} 43 44/** 45 * Applies "Post" to "Skeet" replacements (case-insensitive) to messages 46 * for English locales. 47 * @param messages The raw messages object loaded from Lingui. 48 * @param locale The current locale string. 49 * @returns The messages object with replacements applied if the locale is English, 50 * otherwise the original messages object. 51 */ 52export function applyPostReplacements( 53 messages: Messages, 54 locale: string, 55): Messages { 56 const {enabled} = persisted.get('postReplacement') 57 console.log('replacements enabled:', enabled) 58 if (!enabled || !locale.startsWith('en')) { 59 return messages 60 } 61 62 // Traverse the entire messages catalog and apply replacements 63 const transformedCatalog: Messages = {} 64 for (const key in messages) { 65 if (Object.prototype.hasOwnProperty.call(messages, key)) { 66 transformedCatalog[key] = traverseAndReplace(messages[key]) 67 } 68 } 69 return transformedCatalog 70}