Support » Fixing WordPress » Splitting plugin language files for frontend and admin

  • Resolved Seguinot

    (@seguinot)


    Hi
    I’m developing a plugin for which I generated a .pot file for i18n. It appears that 90% of po and mo files correspond to admin translation. For this reason splitting language files in admin and frontend part could speed up frontend page loading.

    I only found one quite old link about this subject: https://github.com/woocommerce/woocommerce/issues/2051. However, it seems that Woocommerce (I never used it) do not longer implement the solution given in that post.

    Splitting pot file is quite tricky:
    – we must generate 2 pot files from different files and folders (need some custom makepotfile.php)
    – but some strings are used in both admin and frontend part (how to remove duplicate string from one pot file)
    – we must load one or both files while running the plugin (this seems easy)

    Here are my questions:
    – is there any recommendations for splitting pot file in WordPress plugin?
    – is there an open source plugin implementing such a feature (I could base my work on this one)?

    Regards to the WordPress community.

Viewing 3 replies - 1 through 3 (of 3 total)
  • First answers to my own questions was found in makepot.php of WordPress core tools for i18n. This can be a good start to generate admin/frontend pot files for a plugin.

    I implemented a personal solution to split my pot file into 4 different pot files.
    This solution is based on a modified version of makepot.php which contains a new function (or project) called ‘wp-plugin-split’

    Attention: the proposed tools is in beta release, it needs further test to validate the method.

    The modified makepot.php and accompanying file makepot_args.php can be found in the i18n folder of my plugin
    You can test it by just modify your call to makepot using a command line like: php makepot.php wp-plugin-split /path/to/my/plugin-dir

    If the makepot_args.php is not present, makepot will render a single .pot file like the command php makepot.php wp-plugin /path/to/my/plugin-dir do.

    See my explanation about how to configure multiple pot files in makepot_args.php file.

    Please care to not erase old translation when switching to multiple pot files. For each language you must manually copy/split every existing .po file into several .po file as explained in makepot_args.php

    Similarly, when switching back to one single pot file every .po files of one language must be merge manually into one single file.

    If my method were to be used for splitting plugin pot files, several problems must be adressed:

    • Is makeplots_args.php a good solution for this problem ? Where should we locate this file ? (currently I put it in /i18n folder)
    • admin frontend and common pot filenames may be normalized.
    • Should the common pot file be named plugin_name.pot (that name is already used for single pot file) or plugin_name-common.pot (that creates a new pot file but avoid confusion with single pot file)?

    How to use splitted pot files with Polylang

    Polylang can only load one pot file per plugin (ie per domain) so calling load_textdomain multiple times can be broken depending on action hook used.

    Polylang will always load plugin_name-xx_YY.mo file for frontend and admin pages so splitting pot file is not usefull if the common file is called plugin_name-xx_YY.mo and you don’t want to load it every times.

    In order to use splitted pot files with Polylang you have to:

    • rename common pot/mo file plugin_name-xx_YY.mo to something else such as plugin_name-common-xx_YY.mo (this is not required if you want to load common mo file every times))
    • for frontend replace add_action( 'plugins_loaded', load_my_plugin_frontend_textdomain' ) by add_action( 'pll_translate_labels', load_my_plugin_frontend_textdomain' ) so that this action is fired after polylang removes its filter ‘load_textdomain_mofile’
    • add similar action for every .mo file using ‘pll_translate_labels’ for frontend and ‘plugins_loaded’ for backend
    • This reply was modified 2 years, 2 months ago by Seguinot.
    • This reply was modified 2 years, 2 months ago by Seguinot.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Splitting plugin language files for frontend and admin’ is closed to new replies.