2

I've taken over a website targeting different markets using five language versions, each is setup as it's own site within a multisite network. The main website mainly targets a British audience and one of the language versions targets an American audience.

To get the lang right in the html tag, like this <html <?php language_attributes(); ?>> I need to set the Site language of each site to the language the site in question is written in, but when I install the en_GB language files the default English option disappears. And according to the WP website there are no en_US language files.

How does people usually solve this? While I could, in this case, hardcode the lang-attribute for each site it still raises the question for future use.

ETA: based on the code suggested by @toscho below I added this to my functions file. It adds a language to the list, but it won't be saved for some reason. The $current parameter still holds the previous parameter.

add_filter('mu_dropdown_languages', 'add_en_us', 10, 3);

function add_en_us( $output, $lang_files, $current ) {
    array_unshift(
        $output,
        '<option value="en_US"' . selected( $current, 'en_US', false ) . '>'
        . __( 'American English' ) . "</option>"
    );
    return $output;
}

Based on time constraints in this project I will try adding empty language files instead, however I would still like to know how to make this work properly.

  • Maybe you can create the en-US languages files. Just a quick thought. – cybmeta Sep 17 '13 at 8:58
  • I probably could, but I figure I shouldn't have to "invent the wheel" again, since it's all there already. Though maybe I could just create empty files to fool WP? – Linda H Sep 17 '13 at 9:22
  • I think empty en_US files should work. Note that using both, en-GB and en-US, is like having a multilingual site and Wordpress official documentation says "Wordpress doesn't support multilingual out of the box" See codex.wordpress.org/Multilingual_WordPress – cybmeta Sep 17 '13 at 9:33
  • The site isn't multilingual, as per Wordpress' definition, but a multisite with different languages per site - one is en-GB, one is en-US and the other are nn-NO, da-DK and sv-SE, so five languages/locales in total, targeting five different markets. – Linda H Sep 18 '13 at 12:45
  • I tried an empty po-file - or actually a copy of the original wordpress.pot-file without any translations - which did the trick. It may not be the prettiest solution, but it's much nicer than hardcoding stuff. I'd still love to figure out how to code it, as in the answer below, but for now I'm happy with this simple little trick. Thanks! – Linda H Sep 18 '13 at 12:49
2

You can filter mu_dropdown_languages and re-add American English here.

Sample code, not tested:

add_filter( 'mu_dropdown_languages', function( $output, $lang_files, $current )
{
    array_unshift(
        $output,
        '<option value=""' . selected( $current, 'en_US', false ) . '>'
            . __( 'American English' ) . "</option>"
    );
    return $output;
}, 10, 3 );

Filter sanitize_option_WPLANG or pre_update_option_WPLANG to jump into the save process and correct any possible issues.

  • I just tried this (breaking out the function) and it took me halfway - it added the option but it's not saved/marked as selected when I try to use it. See the ETA in the original post. If I could just figure out that last bit, it would be a much neater solution than adding empty language files. – Linda H Sep 18 '13 at 9:42
  • @LindaH See my update. Unfortunately, I have no time for tests right now, sorry. – fuxia Sep 18 '13 at 13:54
  • Thanks a bunch! I'll have a look at it later. It's obvious to me that I really need to learn more about filters and such - there are just so many little tricks to WP that it's hard to know where to begin :) – Linda H Sep 18 '13 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.