I would expect the script displays “P1 title”, but I keep getting “P1” instead. Does anyone have an explanation for this behaviour? (when language=fr is set in the URL, I get “P1 titre” as expected)
Thank you surli, I was suspecting this as well but whatever language I set in the URL so far except language=fr, I keep getting “P1” as title output, never “P1 title”…
I think what you are missing is that the locale of the default version of a document is always “” (Locale.ROOT). Then the defaultLocale field contains the actual locale you want to associated to the document content (in your case en).
It’s not. The locale is passed as second parameter because the document reference and the locale are separated in XWikiDocument for historical reason.
Thank you for this explanation, tmortagne. There’s still something that remains unclear to me: what is the canonical way to get a document in a given locale in all cases? Should the developer expect that the target document should exist with a default language plus in all available wiki languages? Should the default be empty in that case (for avoiding duplicated content)? Or should the developer test if a given locale matches the wiki default locale and fall back to the document with root locale in that specific case?
It’s better since you let XWiki deal with the translation internally, without trying to build manually a reference that might be wrong in the end. Now I agree that it could be useful to have a proper API to get directly the right translated document given a reference and a locale.
The code you suggest work indeed but at the cost of two documents loading instead of one, unless I’m mistaken? So we agree that an API would be useful, good, but what about the existing API method ModelScriptService#createDocumentReference(DocumentReference,Locale)? How is that API more manual than the one you suggest and why should a developer expect two different results?
Just trying to possibly contribute to the improvement of the developer experience, I understand it’s complex and that I may misunderstand the underpinnings…
We already have that, you just have to pass a reference with the locale in it. But again don’t mix the document actual locale and the locale of the content of the default document. The reason to go trough the default version is when you don’t know the actual locale, if you know it then you can get it directly.
Indeed, but what’s interesting in @slauriere example is that it really shows that the internal mechanism of managing locale is far from being obvious. Personally I’d say that the fact XWiki cannot find the document in the first snippet should be considered as a bug: the wiki is set as using english as default document language, the page is edited as such, so anyone would expect to get it when specifying “en” for locale.
It’s not that simple, this is actually a pretty low level API right now and it’s perfectly possible technically to have both a default document with locale="" and defaultLocale=en and another instance with locale=en right now. It’s also important to remember that the XWikiDocument you get for the translations is not complete (it does not contain any attachment/object for example), that’s mainly why the default version of the document is at the center of the current API. So IMO it’s not as simple as adding a new getDocument with a boolean to fallback. A getDocumentContent which does that would be a lot easier to fit (we have that already in the DocumentAccessBridge).
I have the feeling what you are talking about require a new API completly which hide internal better that XWikiDocument or even Document does (which is something we need since a long time).