|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
s0undt3ch Smarty Rookie
Joined: 11 Nov 2004 Posts: 5
|
Posted: Thu Nov 11, 2004 1:00 pm Post subject: Andre's multilanguage aproach, with also multiple templates |
|
|
Andre's multilanguage aproach, with also multiple templates!!!
Too much code to post here, take a look at:
THIS |
|
Back to top |
|
smartknight Smarty n00b
Joined: 15 Nov 2004 Posts: 4
|
Posted: Mon Nov 15, 2004 7:02 pm Post subject: |
|
|
Thanks for this information.
smartknight |
|
Back to top |
|
brunobbm Smarty Rookie
Joined: 18 Jul 2004 Posts: 8 Location: Rio de Janeiro, RJ, Brazil
|
Posted: Tue Dec 21, 2004 12:43 pm Post subject: Solution |
|
|
Instead of loading from config files, witch btw is very slown (before the compiling), I use a small class that extends smarty, to configure everything as everybody does, but I have also 2 more methods, but before I hava an array with all possible languages and its codes:
Code: |
/*****************************************************
* Avaiable Languages
*****************************************************/
var $avaiable_languages = array(
'af' => 'Afrikaans',
'ar_ae' => 'Arabic (U.A.E.)',
'ar_bh' => 'Arabic (Bahrain)',
'ar_dz' => 'Arabic (Algeria)',
'ar_eg' => 'Arabic (Egypt)',
'ar_iq' => 'Arabic (Iraq)',
'ar_jo' => 'Arabic (Jordan)',
'ar_kw' => 'Arabic (Kuwait)',
'ar_lb' => 'Arabic (Lebanon)',
'ar_ly' => 'Arabic (libya)',
'ar_ma' => 'Arabic (Morocco)',
'ar_om' => 'Arabic (Oman)',
'ar_qa' => 'Arabic (Qatar)',
'ar_sa' => 'Arabic (Saudi Arabia)',
'ar_sy' => 'Arabic (Syria)',
'ar_tn' => 'Arabic (Tunisia)',
'ar_ye' => 'Arabic (Yemen)',
'ar' => 'Arabic',
'as' => 'Assamese',
'az' => 'Azeri (Cyrillic)',
'az' => 'Azeri (Latin)',
'be' => 'Belarusian',
'bg' => 'Bulgarian',
'bn' => 'Bengali',
'ca' => 'Catalan',
'cs' => 'Czech',
'da' => 'Danish',
'de_at' => 'German (Austria)',
'de_ch' => 'German (Switzerland)',
'de_li' => 'German (Liechtenstein)',
'de_lu' => 'German (Lexumbourg)',
'de' => 'German (Germany)',
'div' => 'Divehi',
'el' => 'Greek',
'en_au' => 'English (Australia)',
'en_bz' => 'English (Belize)',
'en_ca' => 'English (Canada)',
'en_gb' => 'English (United Kingdom)',
'en_ie' => 'English (Ireland)',
'en_jm' => 'English (Jamaica)',
'en_nz' => 'English (New Zealand)',
'en_ph' => 'English (Philippines)',
'en_tt' => 'English (Trinidad)',
'en_us' => 'English (United States)',
'en_za' => 'English (South Africa)',
'en_zw' => 'English (Zimbabwe)',
'en_uk' => 'English (United Kindon)',
'en' => 'English',
'es_ar' => 'Spanish (Argentina)',
'es_bo' => 'Spanish (Bolivia)',
'es_cl' => 'Spanish (Chile)',
'es_co' => 'Spanish (Colombia)',
'es_cr' => 'Spanish (Costa Rica)',
'es_do' => 'Spanish (Dominican Republic)',
'es_ec' => 'Spanish (Ecuador)',
'es_es' => 'Spanish (Spain)',
'es_gt' => 'Spanish (Guatemala)',
'es_hn' => 'Spanish (Honduras)',
'es_mx'=> 'Spanish (Mexico)',
'es_ni' => 'Spanish (Nicaragua)',
'es_pa' => 'Spanish (Panama)',
'es_pe' => 'Spanish (Peru)',
'es_pr' => 'Spanish (Puerto Rico)',
'es_py' => 'Spanish (Paraguay)',
'es_sv' => 'Spanish (El Salvador)',
'es_us' => 'Spanish (United States)',
'es_uy' => 'Spanish (Uruguay)',
'es_ve' => 'Spanish (Venezuela)',
'es' => 'Spanish',
'et' => 'Estonian',
'eu' => 'Basque',
'fa' => 'Farsi',
'fi' => 'Finnish',
'fo' => 'Faeroese',
'fr_be' => 'French (Belgium)',
'fr_ca' => 'French (Canada)',
'fr_ch' => 'French (Switzerland)',
'fr_lu' => 'French (Luxembourg)',
'fr_mc' => 'French (Monaco)',
'fr_fr' => 'French (France)',
'gd' => 'Gaelic',
'gu' => 'Gujarati',
'he' => 'Hebrew',
'hi' => 'Hindi',
'hr' => 'Croatian',
'hu' => 'Hungarian',
'hy' => 'Armenian',
'id' => 'Indonesian',
'is' => 'Icelandic',
'it_ch' => 'Italian (Switzerland)',
'it' => 'Italian (Italy)',
'ja' => 'Japanese',
'ka' => 'Georgian',
'kk' => 'Kazakh',
'kn' => 'Kannada',
'ko' => 'Korean',
'kok' => 'Konkani',
'kz' => 'Kyrgyz',
'ls' => 'Slovenian',
'lt' => 'Lithuanian',
'lv' => 'Latvian',
'mk' => 'FYRO Macedonian',
'ml' => 'Malayalam',
'mn' => 'Mongolian (Cyrillic)',
'mr' => 'Marathi',
'ms' => 'Malay (Brunei)',
'ms' => 'Malay (Malaysia)',
'mt' => 'Maltese',
'nb_no' => 'Norwegian (Bokmal)',
'ne' => 'Nepali (India)',
'nl_be' => 'Dutch (Belgium)',
'nl' => 'Dutch (Netherlands)',
'nn_no' => 'Norwegian (Nynorsk)',
'no' => 'Norwegian (Bokmal)',
'or' => 'Oriya',
'pa' => 'Punjabi',
'pl' => 'Polish',
'pt_br' => 'Portuguese (Brazil)',
'pt' => 'Portuguese (Portugal)',
'rm' => 'Rhaeto-Romanic',
'ro_md' => 'Romanian (Moldova)',
'ro' => 'Romanian',
'ru_md' => 'Russian (Moldova)',
'ru' => 'Russian',
'sa' => 'Sanskrit',
'sb' => 'Sorbian',
'sk' => 'Slovak',
'sq' => 'Albanian',
'sr' => 'Serbian (Cyrillic)',
'sr' => 'Serbian (Latin)',
'sv_fi' => 'Swedish (Finland)',
'sv' => 'Swedish ',
'sw' => 'Swahili',
'sx' => 'Sutu',
'syr' => 'Syriac',
'ta' => 'Tamil',
'te' => 'Telugu',
'th' => 'Thai',
'tn' => 'Tswana',
'tr' => 'Turkish',
'ts' => 'Tsonga',
'tt' => 'Tatar',
'uk' => 'Ukrainian',
'ur' => 'Urdu',
'uz' => 'Uzbek (Cyrillic)',
'uz' => 'Uzbek (Latin)',
'vi' => 'Vietnamese',
'xh' => 'Xhosa',
'yi' => 'Yiddish',
'zh_cn' => 'Chinese (China)',
'zh_hk' => 'Chinese (Hong Kong SAR)',
'zh_mo'=> 'Chinese (Macau SAR)',
'zh_sg' => 'Chinese (Singapore)',
'zh_tw' => 'Chinese (Taiwan)',
'zh' => 'Chinese',
'zu' => 'Zulu'
);
|
The methods
Code: |
/*****************************************************
* Load confug vars
*****************************************************/
function config_load($controller_id=0)
{
$this->config_vars = $this->database->fetch_keyvalue('SELECT SQL_CACHE
p.preference_key,
p.preference_value
FROM
'.$this->database->tables['preferences'].' AS p
WHERE
p.controller_id=\'0\'
OR
p.controller_id=\''.$controller_id.'\'');
if(isset($this->config_vars))
{
if(isset($this->_config[0]['vars']))
{
$this->_config[0]['vars'] = array_merge($this->_config[0]['vars'],$this->config_vars);
}
else
{
$this->_config[0]['vars'] = $this->config_vars;
}
}
}
/*****************************************************
* Load language vars
*****************************************************/
function lang_load($language='en_uk')
{
$this->lang_vars = $this->database->fetch_keyvalue('SELECT SQL_CACHE
l.language_key,
l.language_value
FROM
'.$this->database->tables['languages'].' AS l FORCE INDEX(language_code)
WHERE
l.language_code=\''.$language.'\'
');
if(isset($this->lang_vars))
{
$this->lang_vars = array_merge($this->lang_vars,$this->avaiable_languages);
if(isset($this->_config[0]['vars']))
{
$this->_config[0]['vars'] = array_merge($this->_config[0]['vars'],$this->lang_vars);
}
else
{
$this->_config[0]['vars'] = $this->lang_vars;
}
}
}
|
So I can use {#config_var_name#}.. But if I wan to translate lets say a string inside a foreach or section I have a plugin:
Code: | {translate prefix='' string=$looparray.var sufix=''} |
I can also use it in every string, phare or anything... Here is the code:
Code: |
<?php
function smarty_function_translate($params,&$smarty)
{
if(isset($params['string'])) $original_string = $string = $params['string'];
if(isset($params['prefix'])) $string = $params['prefix'].$string;
if(isset($params['sufix'])) $string = $string.$params['sufix'];
if(isset($smarty->lang_vars[$string]))
{
return $smarty->lang_vars[$string];
}
else
{
return $original_string;
}
}
?>
|
I also have two more functions:
Code: | {translate_key_html_options prefix='' options=$options_range sufix='' selected=$someselection} |
Code: | {translate_keyvalue_html_options prefix='' options=$options_range sufix='' selected=$someselection} |
I found it is much faster than loading from the file system, and it can be managed from the web, we can add new languages, and delete, do whatever we want.
Regards,
Bruno B B Magalhaes |
|
Back to top |
|
igor Smarty Rookie
Joined: 11 Jan 2005 Posts: 6
|
Posted: Thu Jan 13, 2005 11:46 am Post subject: |
|
|
Ouch, I've just read the entire thread and I need some time to understand each solution. Thanks to all, it's really interesting.
however, I haven't seen tips to manage long texts instead of strings, like articles. Does anybody faced this problem? |
|
Back to top |
|
bdv Smarty Rookie
Joined: 18 Feb 2005 Posts: 5
|
Posted: Fri Feb 25, 2005 2:50 am Post subject: Re: IntSmarty |
|
|
NewfieBilko wrote: |
He talks of the IntSmarty by John Coggswall.... I have been trying to impliment it for a few days now but to no success.. Can anyone offer any help? I am having one weird problem where my L block function will not pick up.. When I put something in between {l} and {/l} all i get is the following errors:
Warning: Smarty plugin error: [in leftbar.tpl line 4]: unknown tag - 'l' in /var/www/php2/Smarty/Smarty.class.php on line 1889
Warning: Smarty plugin error: [in leftbar.tpl line 4]: unknown tag - '/l' in /var/www/php2/Smarty/Smarty.class.php on line 1889
Fatal error: Call to undefined function: () in /var/www/php2/templates_c/N626/N626615291/leftbar.tpl.php on line 8
Weird eh? I have installed the IntSmarty Class. If someone who knows a thing about the block function or IntSmarty, please reply OR e-mail me: na027436@students.cna.nl.ca
THANKS |
Just found this:
http://www.php.net.my/332-Internalize-your-website-with-IntSmarty-and-Smarty.html _________________ Bruce Vincent |
|
Back to top |
|
mshaffer Smarty Rookie
Joined: 18 Dec 2003 Posts: 12
|
Posted: Mon Feb 28, 2005 2:48 pm Post subject: CMS framework |
|
|
AZTEK wrote: | I wrote mine with idea its to be portable because its part of my CMS. |
Like always, I am impressed with your abstraction mentality. I am curious if I could take a look at your framework for CMS. I am working on one and have incorporated your [translate] element.
I am running into a few issues for portability.
1. Recursive nature of smarty (or lack thereof)
2. Multiple plugin directories.
My abstraction at the display level include the following:
* lang
* colors
* templates (right toc, left toc)
* css
Based on these abstractions you have the utmost flexibility. With my CMS and framework integrated into smarty, modules are created and managed around this framework. Translated items live in XML files like suggested.
_default.xml is the default; en.xml is english and en_us.xml is english-US. The parsing uses a hierarchy to determine which one to display.
Code: |
<?xml version="1.0" encoding="utf-8" ?>
<locale lang="en">
<header>
<project>Project Name</project>
<create>2003-01-12 17:24-0500</create>
<revise>2003-01-15 19:15-0500</revise>
<trans>Your Name - Your Email</trans>
<lang>English</lang>
</header>
<message>
<label>myWelcome</label>
<translation>Hello %f! If you are not %f %l, click here!</translation>
</message>
<message>
<label>myLogin</label>
<translation>LOGIN</translation>
</message>
<message>
<label>myLogout</label>
<translation>LOGOUT</translation>
</message>
</locale>
|
Sample file of xml with inline content module that links to translate wrapper.
Code: |
{x: kas type="module" name="contentManager" function="grabContent" content="introContent" caching="2" cache_ttl="300" lang="es"}
|
As I refine my CMS, I would be very interested in seeing your abstraction for your CMS.
Best regards,
monte
{x: |
|
Back to top |
|
jonr Smarty n00b
Joined: 28 Feb 2005 Posts: 2 Location: Iceland
|
Posted: Thu Mar 10, 2005 2:37 pm Post subject: |
|
|
Ok, I'm trying to get the smartyML class method to work. First the files:
The php file:
Code: |
<?php
require_once('Smarty.class.php');
require_once('SmartyML.php');
$smarty = new smartyML();
$smarty->template_dir = '/mnt/plweb/www/templates/';
$smarty->compile_dir = '/mnt/plweb/templates_c/physicallink';
$smarty->display('i18test.tpl');
?>
|
And the template file:
Code: |
<html>
<body>
##NG_HELLO_WORLD##<br>
##NG_MULTILINE##<br>
<input type="button" value="##NG_OK##" onclick="alert('##NG_OK##')">
<input type="button" value="##NG_CANCEL##" onclick="alert('##NG_CANCEL##')">
</body>
</html>
|
And the language file inside languages/en:
Code: |
// Engrish
// put in languages/en/global.lng
NG_OK=OK
NG_ABORT=Abort!
NG_CANCEL=Cancel
NG_MULTILINE=Yeah, whatever
in two lines!
NG_HELLO_WORLD=Hello world<br/>How' you doin'?
|
I also have languages/is/global.lng, but nothing works, anyway.
Please tell me what I am doing wrong!
P.S. The result is a page with the ## tags. |
|
Back to top |
|
Dirk Smarty n00b
Joined: 16 Mar 2005 Posts: 1 Location: Brighton
|
Posted: Thu Mar 17, 2005 5:03 pm Post subject: Gettext for translation |
|
|
Hi,
I'm very new to this environment. Trying to get SmartyGettext to work but I get the following error: (Redhat with php5)
# ./tsmarty2c.php *.tpl > smarty.c
bash: ./tsmarty2c.php: /usr/local/bin/php: bad interpreter: No such file or directory
This is not the directory for php on the system so where can I change it?
or what causes the problem?
Thanks
Dirk. |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Thu Mar 17, 2005 5:23 pm Post subject: |
|
|
You can edit the file and fix the path to php in the first line, or you can just run "php ./tsmarty2c.php" instead. |
|
Back to top |
|
Jakob Smarty n00b
Joined: 13 Apr 2005 Posts: 3
|
Posted: Sat Apr 16, 2005 4:07 pm Post subject: Easy preparation for translation |
|
|
I prepare my code for translation this way
Code: | <?php // in PHP
print _("Hello, world");
?>
{* in templates *}
<h1>{"Menu"|_}</h1>
|
You just have to define the function _(), for instance:
Code: |
if ($use_gettext) {
function _($s) { return gettext($s); }
} else if ($use_my_translations) {
function _($s) {
global $my_translations;
return isset($my_translations[$s]) ? $my_translations[$s] : $s;
}
} else {
function _($s) { return $s; }
}
|
|
|
Back to top |
|
irfanuygur Smarty Rookie
Joined: 02 May 2005 Posts: 13
|
Posted: Fri Jul 15, 2005 6:28 pm Post subject: |
|
|
Hi Jakob,
that's a nice implementation but I have 1 question
how do you retrieve the translation strings from the template to produce the gettext .po files ?
From php code you can extract them using "xgettext -n *.php" but how can you retrieve them from .tpl files to produce the .po translation file ? |
|
Back to top |
|
crashi Smarty n00b
Joined: 29 Jul 2005 Posts: 3
|
Posted: Fri Jul 29, 2005 11:41 am Post subject: |
|
|
Hi!
I'm using the smartyML class method but how I use the = (equal) in my language files?
eg:
Code: | STRING_CHARS=Allowed chars: A-Z, 0-9, +, - *, /, =, ^. |
It ends with /,
How I can escape the =? \= doesn't work
Sorry for my bad english, I hope you can understand me ^^
Crashi |
|
Back to top |
|
hex Smarty n00b
Joined: 12 Jun 2006 Posts: 0 Location: Krakow, Poland
|
Posted: Mon Jun 12, 2006 3:03 pm Post subject: |
|
|
Hi,
I would like to use modifiers (eg. {"##TEXT##"|lower}) but it does not work.
Have you got any ideas why?
What shoud I do to make it work?
Ps
I use SmartyMultilanguageSupport |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 9:21 pm Post subject: |
|
|
Hello, I'm using your script. I like it so much. But I got some issues Im trying to solve.
________
California Mdecial Marijuana Dispensaries
Last edited by mateusfig on Sun Feb 13, 2011 3:00 am; edited 1 time in total |
|
Back to top |
|
Lemon Juice Smarty Pro
Joined: 24 May 2006 Posts: 109
|
Posted: Sat Jan 13, 2007 2:42 pm Post subject: simple method without external file |
|
|
Someone posted earlier in this thread that he wanted to have all the translations within the templates and not in external language files. I've been also looking for a method to do it this way - I understand that such an external file can be handy if a site has many language versions but most often I need to make a site in just two languages and so it is more convenient to me to have all texts in the templates and not be bothered with any placeholders and external translation files. So I have started using a very simple method, which seems to work very well:
For example, I have a site in English and Polish. I do put the texts in both languages into my templates like this:
Code: |
[...]
<body>
<h1>{en}Welcome!{/en}{pl}Witamy!{/pl}</h1>
[...]
|
In the application code, I define language like this:
Code: |
$smarty->assign('lang', 'en');
|
And I have defined two block functions to handle the language versions, they simply ignore the content if it is not in the specified language or display it:
Code: |
function smarty_block_en($params, $content, &$smarty, &$repeat) {
if ($smarty->get_template_vars('lang') == 'en') {
return $content;
} else {
$repeat = false;
}
}
function smarty_block_pl($params, $content, &$smarty, &$repeat) {
if ($smarty->get_template_vars('lang') == 'pl') {
return $content;
} else {
$repeat = false;
}
}
|
By using $repeat=false I make sure that any smarty code will not be executed withing those language tags which do not correspond to the chosen language, so I presume this should work pretty fast. Generally, this simply works like {if lang='xx'}{/if} statements but requires less typing.
This method also allows me to easily implement language display logic, for example different date/number formatting depending on the language version because within the language tags {en}{/en} or {pl}{/pl} I can insert any smarty code that I want. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|