z_mirror/flib
git clone https://git.raiguard.me/z_mirror/flib
6ae3ed1
trunk
1local flib_prototypes =require ( "__flib__.prototypes" ) 2 3--- Provides utilities for deducing the localised names of various prototypes in the prototype stage. 4--- Inspired by Rusty's Locale Utilities: https://github.com/theRustyKnife/rusty-locale 5--- @class flib_locale 6local flib_locale ={} 7 8--- Returns the localised name of the given prototype. 9--- @overload fun(prototype: data.PrototypeBase): data.LocalisedString 10--- @overload fun(base_type: string, name: string): data.LocalisedString 11function flib_locale . of ( prototype , name ) 12-- In this case, `prototype` is actually `base_type`. 13if type ( prototype ) == "string" then 14return flib_locale . of ( flib_prototypes . get ( prototype , name ) --[[@as data.PrototypeBase]] ) 15end 16if prototype . type == "recipe" then 17return flib_locale . of_recipe ( prototype --[[@as data.RecipePrototype]] ) 18elseif defines . prototypes . item [ prototype . type ] then 19return flib_locale . of_item ( prototype --[[@as data.ItemPrototype]] ) 20else 21return prototype . localised_name or { flib_prototypes . get_base_type ( prototype . type ) .. "-name." .. prototype . name } 22end 23end 24 25--- Returns the localised name of the given item. 26--- @param item data.ItemPrototype 27--- @return data.LocalisedString 28function flib_locale . of_item ( item ) 29if not defines . prototypes . item [ item . type ] then 30error ( "Given prototype is not an item: " .. serpent . block ( item )) 31end 32if item . localised_name then 33return item . localised_name 34end 35local type_name ="item" 36--- @type data.PrototypeBase? 37local prototype 38if item . place_result then 39type_name ="entity" 40prototype =flib_prototypes . get ( "entity" , item . place_result ) --[[@as data.PrototypeBase]] 41elseif item . place_as_equipment_result then 42type_name ="equipment" 43prototype =flib_prototypes . get ( "equipment" , item . place_as_equipment_result ) --[[@as data.PrototypeBase]] 44elseif item . place_as_tile then 45local tile_prototype =data . raw . tile [ item . place_as_tile . result ] 46-- Tiles with variations don't have a localised name 47if tile_prototype and tile_prototype . localised_name then 48prototype =tile_prototype 49type_name ="tile" 50end 51end 52return prototype and prototype . localised_name or { type_name .. "-name." .. item . name } 53end 54 55--- Returns the localised name of the given recipe. 56--- @param recipe data.RecipePrototype 57--- @return data.LocalisedString 58function flib_locale . of_recipe ( recipe ) 59if recipe . type ~= "recipe" then 60error ( "Given prototype is not an recipe: " .. serpent . block ( recipe )) 61end 62if recipe . localised_name then 63return recipe . localised_name 64end 65local main_product =recipe . main_product -- LuaLS gets confused if we don't assign to a local. 66if main_product == "" then 67return { "recipe-name." .. recipe . name } 68elseif main_product and main_product == recipe . name then 69return flib_locale . of_item ( flib_prototypes . get ( "item" , main_product )) 70end 71local results =recipe . results 72if results and # results == 1 and results [ 1 ]. name == recipe . name then 73return flib_locale . of_item ( flib_prototypes . get ( "item" , results [ 1 ]. name )) 74end 75return { "recipe-name." .. recipe . name } 76end 77 78return flib_locale