In formal text its all standard written chinese, and the author simply says they are speaking in XYZ accent, and the reader can imagine it. Just like in english I can tell you two characters are speaking french, and you can imagine it, even though I write all the dialogue out in regular english.
It is possible to write colloquially, sometimes. You are correct that it won't make sense if the reader doesn't know it, so it normally isn't done. Jus' like Imma write inna more slangy way right 'ere dis second, and you finna know what big chungus is only if you already know it, if ya knows what I mean. Or thats all gibberish, and thats why you don't do it. But the equivalent slangy colloquialism is totally possible in most chinese at least. Just like I could write that french dialogue in french and sucks to be you if you don't know french.
Fun fact, some movies especially those from hongkong, would have characters speaking two different types of chinese side by side, no translations or subtitles, cause its a reasonable expectation to know both types of chinese for the viewership.
Alternate fun fact, mainland china takes standard chinese very seriously for viewership enjoyment. Many actresses and actors are straight up dubbed over on their parts for having accents on their standard mandarin, let alone speaking colloquial slang from there hometowns ((you do see local accents when its part of the character itself of course)).