#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    436
    Rep Power
    8

    Custom error messages


    hi,

    I was asking myself, how can I handle custom error messages the best? With custom error messages I mean something like:

    Your application is made to send messages to other users, but there are some rules, like they can't send those to themselves. How can I handle this the best? And when I decide to use the same application with different languages? Maybe I should use define, but I don't know sure.

    Or should I put the errors in a database and call them when I need them?
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Many frameworks use the YAML file format to store "translation" files which can be used as associative arrays. Basically you put every line of text on your website into a file and then have your controller load that file when it goes to render the templates. If you want to select a new language, load a new file with the same keys but translated messages.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    436
    Rep Power
    8
    And when I'm not using a framework (I only work with code...), could I use a similar solution? Just making a file with an array and call it?
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You can do anything you want. Frameworks aren't naturally occurring, people write them.

    If I were writing an internationalized site without a framework, I'd probably use some sort of flat data structure like a YAML file, an include file, or a flat database table to store all the translations, and spit them out as necessary from a global array.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    436
    Rep Power
    8
    ok thanks, I think I'll go for the database, because I haven't done anything with YAML (not even heard of it ) and the included file could be accessed I think in one way or the other... still it won't be anything to worry about if the array can't be changed but better safe than sorry

    Thanks.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    The columns in your table should be:
    SECTION
    PAGE
    LANGUAGE
    LABEL
    CONTENT

    Section is the controller, page is the page, language is the spoken language being used, label is the actual variable in the page's template, and content is the actual text to show on the screen.

    You can omit "section" if you don't use controllers or folders in your site.

    When you go to render a page, SELECT label, content FROM thisTable WHERE page = '$currentPage' AND language = '$currentLanguage'

    Then, replace all the placeholders in your code (the labels) with the values from the table.

    Your placeholders, if you're not using a templating language, should be delimited, so that {{welcome_msg}} is the welcome message, and the brackets are stripped by your replacement function.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    436
    Rep Power
    8
    I get everything except the specific use of SECTION and PAGE... and I use PHP, so replacing the labels by the real content isn't hard to do. Thanks for this
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    SECTION and PAGE define which section/page of your site the label goes on. If you omit them, the labels will have to be very specific, like {{shopping_cart_thank_you}} rather than being on the page called "shopping_cart" and the label simply being "thank_you"
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Maniac's database is probably the best bang for the buck, giving you flexibility without getting too complex.

    Now if you want complex, I've been there.

    When I had to internationalize a system for a past employer I ended up putting the engine behind a SOAP service complete with language inheritance, common string caching/preloading, and on-demand string loading. The primary UI using it included an administration tool to maintain the language database, and the ability for admins to view the key onscreen alongside what it's translated to. And it was all handled through a simple Smarty modifier plugin.

    But Maniac's idea is a lot more straightforward.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    436
    Rep Power
    8
    ok, sounds difficult

    And when I have to display an error message? How do I do that? First get all the error messages and then display one from an array or use a query for every error message?
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Get all the possible strings for the page you're on, then get the proper error message.

    Or fetch all error messages and get the one for the page.

    The system design is up to you. Whichever makes the most sense for your site's layout.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo