Author Topic: Event notifications (multilanguage)  (Read 654 times)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Event notifications (multilanguage)
« on: June 16, 2011, 03:29:56 AM »
There is an event ("City X got infested by rats.") and there is a page displaying all events. The trick is that the game is multilanguage (standard gettext translation) so I can not just dump a single string into database...

I wonder how to do it the best (and the simpliest/easiest) way?


Example events:
(INTEGER means there is a normal number, STRING means there is a normal string (but can be stored as an integer since it is derived from database), T_STRING means there a string string (again can be stored as int and then read from DB) and the string will have different translated string (translated string))

"City T_STRING got infested by INTEGER rats."
"Player STRING get elected T_STRING in a kingdom of T_STRING." (params: user_id (then converted to user_name); office_id (then converted to office_name and then the office_name translated to appropriate language); kingdom_id (then converted to kingdom_name and then translated)
"Plague! INTEGER % peasants die."
"Plague struck city of {list of T_STRING here}." (I can live without that one I guess and could stick to separate event for each city, so no list would be critical)





Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Event notifications (multilanguage)
« Reply #1 on: June 16, 2011, 03:37:41 AM »
Dump the single string, and translate after you get it out of the database?

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Event notifications (multilanguage)
« Reply #2 on: June 16, 2011, 03:41:19 AM »
Dump the single string, and translate after you get it out of the database?
Not possible. The string is built from several strings and integers (think of it like sprintf, you keep parameters separate).

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Event notifications (multilanguage)
« Reply #3 on: June 16, 2011, 03:41:47 AM »
Code: [Select]
event_messages { id, language, message }: {1, 'en', "City %s got infested by %s"}
events { id, message_id, data }: {137,1, ['kNoxville', 'rats']}

$msgs = select em.message, data from events e join events_messages em on e.message_id = em.id

echo vsprintf($msgs['message'], (array)json_decode($msgs['data']));

EDIT

when I think about it... less flexible, more performant...so you can choose
Code: [Select]
events { id, message, time }

$message = vsprintf(_($messages[$lang][$eventType]), $data); // where

insert into evets (message, time) values ('$message', NOW())

select message from events where time > NOW() - interval 3 day
(ommited escaping and stuff)
« Last Edit: June 16, 2011, 03:55:44 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Event notifications (multilanguage)
« Reply #4 on: June 16, 2011, 04:07:07 AM »
No, I can't use separate language rows in database either. There are several reasons, but the most important is that the translation is done after the code (maybe even months after). So, I simply can not put in database something I don't have yet.

(reallife example: imagine a game called RadioactiveShelter and a hypothetical translator, let's call him "Nox", who is supposed to make Czech translation but is so lazy that the game had to be launched before he finish his translation, so only part of the interface and event strings would be translated and the rest would have to display English version of the string :P)

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Event notifications (multilanguage)
« Reply #5 on: June 16, 2011, 04:14:09 AM »
Code: [Select]
event_messages { id, message }: {1, "City %s got infested by %s"}
events { id, message_id, data }: {137,1, ['kNoxville', 'rats']}

$msgs = select em.message, data from events e join events_messages em on e.message_id = em.id

echo vsprintf(_($msgs['message']), (array)json_decode($msgs['data']));
or
Code: [Select]
events { id, message, data }: {137, 'cityInfested', ['kNoxville', 'rats']}

$msgs = select message, data from events

echo vsprintf(_($messages[ $msgs['message'] ]), (array)json_decode($msgs['data']));
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Event notifications (multilanguage)
« Reply #6 on: June 16, 2011, 04:34:26 AM »
OK... so we have a reasonable way of storing unlimited number of strings in one table row (actually, I should have thought about it myself, dunno why I haven't :)).

But there is a catch. Some of the strings should go via gettext and some are to be displayed raw (like: city names are translated, player names and numbers are not).

Code: [Select]
echo sprintf(_("There is a player %s in a city of %s who broke his leg and lost %s HP."),$playername,_($cityname),10);

Offline andrewjbaker

  • Level 17
  • *
  • Posts: 154
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: Event notifications (multilanguage)
« Reply #7 on: June 16, 2011, 04:38:35 AM »
Hmm... remember that NIH issue you wrestle with frequently Chris? Kohana has a __() method for translation (as do a number of other frameworks), and it doesn't matter whether the string is /translatable/ or not.
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Event notifications (multilanguage)
« Reply #8 on: June 16, 2011, 04:42:56 AM »
Dirty solution just put array walk and gettext everything and it should return numbers and names intact...theoretically

Cleaner solution ... data will not be array of strings but array of arrays/objects of a string and it's flag determining whether it should be translated or not

Code: [Select]
array_walk($result['data'], function(&$item, $key) { // ;-)
  $item = $item['translatable'] ? _($item['string']) : $item['string'];
});

@andrewjbaker: what if the name of a player exactly matches some translatable string?
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Event notifications (multilanguage)
« Reply #9 on: June 16, 2011, 04:45:01 AM »
Hmm... remember that NIH issue you wrestle with frequently Chris? Kohana has a __() method for translation (as do a number of other frameworks), and it doesn't matter whether the string is /translatable/ or not.
_() and __() are standard gettext methods of translation, you don't need a framework for this :) What I meant you can not put __($playername) because it will cause problems if the player name is "Gold" or other general term :)

Offline andrewjbaker

  • Level 17
  • *
  • Posts: 154
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: Event notifications (multilanguage)
« Reply #10 on: June 16, 2011, 04:47:46 AM »
Quote
@andrewjbaker: what if the name of a player exactly matches some translatable string?

Whenever you emit the player's name, don't use translation. Erm... or I've missed something.
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Event notifications (multilanguage)
« Reply #11 on: June 16, 2011, 04:49:33 AM »
But in this case you do not know in advance what the meaning of the string is
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Event notifications (multilanguage)
« Reply #12 on: June 16, 2011, 05:02:47 AM »
Maybe something like this:

DATABASE: {id, formatstring, params}

Example:
formatstring="There is a player %s in a city of %s who broke his leg and lost %s HP."
params="R#playername#T#cityname#R#10"

Then explode() params field and all params of type "R" go unmodified/raw and "T" go through _().



BTW, I assume explode() is faster than json_decode(), is that correct?

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Event notifications (multilanguage)
« Reply #13 on: June 16, 2011, 05:09:36 AM »
i guess... test it out ;)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal