You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
			
		
		
		
		
			
		
			
				
					
					
						
							37 lines
						
					
					
						
							1.1 KiB
						
					
					
				
			
		
		
	
	
							37 lines
						
					
					
						
							1.1 KiB
						
					
					
				import { set } from 'lodash-es' | 
						|
import type { LocaleType } from '@/types/config' | 
						|
 | 
						|
export const loadLocalePool: LocaleType[] = [] | 
						|
 | 
						|
export function setHtmlPageLang(locale: LocaleType) { | 
						|
  document.querySelector('html')?.setAttribute('lang', locale) | 
						|
} | 
						|
 | 
						|
export function setLoadLocalePool(cb: (loadLocalePool: LocaleType[]) => void) { | 
						|
  cb(loadLocalePool) | 
						|
} | 
						|
 | 
						|
export function genMessage(langs: Record<string, Record<string, any>>, prefix = 'lang') { | 
						|
  const obj: Recordable = {} | 
						|
 | 
						|
  Object.keys(langs).forEach((key) => { | 
						|
    const langFileModule = langs[key].default | 
						|
    let fileName = key.replace(`./${prefix}/`, '').replace(/^\.\//, '') | 
						|
    const lastIndex = fileName.lastIndexOf('.') | 
						|
    fileName = fileName.substring(0, lastIndex) | 
						|
    const keyList = fileName.split('/') | 
						|
    const moduleName = keyList.shift() | 
						|
    const objKey = keyList.join('.') | 
						|
 | 
						|
    if (moduleName) { | 
						|
      if (objKey) { | 
						|
        set(obj, moduleName, obj[moduleName] || {}) | 
						|
        set(obj[moduleName], objKey, langFileModule) | 
						|
      } | 
						|
      else { | 
						|
        set(obj, moduleName, langFileModule || {}) | 
						|
      } | 
						|
    } | 
						|
  }) | 
						|
  return obj | 
						|
}
 | 
						|
 |