maximeschoeni / sublanguage

Multilanguage plugin for wordpress
42 stars 13 forks source link

Posts in native language #22

Open misiman opened 7 years ago

misiman commented 7 years ago

Hello again Maxime,

I wish to use Sublanguage for my multilingual solution, but have some questions on whether this release or v1.5.3 is a better starting point for my requirements.

I need to allow users to post in their native language and have these stored in wp_posts as the "primary/base" post. Thus the base site language is not actually any particular language because any translations would be of the "primary/base" post language and not the site base language setting.

For example, if two users are posting, one in English (EN) and one in German (DE), whichever language creates the post should be the base for that post:

Scenario 1 - Base language EN - User1 posts an entry to wp_posts in English, and that post may (or may not) be translated into German by User2 which would be saved in wp_postmeta as a DE sublanguage.

Obviously the reverse of which is:

Scenario 2 - Base language DE - User2 posts an entry to wp_posts in German, and that post may (or may not) be translated into English by User1 which would be saved in wp_postmeta as an EN sublanguage.

Crucially, the base language in Scenario 1 is English whereas the base language in Scenario 2 is German where Sublanguage automatically adjusts the base language per post.

Is this possible?

Thanks

maximeschoeni commented 7 years ago

Firstly, there is no reason to prefer 1.x over 2.x. The 2.0 version may contains a few bugs because it is still new but overall, it is just cleaner, faster and easier to extend.

Actually I think it is very possible to achieve what you want.

First we must chose a main language and stick with it. Lets say we chose english for main language. Now in the german side of the site, it is going to work as expected: when a post have a german translation it will be displayed in german, otherwise in english. But in the english side, when a post have a german content but no english content, it is just going to be blank. We can use a filter to fix this: sublanguage_translate_the_post. This filter is called by sublanguage for every queried post. So we just need to test every post if the language is english and if the content is empty, and if so, translate post to german:

function my_sublanguage_retro_inheritance($translated_post, $original_post, $language, $sublanguage) {

    if ($sublanguage->is_main($language) && empty($original_post->post_content)) { // -> the queried language is english (main) and the content is empty

        $german = $sublanguage->get_language_by('de', 'post_name'); // -> find german language object

        return $sublanguage->translate_post($original_post, $german); // -> use german content instead

    }

    return $translated_post;
}

add_filter('sublanguage_translate_the_post', my_sublanguage_retro_inheritance, 10, 4);

Does it help?

misiman commented 7 years ago

Sorry for the very long delay, I'm still working on some (unrelated) custom tables but am nearly at the point of choosing a language plugin (or deciding to write my own).

In the current plugin description you write "Translations are custom-post-types parented to original posts", whereas in issue #17 you said "translations are stored in post_meta".

To me, translations stored as a CPT parented to the original seems much more logical and true to Wordpress principles - can you clarify which one Sublanguage 2.3 is using?

Thanks

maximeschoeni commented 7 years ago

Hello, Translations stored as a CPT parented to the original was how the plugin worked until 2.0. At first it sounded also very logical to me to do this way. But after experimenting it, I found it was causing a lot of bugs when used along with other plugins. Storing translations in post-meta data (as it is from 2.0) reveals much more stable. This solution also needs less database queries and I think it's overall better for performances.

okainov commented 5 years ago

This filter is called by sublanguage for every queried post. So we just need to test every post if the language is english and if the content is empty, and if so, translate post to german:

Thanks for the snippet, but where should I add it?

okainov commented 5 years ago

But in the english side, when a post have a german content but no english content, it is just going to be blank

At least for me it does not work, when I try to add post from wp-admin which does not contain anything in my main language but only translation, it gives me "Error updating"