Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SMARTY] Funkcja {l} nie tłumaczy wartości dynamicznych
Forum PHP.pl > Forum > Przedszkole
colachips
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.
rocktech.pl
Witam.

Niewiadome:

1. Wersja Smarty
2. Co robi block.l.php w katalogu plugins?

Pokaż kod. implementuje get_text ..
colachips
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. }
rocktech.pl
Hmm ciężka sprawa. Spróbuj tak ...

[SMARTY] pobierz, plaintext
  1. {capture name='title'}{$aBreadcrumbs[br].title}{/capture}
  2. {l}{$smarty.capture.title}{/l}
[SMARTY] pobierz, plaintext


Ewentrulanie przekaż to jako parametr do bloku

[SMARTY] pobierz, plaintext
  1. {l value=$aBreadcrumbs[br].title}{/l}
[SMARTY] pobierz, plaintext


Jak już doszedłeś daj w funkcji

  1. function translate($value) {
  2. var_dump($value);


colachips
pierwszy kod wyświetla dokładnie tak samo jak przedtem i nie tłumaczy.

[SMARTY] pobierz, plaintext
  1. {l value=$aBreadcrumbs[br].title}{/l}
[SMARTY] pobierz, plaintext


to nie wyswietla nic.

  1. var_dump($value);


nie wyswietla 'Moje konto', dla żadnego z poniższych
[SMARTY] pobierz, plaintext
  1. {l}Moje konto{/l}
  2. {l}{$aBreadcrumbs[br].title}{/l}
[SMARTY] pobierz, plaintext


Może chodzi o cast na string? Sam już nie wiem co o tym myśleć...
aras785
A to coś pokaże?
[SMARTY] pobierz, plaintext
  1. {assign var='lang' value='$aBreadcrumbs[br].title'}
  2. {l}{$lang}{/l}
[SMARTY] pobierz, plaintext
colachips
Wyświetla 'Moje konto', ale nie tłumaczy.

Spróbowałem jeszcze
[SMARTY] pobierz, plaintext
  1. {l}{$aBreadcrumbs[br].title|trim}{/l}
[SMARTY] pobierz, plaintext
ale nadal to samo, czyli po prostu 'Moje konto'

Zastanawiam się czy ma znaczenie to, że to wszystko odbywa się w section?
[SMARTY] pobierz, plaintext
  1. {section name=br loop=$aBreadcrumbs} ... {/section}
[SMARTY] pobierz, plaintext
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.