Check if text is translated


#1

Hello,
In Kirby 2, I was using $page->text()->isTranslated() to check if a specific content was translated. I haven’t found how to do this in Kirby 3.
$page->text()->isEmpty() doesn’t work because an untranslated page fallback to the default language’s translation.
Same for $page->translation('code'), it fallback to the default language.
Thanks.


#2

Looks like you need a custom field method…


#3

Oh alright… Thanks for your answer.
However, I cannot find how to access the $kirby object from my plugin definition, and I guess I need it in order to check if the field exists/is not empty for the current language.


#4

You can always get the $kirby object through the Kirby() helper in a plugin, or through the field itself:$field->parent()->kirby()


#5

Thanks! This parent() getter doesn’t seem to appear in the docs, there’s just the model attribute which returns NULL.


#6

https://getkirby.com/docs/reference/templates/field-methods/parent


#7

Damn it, thanks and sorry about that.
I’m still not sure if I can access the raw value of the field, I always get the fallback one. So I made this field method which works but is so ugly: if the field’s value is the same as the fallback’s value, it means the content is untranslated.

Kirby::plugin('gildev/helpers', [
    'fieldMethods' => [
        'isTranslated' => function ($field) {
            // Check if field's value is empty
            if (empty($field->parent()->translation()->content()[$field->key()]))
                return false;

            // Check if field's value is the same as the fallback
            return $field->parent()->translation()->content()[$field->key()] != $field->parent()->translation(Kirby()->defaultLanguage()->code())->content()[$field->key()];
        }
    ]
]);

Sorry to bother you with that but is there a cleaner solution which entirely avoids getting the fallback value?


#8

I think we have to differentiate two different things:

  1. check if the translation file exist:

    $field->parent()->translation(kirby()->language()->code())->exists()`
    
  2. check if a single field value is different from the field value of the default language or empty

    To check if the field is empty, you don’t need a custom method $page->content(kirby()->language()->code())->text()->isEmpty()

To check if the field value is different from the default languages value (that’s what isTranslated() in Kirby 2 did, you’d get the field values from both fields and compare.

But the only thing that reliably tells me that there is no translation is when the field is actually empty. The second option could still be both the same languages but with some differences.

If this is important and should work reliably, maybe it would be better to use a custom field that marks a page as translated or not.


#9

Alright, thanks for the precisions.
In fact, I only need to check the “text” field to determine if the page is translated or not. Only the title may be translated but not the content, in that case the page will be marked as untranslated. That’s why I cannot use $page->content() like this but have to do $page->translation->content()[$field->key()] in order to get the field’s specific value (at least that’s the only way I currently know to access a $page’s field with a variable name). Doing so prevent me from using the isEmpty() method because I get the field’s value as a simple string.


#10

I forgot the field name above, corrected it.


#11

Great! But then this only works for the “text” field and cannot be used with another one…


#12

No, you can replace text with the name of any other field.