Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SMARTY] Funkcja {l} nie tłumaczy wartości dynamicznych
colachips
post
Post #1





Grupa: Zarejestrowani
Postów: 49
Pomógł: 0
Dołączył: 12.01.2009

Ostrzeżenie: (0%)
-----


Witam,

nie wiem jak dobrze opisać problem, ale spróbuję. Dostałem "projekt po kimś" do poprawki. W kodzie jest miejsce w którym tekst powinien zostać przetłumaczony w zależności od używanej przez użytkownika wersji językowej. Oto kod, który przetłumaczy frazę "Moje konto", w/g słownika zdefiniowanego w pliku:
  1. {l}Moje konto{/l} // zostanie przetłumaczone na niemieckie "Mein Konto" i to działa ok, ale...
  2.  
  3. {l}{$aBreadcrumbs[br].title}{/l} // ... to już nie działa jak trzeba, mimo że zmienna generuje dokładnie "Moje konto"


Wydaje mi się, że Smarty coś miesza/przesłania albo w pierwszej kolejności próbuje tłumaczyć a dopiero później podstawia wartości... Nie znam się za bardzo na Smarty, także nie bardzo wiem jak to sprawdzić.

Może ktoś wie dlaczego tak się dzieje? Albo gdzie/jak szukac przyczyny?

Pozdr.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
colachips
post
Post #2





Grupa: Zarejestrowani
Postów: 49
Pomógł: 0
Dołączył: 12.01.2009

Ostrzeżenie: (0%)
-----


Witam.

1. 2.5.3
2. nie mogę znaleźć block.l.php... Przeszukałem pliki i w jednym znalazłem 2 funkcje, które póki co niewiele mi rozjaśniły sprawę, ale coś sprawdziłem dzięki temu. Wiem, że string jest hasowany na md5 i porównywany z kluczem w tablicy z tłumaczeniem. MD5 dla "Moje konto" to b22652b0e4a3d18fcff0e53f0fc14564 i taki klucz jest w pliku z tłumaczeniem (jak wspomniałem wyżej, {l}Moje konto{/l} zostanie prawidłowo przetłumaczone na 'Mein konto'). Sprawdziłem tak:
  1. {php}echo md5('{$aBreadcrumbs[br].title}') {/php} // wygenerowałem hash dla '{$aBreadcrumbs[br].title}' - 8bacd0dd34989a96c6640709988dc0c4 i dodałem go do słownika
  2.  
  3. {l}{literal}{$aBreadcrumbs[br].title}{/literal}{/l} // nie działa, mimo że hash jest w słowniku :(



Wstawiam kod obu funkcji poniżej (Int.Smarty.class.php):
  1. /**
  2. * Provides a PHP-side facility to translate strings through the IntSmarty system.
  3. * Identical to the block IntSmarty tag {l}{/l}, it takes the provided string
  4. * and attempts to find a suitable translation for the current language.
  5. *
  6. * @param string $value The string to translate
  7. * @return string The translated version of the string for the current language,
  8. * or the same string passed to the method if no translation was
  9. * found.
  10. */
  11. function translate($value)
  12. {
  13. if (is_string($value))
  14. {
  15. $hash = md5($value);
  16.  
  17. if (array_key_exists($hash, $this->translation ))
  18. return $this->translation[$hash];
  19. else
  20. {
  21. $this->transtable_updated = true;
  22. $this->translation[$hash] = $value;
  23. return $value;
  24. }
  25. }
  26. }
  27.  
  28. /**
  29.  * This function contains the functionality for the block-level {l}{/l} tags used in IntSmarty templates
  30.  * to provide the localization functionality. It is registered during the construction of an IntSmarty
  31.  * object.
  32.  *
  33.  * @param string $content The content of the template before it is compiled
  34.  * @param object $smarty A reference to the active Smarty class
  35.  * @return string The original template with all of the translations in place
  36.  */
  37. function smarty_lang_prefilter($content, &$smarty)
  38. {
  39. $inst = &$smarty->parent_inst;
  40.  
  41. $ldq = preg_quote($inst->left_delimiter, '!');
  42. $rdq = preg_quote($inst->right_delimiter, '!');
  43.  
  44. /* Grab all of the tagged strings */
  45. preg_match_all("!{$ldq}l{$rdq}(.*?){$ldq}/l{$rdq}!s", $content, $match);
  46.  
  47. foreach($match[1] as $str)
  48. {
  49. $q_str = preg_quote($str);
  50. $hash = md5($str);
  51.  
  52. /* Do we have a translation for this string? */
  53. if (key_exists($hash, $inst->translation))
  54. {
  55. /* Replace all occurances of this string with its translation */
  56. $content = preg_replace("!{$ldq}l{$rdq}$q_str{$ldq}/l{$rdq}!s", $inst->translation[$hash], $content);
  57. }
  58. else
  59. {
  60. $inst->transtable_updated = true;
  61. $inst->translation[$hash] = $str;
  62. }
  63. }
  64.  
  65. /* Strip off the tags now that the strings have been replaced */
  66. $content = preg_replace("!{$ldq}l{$rdq}(.*?){$ldq}/l{$rdq}!s", "\${1}", $content);
  67.  
  68. return $content;
  69. }
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 10.10.2025 - 17:25