I'm making a translation system for my website and I'm having troubles with URL rewriting. Each page is translated to multiple languages and should have different URLs for each language, e.g.:

```
/en/best-restaurants-in-tokyo

/it/migliori-ristoranti-a-tokyo

/fr/meilleurs-restaurants-à-tokyo

```

et cetera.

All those links should open the same post. All the translation related info (including the slug) is stored in a separate table. Changing the content of a post based on the selected language is trivial, but I can't come up with working URL rewriting.

Ideally, I would like to have code like that:

```
$languageShortNames = join('|', $languageShortNames);
add_rewrite_rule('('.$languageShortNames.')/(.*)/?', 'index.php?p=4015&language=$matches[1]', 'top');

```

but instead of `p` being a set number, it should be dynamically determined by some callback. 

One working solution I've stumbled upon is to call `add_rewrite_rule` in a cycle for every existing post for each language. The problem is that my site has thousands of posts, so I think performance will be abysmal. It would really make more sense if I could simply chose the post ID I want to render in a callback by searching the translation table for a post ID with the slug.