dennisfokker / Factorio-Json-Calculator-Exporter-mod

Simple Factorio exporter mod which I use for my calculator.
2 stars 0 forks source link

Feature Request: Extract Translated Recipe Names #4

Open brodkin opened 2 years ago

brodkin commented 2 years ago

I'm not a modder, but I do consume JSON output from mods like yours. Any chance that you'd consider extracting the translated names of the items, and not just the hyphenated names? Many mods change the names of the recipes, resulting in the name of the item shown in-game to mismatch the hyphenated name. It makes this data much harder to consume.

If not, totally respect that, but I'd love any thoughts you have on how it might be accomplished.

FWIW I'm consuming JSON data here... https://bitbucket.org/gkrb/jiratorio/

dennisfokker commented 2 years ago

It is something I'm planning to look into. Giving it a quick look though I'm not sure I can properly export it through a mod. Basically, everything contains an optional localisation identifier (much like an item's name) that I can expose (would just have to handle the "optional" part somehow). However, converting that into "human readable" localised text isn't quite as straightforward from a mod's perspective.

As far as I can tell, the only way I can programmatically export the text for a specific language is through a player object. Basically, this would mean starting up the game completely, setting the game's locale as desired, and then starting a (temporary) map where I can push all known localisation strings through to put it in a different output file (since we're in a different stage in the game this cannot be joined with the other data).

Either that, or you'd have to utilise the localisation identifier directly and just figure out the correct translation based on all of the localisation files yourself.

brodkin commented 2 years ago

I'm not even sure that you could use the player because you might not be able to grab the item's name if it has not been researched. How does the localisation identifier compare to the item name?

dennisfokker commented 2 years ago

The research bit won't be a limitation. The player object from a LUA (i.e. modding) point of view will have full control. Even if research is a problem, you can always just run some functions to mark everything as researched (just like you can do with the console).

Regarding the localisation identifier, it's effectively the same as the name with some additional stuff, so you're better off with the name property.

Did some more digging and extracting this indeed won't be pretty. The only way to extract localisation is basically what I mentioned previously:

  1. Export the localisation strings (which are actually objects and not just strings) and do it all manually, including the tasks of dereferencing to the localisation files, parameter injection, and the error handling that Factorio does. --- or ---
  2. Open up a map and call request_translation on a player object for every localisation identifier (how you get that information in will be a different story again), which is a latent call that'll return the result on a different dispatcher...

For now I'm looking if it's possible to have this mod handle this semi-automatically with some input. An possible description of the process would be:

  1. You extract the currently available JSON object as you would normally.
  2. Extract all the localisation identifiers and put those in a new file in the mod's folder.
  3. Launch an instance of Factorio and load in a level with a player (hope you can do this through console commands).
  4. The mod will read the previously created file once the player is available and eventually outputs the resulting translations in a different file to read. The program that started this process would probably be notified of its completion either by the closure of the Factorio process (if that's possible from within the mod), or through the existence of a simple "lock" or confirmation file that you can poll for.

Like I said. It won't be pretty, but other than manually implementing localisation yourself I currently don't see another method.

brodkin commented 2 years ago

You are correct. That is both well-researched and not at all pretty.

dennisfokker commented 2 years ago

I did manage to find this mod, which afaict exports everything in a special JSON format as well (including icons through an atlas). So you might be able to use that. It's a bit more manual in its usage, though (haven't yet checked yet if there's a method to automate joining a level through console parameters).

In case you're interested in the technical details for how the above mod handles translation: it uses Flib for extracting the translation, which roughly uses the method I outlined, + a whole lot of smart stuff which it uses to return all the latent calls' result in a single object once it's done.