Support » Plugin: Loco Translate » Translate strings of custom post template

  • Resolved nicoter

    (@nicoter)


    Hi,

    I’ve read the documentation but didn’t find this use case:

    I created a custom post template (e.g. single-cpt.php) and added strings like this to it:
    _e('My custom string', 'custom-post-template');

    What’s an update proof way of translating ‘My custom string’?

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Tim W

    (@timwhitlock)

    See guide to customizing child themes.
    https://localise.biz/wordpress/plugin/child-themes

    Thanks for the fast reply.

    I didn’t create a child theme, I added a custom post template to the existing theme.

    I already tried to create a custom language file by copying the existing and choosing location = custom, but syncing didn’t add the custom string to the file?

    Plugin Author Tim W

    (@timwhitlock)

    Don’t add code to an existing theme, unless you are the author of the theme.

    You asked for an update-proof solution. Modifying themes is never an update-proof solution. New strings should be added into a new text domain, and that is best achieved with a child theme to avoid updates destroying your changes.

    Adding a custom post template is not modifying a theme and totally update proof as well as best practice.

    Anyway, I thought when creating a custom language the new string should be added automatically when clicking the sync button, or am I wrong?

    Plugin Author Tim W

    (@timwhitlock)

    Your code appears to add a new text domain (custom-post-template), which I presume is not the text domain of your theme. If you haven’t created a child theme, what does this text domain belong to? Have you created a plugin that loads these strings? Are you trying to add a second text domain to the existing theme? Perhaps you didn’t mean to define a new text domain at all?

    If you can provide some clarity on this, I may be able to answer your questions.

    Thanks for the hint, I didn’t mean to define a new text-domain resp. I don’t know exactly what it does. I have updated the text domain to match the one of the theme.

    When clicking sync, the new strings still don’t show up.

    Can I manually edit /wp-content/languages/loco/themes/astra-de_DE.po from the custom language copy and add the strings or is this not the way to do it?

    To recap, what I did so far:

    1) Create the custom post template single-cpt.php in the theme (not child theme)

    2) Added custom strings like this: _e('My custom string', 'astra');

    3) In Loco I went to the theme and copied the German language file while selecting the custom location.

    4) Clicking “sync” and hoping the custom strings would appear.

    5) Added single-cpt.php to “Source file paths” (include) in the advanced settings of the theme bundle. Then synced again, to no avail.

    6) Only realized now there’s an “Edit template” link where I can also sync. When syncing there, my custom string gets added. Next when syncing the language, I can finally add the translation and it works.

    It’s my understanding the synced string gets added to the theme .pot file and my custom .po file. When I update the theme, the .pot file of the theme gets overwritten, correct?

    But my custom .po file is still there and when re-syncing the template my custom strings get re-added to the pot and the custom translations are not lost, correct?

    Plugin Author Tim W

    (@timwhitlock)

    It’s my understanding the synced string gets added to the theme .pot file and my custom .po file. When I update the theme, the .pot file of the theme gets overwritten, correct?

    Correct.

    But my custom .po file is still there and when re-syncing the template my custom strings get re-added to the pot and the custom translations are not lost, correct?

    Correct that the POT is synced to the source code. (See working with templates).

    Whether translations are lost depends on where you’re saving the PO file. See FAQ and docs.

    Back to your original question:

    What’s an update proof way of translating ‘My custom string’?

    The update-proof solution – as I’ve already mentioned – is to use a child theme. You’ve already said you’re not interested in that approach, but as long as you know how it all works you can take your own calculated risks.

    If I’ve answered your question please mark the topic as resolved.

    Many thanks Tim!

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.