Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Advice on best way to create translation/filters in functions

  1. #1
    Jonny is offline Hello World
    Join Date
    Apr 2010
    Posts
    11

    Icon5 Advice on best way to create translation/filters in functions

    Hi - I am working through setting up some functions and the following very basic example explains where I am currently at - the function would be designed to have an array ($args) passed to it - this is merged with the defaults that I have filtering and translation setup on:

    PHP Code:
    function my_function($args) {
        
    // Setup default with filtering and translation
        
    $defaults = array (
            
    'my_text' => apply_filters'my_text_filter'__('My text default string here ''my_plugin') )
        );

        
    $args wp_parse_args$args$defaults );
        
    extract$argsEXTR_SKIP );

        
    // DATA VALIDATION CODE FOR $my_text HERE
        // Final cleanup ready for use
        
    $my_text_clean esc_attrwp_kses_data($my_text'') );

        return 
    $my_text_clean;

    I'm preparing it for output at the end with esc_attr() - would you suggest that this is best done elsewhere or within the function? I could always build 'output_style' into the $args for this - but that's not too relevant at the moment. I'm just looking for the neatest (and I guess safest!) way to add the ability for filtering and translation in a function.

    Any thoughts on this would be much appreciated - I'm not exactly a super-duper code warrior - just trying to do the right thing so users get the most out of the code I'm creating. Is there a better/more flexible way to achieve this?

  2. #2
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

    Default

    Why use filters when you are already using the built translation stuff using __(...,...)?

  3. #3
    Jonny is offline Hello World
    Join Date
    Apr 2010
    Posts
    11

    Default

    Good question - its just for the fact that some users understand filters and other more advanced users know how to manipulate this stuff with a language file (or simply do just want to translate)... overkill you think, I dunno?

  4. #4
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

    Default

    Don't really like the filter approach for this anyway so I'm biased. I think its a little overkill and a cumbersome method to translate.
    If you just want a simple method for translation perhaps the use of parse_ini_file is a simpler approach for users.
    http://www.php.net/manual/en/functio...e-ini-file.php

  5. #5
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

  6. #6
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

    Default

    Did not know about that filter. Not that I've been looking for it. A lot of calls it will process using that method though. There really should be a filter per domain. Jonnys method is to be prefered to using gettext.

  7. #7
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default

    I suggest a variation of it for filtering an odd string in my plugin.

  8. #8
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

  9. #9
    Jonny is offline Hello World
    Join Date
    Apr 2010
    Posts
    11

    Icon14

    Yea - that Westi is a guru;) I came across that way back when it was originally posted, quite a trick huh! As an aside it's useful for quick testing translation strings.

    Back on topic - what do you think about this translated, filtered default idea from my original post - would it substantially add to execution overhead? I think it looks pretty neat and just wanted to check in with the guys on here to see what they thought? I would be rolling out a similar code pattern to quite a substantial free, GPLv2 WordPress project I'm working on and didn't want to go ahead without getting a few peoples opinions;)

    I think it basically offers a compact way to let people run simple filters if they want OR run a language pack if they need to. Andreas, I hear you on the 'double functionality' thing - but my answer is why not - if it suits different people If something was to give, it would be the filter - I'll need to keep the translation functionality for the project.

  10. #10
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

    Default

    It wont add much to time since its use is limited to just your stuff. Am I correct to presuppose that you'll only have one filter?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •