Add the localize utility function.
This commit is contained in:
		
							parent
							
								
									c1cbdaf7f9
								
							
						
					
					
						commit
						a07d00e66d
					
				|  | @ -0,0 +1,24 @@ | ||||||
|  | // Get the localized item of a given key inside a data dictionary, using the | ||||||
|  | // provided language. | ||||||
|  | #let localize(data, key, language) = { | ||||||
|  |   // If the key is directly available in the data then it hasn't been localized. | ||||||
|  |   if key in data { | ||||||
|  |     return data.at(key) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // Then check for the key with the wanted language appended to it. | ||||||
|  |   let key_with_language = key + "_" + language | ||||||
|  |   if key_with_language in data { | ||||||
|  |     return data.at(key_with_language) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // If a locale dictionary exists in the data, then run this function again | ||||||
|  |   // using that dictionary for the same key and language. | ||||||
|  |   if "locale" in data { | ||||||
|  |     return localize(data.locale, key, language) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // And if the key nor locale exists in any way then panic, as we won't have | ||||||
|  |   // any text to return. | ||||||
|  |   panic("Data does not contain key in any localized way: " + key, data) | ||||||
|  | } | ||||||
		Reference in New Issue