If you follow the letter of the law, no it is not legal. To do a conversion you must use an engine that is the same year or newer than the engine it is replacing. Also it must have all emissions equipment that said engine had for the vehicle the donor engine came from. A good example would be if you swapped in a 6.7L POWERSTROKE, you would have to swap in all of teh DPF, cat, and DEF injection system to be legal. On the other hand how many people do all of this and never get caught. if you live where there are emissions tests done, then I would at least say you need an electronically controlled engine with an OBD2 data plug for it to be checked as most all emissions test first start with connecting an OBD2 scanner and checking for codes. If you have no emissions testing in your area or visual checks, then do what you want and just hope you don't move somewhere where they do emissions testing(or get caught somewhere at a roadside inspection station like California has).
If I had to guess I would say probably 90% of the diesel swaps out there are not legal by the letter of the law, but on the other hand the enforcement isn't there to check them.