mauricerenck / komments

A Kirby comment plugin
MIT License
45 stars 8 forks source link

Can't comment on translated content (with lang-specific slugs) #56

Closed AllienWorks closed 1 year ago

AllienWorks commented 1 year ago

Some background first:

I've set up the plugin based on your instructions here on GH. Got the panel, templates and all ready. Customized couple options in the config. Created one custom snippet for rendering the reply (really just updated layout).

Adding comments on the default language works without problems.

But when trying to comment on the translated version, I'm getting The page you wrote a comment for could not be found. error and no comment ever gets saved (checked via Panel and my content file).

AllienWorks commented 1 year ago

Relating to https://github.com/mauricerenck/komments/issues/19#issuecomment-900133381, I did check the JSON of the request when sending the form and it points to a valid url (when I copy and paste it to browser, it shows me the correct page). It's using the translated slug.

{
  "komment":"blah blah blah",
  "url":"",
  "email":"(REDACTED)",
  "author":"Martin Allien",
  "author_url":"",
  "wmSource":"http://localhost:8000/cs/clanky/correct-cs-article-name",
  "wmTarget":"http://localhost:8000/cs/clanky/correct-cs-article-name",
  "wmProperty":"komment",
  "quote":"",
  "replyTo":"",
  "replyHandle":"",
  "cts":"10"
}
mauricerenck commented 1 year ago

The plugin uses the url to find the corresponding kirby page, which seems to fail. I will have a look at it.

Do you use different slugs for each language, like?

English: http://localhost:8000/en/clanky/correct-english-article-name Czech: http://localhost:8000/cs/clanky/correct-czech-article-name

Or are the urls the same, only the language code changes, like?

English: http://localhost:8000/en/clanky/correct-article-name Czech: http://localhost:8000/cs/clanky/correct-article-name

AllienWorks commented 1 year ago

Thanks for a super-fast reply!

Yes, I use different slugs for each language. (The slug I supplied in my issue is just illustrative and not a real one.)

It looks like this (different lang code, category slug and page slug - all adjusted to the concrete language):

                         |--> lang-customized
EN: http://localhost:8000/en/articles/correct-english-article-name
CS: http://localhost:8000/cs/clanky/correct-czech-article-name

The CS page titles are using accents, but the slug itself is accent-free. "áčcěňtž" in page name would become "accents" in slug. And one last thing: all other Kirby functionality is working fine (ie. I'd say I haven't done anything nonstandard with the translations).

Edit: Again, both URLs work as expected, if you're on /en/articles/A and switch via Kirby lang switcher to CS, you get correctly redirected to /cs/clanky/A. So they are correctly "paired", Kirby-wise.

mauricerenck commented 1 year ago

Can you tell me how this works? The normal behavior is to have the same page slug, only the language part of the path changes. Are you doing a redirect? Did you configure something in your language settings for this or followed a special guide?

I am taking the url, extract the path and hand it to the kirby page() method to get and access the page. My guess is, that the page methods doesn't find the page by the path of the url, because the folder on the filesystem is probably named after the default language slug.

In order to reproduce the issue it would be good to known how your language setup works.

Also could you please add <?php var_dump(page('/clanky/slug-of-a-page')); ?> anywhere in a template and tell me if it returns null or a page object? That would proof my guess. Of course replace theslug-of-a-page` with an existing one.

AllienWorks commented 1 year ago

So in my articles/ folder, I have a folder for this particular article (20230227_write-beyond-ashes/). Inside, there are two article.md files, one for each language:

📁 articles/
  📁 20230227_write-beyond-ashes/
    📝 article.en.md
    📝 article.cs.md

Then EN version:

Title: Write beyond ashes

And the CS version:

Title: Psát až za popel
----
Slug: psat-az-za-popel

Edit: I'll do the dump later when I'm at my other machine..

AllienWorks commented 1 year ago

The var_dump of the CS version <?php var_dump(page('/clanky/psat-az-za-popel')); ?> below (excuse my poor indentation, just so it's more readable):

object(ArticlePage)#309 (15) {
  ["children"]=> object(Kirby\Cms\Pages)#359 (0) { }
  ["content"]=> object(Kirby\Cms\Content)#358 (11) {
    ["title"]=> string(18) "Psát až za popel"
    ["subheadline"]=> string(192) "Od dob, kdy jsem začal.. <snip>
    ["text"]=> string(5048) "Od dob.. <snip>
    ["cover"]=> string(0) ""
    ["date"]=> string(19) "2023-02-27 22:35:00"
    ["author"]=> string(17) "- <snip>
    ["tags"]=> string(8) "Obsidian"
    ["kommentsenabledonpage"]=> string(4) "true"
    ["kommentsinbox"]=> string(0) ""
    ["uuid"]=> string(16) <snip>
    ["slug"]=> string(16) "psat-az-za-popel"
  }
  ["files"]=> object(Kirby\Cms\Files)#369 (1) {
    [0]=> string(51) "articles/write-beyond-ashes/obsidian-note-graph.png"
  }
  ["id"]=> string(27) "articles/write-beyond-ashes"
  ["mediaUrl"]=> string(61) "http://localhost:8000/media/pages/articles/write-beyond-ashes"
  ["mediaRoot"]=> string(81) "<snip>/kirby/media/pages/articles/write-beyond-ashes"
  ["num"]=> int(20230227)
  ["parent"]=> string(8) "articles"
  ["slug"]=> string(16) "psat-az-za-popel"
  ["template"]=> object(Kirby\Template\Template)#354 (3) {
    ["defaultType":protected]=> string(4) "html"
    ["name":protected]=> string(7) "article"
    ["type":protected]=> string(4) "html"
  }
  ["translations"]=> object(Kirby\Cms\Collection)#349 (2) {
    [0]=> string(2) "cs"
    [1]=> string(2) "en"
  }
  ["uid"]=> string(18) "write-beyond-ashes"
  ["uri"]=> string(23) "clanky/psat-az-za-popel"
  ["url"]=> string(48) "http://localhost:8000/cs/clanky/psat-az-za-popel"
  ["siblings"]=> object(Kirby\Cms\Pages)#296 (12) <snip>
} 
mauricerenck commented 1 year ago

Thanks for the info. I was able to reproduce the error.

I created a translated page and changed the slug for the translation. I stripped the path of the url and gave it to the page() function, which returns NULL When trying to get the page using a filter over all pages, I also get no results, because when filtering via slug, the slug of the default language is used.

I'll try to figure out how to handle this and if the page() behavior is a bug or intended.

AllienWorks commented 1 year ago

Hmm, OK. I had no idea this was a nonstandard approach to translations.

I'm coming from Hugo CMS and it worked there without problems. So naturally I tried if it works in Kirby too and bam - it did. Well, kinda, I see now..

So the correct way of handling Kirby translations is to keep the same slug for all versions?

mauricerenck commented 1 year ago

The option to change the slug per language is there and kirby can handle it, so you should be able to do so and shouldn't change anything on your side.

I guess the problem is, that the approach "from url to page" isn't that straightforward outside of the kirby router. I had problems with that before. I'll figure it out, there must be a solution!

mauricerenck commented 1 year ago

Please try if it works with the new release. It should work for you.

I use another way to handle "url to pages", which works better. This will - at the moment - cause in separate comments for each language. So comments on different languages will be saved only for that specific language. I am not quite sure if this is a feature or not 😅 I'll see if I can make this optional in the future.

AllienWorks commented 1 year ago

Wow, that was really fast!

Please try if it works with the new release. It should work for you.

Yes it does, thank you very much!

(..) separate comments for each language (..)

Honestly, I didn't even think about having shared comments for all languages. But now that you mentioned it, it would be really cool 😆

Anyway I'm 100% happy here and amazed about your service. Really, hats off to you, sir!