News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Show Specified Language

Started by dougiefresh, April 04, 2016, 06:01:14 PM

Previous topic - Next topic

dougiefresh

Link to Mod



SHOW SPECIFIED LANGUAGE v2.4
By Dougiefresh -> Link to Mod



Introduction
This mod allows a user to make a SINGLE post in multiple language and show ONLY the appropriate language to the visiting user.

For example, if a user posted this:
Quote
[language=EN]Hi, this is a test[/language]
[language=EE]Tere, see on test[/language]
[language=ES]Hola, esta es una prueba[/language]
[language=english]Hi, this is a test[/language]
[language=estonian]Tere, see on test[/language]
[language=spanish]Hola, esta es una prueba[/language]
If the user had the Estonian language set, he/she would see this:
Quote
Tere, see on test
Tere, see on test
Either the 2-character language code (ex: en) or the 5-character code (ex: en-US) may be used to specify the language to display.  Note that both en-US and en-GB will be matched to en, likewise for other languages.

If the user's language is not found, the code will either display the preferred default language (if specified) OR the first language code encountered.

Admin Settings
Under Admin => Forum => Posts and Topics => Post Settings, there are 3 new settings:
o Allow sub-languages for language bbcode?
o Show only installed languages for language dropdown?
o Show natural language IDs instead of RFC-1766 IDs?

Related Discussion
o Support for multiple languages!

Translators
o Spanish Latin: Rock Lee

Compatibility Notes
This mod was tested on SMF 2.0.11 and SMF 2.1 RC2, but should work on SMF 2.0 and up.  SMF 1.x is not and will not be supported.

Changelog
The changelog can be viewed at XPtsp.com.

License
Copyright (c) 2016 - 2019, Douglas Orend
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Colin

"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

-Rock Lee-

Great work indeed :D As always I leave the Latin translation spanish ;)

ManageSettings.spanish_latin.php

$txt['SLBBC_Default'] = 'Idioma predeterminado por el <strong>idioma</strong> bbcode:<div class="smalltext">Informaci&oacute;n adicional se puede encontrar <a href="http://www.lingoes.net/en/translator/langcode.htm">Aqu&iacute;</a>!</div>';

ManageSettings.spanish_latin-utf8.php

$txt['SLBBC_Default'] = 'Idioma predeterminado por el <strong>idioma</strong> bbcode:<div class="smalltext">Información adicional se puede encontrar <a href="http://www.lingoes.net/en/translator/langcode.htm">Aquí</a>!</div>';


Regards!
¡Regresando como cual Fenix! ~ Bomber Code
Ayudas - Aportes - Tutoriales - Y mucho mas!!!

4Kstore

Thanks a lots, this is a great mod for multilanguages forums.

¡¡NEW MOD: Sparkles User Names!!!

tehnetman

Hello,
This is a great mod! I was always looking for something like that so my users from different countries don't need to translate or open some additional spoilers to see the message in different language.
BUT, it has one problem. It doesn't work well when using or [list] etc inside it, the bbcodes just start messing up, and you can see [/language] in the post or you can see both language texts etc. Could somebody help me fixing that? I'm a total newb in any form of web development, can only edit some code.
Thanks in advance and sorry for digging up, but this is a great mod and has only this little flaw.

dougiefresh

In order to "deal" with this issue, I would suggest using it like this:
Quote[language=en][code]Yes Yes Yes[/code][/language]
[language=es][code]Si Si Si[/code][/language]
[language=fr][code]Oui Oui Oui[/code][/language]
Maybe I can deal with the situation better in the code, but I'm not sure....

dougiefresh

Quote from: dougiefresh on September 12, 2016, 09:49:13 AM
In order to "deal" with this issue, I would suggest using it like this:
Quote[language=en][code]Yes Yes Yes[/code][/language]
[language=es][code]Si Si Si[/code][/language]
[language=fr][code]Oui Oui Oui[/code][/language]
Maybe I can deal with the situation better in the code, but I'm not sure....
Whoops, I actually tried the solution that I suggested.  Not only do I see what you were talking about, but it's producing errors in the bbcode parser....

dougiefresh

Uploaded v1.1 - September 20th, 2016
o Complete rewrite of bbcode code functions.
o All language sections are now included in the HTML output, except they are hidden.
o All admin options removed from the mod.
o SMF 2.1: Different hook used to perform similar function.




@tehnetman: Hopefully, this solves the problem you were having.  Thank you for the bug report.

UFFENO1

Maybe it's me doing something wrong, but I can not get it to work.
When I try, it is always the first language displayed regardless of the language the user has set.
It seems that it does not check the language the user has set.

dougiefresh

Quote from: UFFENO1 on October 06, 2016, 10:56:30 AM
Maybe it's me doing something wrong, but I can not get it to work.
When I try, it is always the first language displayed regardless of the language the user has set.
It seems that it does not check the language the user has set.
Can you post (or PM) the contents of the post you are using?  If you are just using [language], then yes, that text between the those opening and closing tags will show, regardless of user setting.....

Also, if your user language is set to (let's say) French, and the post has no designated french text, then whatever language is first in the post will be shown to the user....

UFFENO1

This is what i tested with:

[language=fr]Salut, ceci est un test[/language]
[language=se]Hej, detta är ett test[/language]
[language=en]Hi, this is a test[/language]


No matter what language the user had set, it is always the top option shown.

dougiefresh

Sorry, I can't reproduce your bug report.  I just tested it on a clean SMF 2.0.12 install, with the English and French language packs installed.  I saw the English post when English was my user language, and the French post when French was my user language.

tehnetman

I need to confirm that this also happens to me. I've uninstalled the previous version of mod and installed  v1.1 and now only the first language bbcode is shown, the rest is completely ignored regardless of the bbcode. Is there any way to fix it?

dougiefresh

Quote from: tehnetman on November 27, 2016, 02:01:49 PM
I need to confirm that this also happens to me. I've uninstalled the previous version of mod and installed  v1.1 and now only the first language bbcode is shown, the rest is completely ignored regardless of the bbcode. Is there any way to fix it?
I will look into this bug report....  Have some other stuff to try to fix before I can do so, though....

dougiefresh

For the next version, I've added two dropdown boxes to the mod:
1) on the Display screen, so that available language strings in the post can be selected
2) on the Post screen, so the a particular language can be selected and the appropriate bbcode inserted into the post.

Two screenshots of the mod have been included at the bottom of this post.

Opinions?

dougiefresh

Uploaded v2.0 - December 3rd, 2016
o Added admin option of selecting default language to view.
o Added dropdown box in Display.template.php with available languages in post.
o SMF 2.0: Added dropdown box on the Post screen with common language codes (RFC 1766-compliant)...

tehnetman

Love the ability to switch the language when viewing the post (makes it a lot easier to check if I made any mistakes).
Still, unfortunately when swtiching my language in profile the language in post doesn't change.

dougiefresh

Hmmm.... Will look into this report.

bravoure

Quote from: tehnetman on December 08, 2016, 02:48:41 PM
Love the ability to switch the language when viewing the post (makes it a lot easier to check if I made any mistakes).
Still, unfortunately when swtiching my language in profile the language in post doesn't change.

I had the same problem, and also I received[lang=en][/lang] when choosing in the dropbox menu in stead off [language=en][/language].For this last problem I had to replace in editor.js this part of code:

if (!this.bRichTextEnabled)
{
surroundText('[lang=' + sValue + ']', '[/lang]', this.oTextHandle);
oSelectProperties.oSelect.selectedIndex = 0;
}


into this

if (!this.bRichTextEnabled)
{
surroundText('[language=' + sValue + ']', '[/language]', this.oTextHandle);
oSelectProperties.oSelect.selectedIndex = 0;
}


For the first problem: the user preferred language is in the variable $user_info['language'] as english or french-utf8, and so on. As my board in my case has only 4 possible languages and I want to give info in the preferred language of the user in these 4 languages I have changed the code in subs-SOSL.php
function SOSL_langName($lang = false)
{



global $smcFunc;
$codes = array (

'en' => 'English',
'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)',





'af' => 'Afrikaans',





'ar' => 'Arabic',





'ar-ae' => 'Arabic (U.A.E.)',





'ar-bh' => 'Arabic (Kingdom of 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)',





'as' => 'Assamese',





'az' => 'Azerbaijani',





'be' => 'Belarusian',





'bg' => 'Bulgarian',





'bn' => 'Bangla',





'ca' => 'Catalan',





'cs' => 'Czech',





'da' => 'Danish',





'de' => 'German (Germany)',





'de-at' => 'German (Austria)',





'de-ch' => 'German (Switzerland)',





'de-li' => 'German (Liechtenstein)',





'de-lu' => 'German (Luxembourg)',





'div' => 'Divehi',





'el' => 'Greek',





'es' => 'Spanish',





'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-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)',





'et' => 'Estonian',





'eu' => 'Basque (Basque)',





'fa' => 'Persian',





'fi' => 'Finnish',





'fo' => 'Faeroese',





'fr' => 'French (France)',





'fr-be' => 'French (Belgium)',





'fr-ca' => 'French (Canada)',





'fr-ch' => 'French (Switzerland)',





'fr-lu' => 'French (Luxembourg)',





'fr-mc' => 'French (Monaco)',





'gd' => 'Scottish Gaelic',





'gl' => 'Galician',





'gu' => 'Gujarati',





'he' => 'Hebrew',





'hi' => 'Hindi',





'hr' => 'Croatian',





'hu' => 'Hungarian',





'hy' => 'Armenian',





'id' => 'Indonesian',





'is' => 'Icelandic',





'it' => 'Italian (Italy)',





'it-ch' => 'Italian (Switzerland)',





'ja' => 'Japanese',





'ka' => 'Georgian',





'kk' => 'Kazakh',





'kn' => 'Kannada',





'ko' => 'Korean',





'kok' => 'Konkani',





'kz' => 'Kyrgyz',





'lt' => 'Lithuanian',





'lv' => 'Latvian',





'mk' => 'Macedonian (FYROM)',





'ml' => 'Malayalam',





'mn' => 'Mongolian (Cyrillic)',





'mr' => 'Marathi',





'ms' => 'Malay',





'mt' => 'Maltese',





'nb-no' => 'Norwegian (Bokmal)',





'ne' => 'Nepali (India)',





'nl-be' => 'Dutch (Belgium)',





'nl' => 'Dutch (Netherlands)',





'nn-no' => 'Norwegian (Nynorsk)',





'no' => 'Norwegian (Bokmal)',





'or' => 'Odia',





'pa' => 'Punjabi',





'pl' => 'Polish',





'pt' => 'Portuguese (Portugal)',





'pt-br' => 'Portuguese (Brazil)',





'rm' => 'Rhaeto-Romanic',





'ro' => 'Romanian',





'ro-md' => 'Romanian (Moldova)',
'ru' => 'Russian',
'ru-md' => 'Russian (Moldova)',
'sa' => 'Sanskrit',
'sb' => 'Sorbian',
'sk' => 'Slovak',
'sl' => 'Slovenian',





'sq' => 'Albanian',





'sr' => 'Serbian',





'sv' => 'Swedish',
'sv-fi' => 'Swedish (Finland)',
'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',
'vi' => 'Vietnamese',
'xh' => 'Xhosa',
'yi' => 'Yiddish',
'zh' => 'Chinese',
'zh-cn' => 'Chinese (China)',
'zh-hk' => 'Chinese (Hong Kong SAR)',
'zh-mo' => 'Chinese (Macao SAR)',
'zh-sg' => 'Chinese (Singapore)',
'zh-tw' => 'Chinese (Taiwan)',
'zu' => 'Zulu',
);

if (empty($lang))
return $codes;

$lang = $smcFunc['strtolower']($lang);



return isset($codes[$lang]) ? $codes[$lang] : $smcFunc['strtoupper']($lang);
}


into
function SOSL_langName($lang = false)
{
global $smcFunc;
$codes = array (
'english' => 'English',
'german-utf8' => 'German',
'french-utf8' => 'French',
'dutch-utf8' => 'Dutch',
);
if (empty($lang))
return $codes;
$lang = $smcFunc['strtolower']($lang);
return isset($codes[$lang]) ? $codes[$lang] : $smcFunc['strtoupper']($lang);
}




and
function SOSL_prepareDisplayContext(&$output, &$message)
{
global $context, $txt, $modSettings;
static $user_lang = false, $alt_lang;

// Decide what the default viewing language is for the mod:
if (!$user_lang)

{
$user_lang = empty($modSettings['SOSL_Default']) ? $txt['lang_locale'] : $modSettings['SOSL_Default'];
$alt_lang = substr($user_lang, 0, 2);
}

// Find all language bbcodes in the post's message:
$output['SOSL_lang'] = array();
$pattern = '#\<div class="SOSL_([\w\-]+?)"#i' . ($u = ($context['utf8'] ? 'u' : ''));
if (preg_match_all($pattern, $output['body'], $matches, PREG_PATTERN_ORDER))
{

// Determine the unique language codes used:
$matches = array_unique($matches[1]);
$first_lang = $matches[0];
foreach ($matches as $match)
$first_lang = ($match == $user_lang ? $match : (substr($match, 0, 2) == $alt_lang ? $alt_lang : $first_lang));
$output['SOSL_lang'] = $matches;
$output['SOSL_primary'] = $first_lang;
// Replace the "SOSL" with "SOSL_<post_id>" and hide all non-selected languages:

foreach ($matches as $lang)
$output['body'] = str_replace('<div class="SOSL_' . ($context['SOSL_lang'][] = $lang) . '"', '<div class="SOSL_' . $output['id'] . '_' . $lang . '"' . ($first_lang == $lang ? '' : ' style="display: none;"'), $output['body']);
}

return $output;


into

function SOSL_prepareDisplayContext(&$output, &$message)
{
global $context, $txt, $modSettings, $user_info;
static $user_lang = false, $alt_lang;

// Decide what the default viewing language is for the mod:
if (!$user_lang)
{
$user_lang = empty($modSettings['SOSL_Default']) ? $txt['lang_locale'] : $modSettings['SOSL_Default'];
$alt_lang = substr($user_lang, 0, 2);
}

// Find all language bbcodes in the post's message:
$output['SOSL_lang'] = array();
$pattern = '#\<div class="SOSL_([\w\-]+?)"#i' . ($u = ($context['utf8'] ? 'u' : ''));
if (preg_match_all($pattern, $output['body'], $matches, PREG_PATTERN_ORDER))
{
// Determine the unique language codes used:
$matches = array_unique($matches[1]);

$first_lang = $matches[0];
foreach ($matches as $match)
//$first_lang = ($match == $user_lang ? $match : (substr($match, 0, 2) == $alt_lang ? $alt_lang : $first_lang));
$first_lang = ($match == $user_info['language'] ? $match : (substr($match, 0, 2) == $alt_lang ? $alt_lang : $first_lang));
$output['SOSL_lang'] = $matches;
$output['SOSL_primary'] = $first_lang;

// Replace the "SOSL" with "SOSL_<post_id>" and hide all non-selected languages:
foreach ($matches as $lang)
$output['body'] = str_replace('<div class="SOSL_' . ($context['SOSL_lang'][] = $lang) . '"', '<div class="SOSL_' . $output['id'] . '_' . $lang . '"' . ($first_lang == $lang ? '' : ' style="display: none;"'), $output['body']);
}
return $output;
}



dougiefresh

#19
@bravoure:  Thank you for showing me how to fix the mod so that the language button works properly, as well as showing me a more natural way for users to use the language tag!

Quote from: tehnetman on December 08, 2016, 02:48:41 PM
Still, unfortunately when swtiching my language in profile the language in post doesn't change.
This issue has been addressed and will be available in the next version.  Thank you for the bug report!




UPDATE:  I've made changes to the code so that it will show the language that the user has selected, using the code that bravoure showed me in the last post.  Unfortunately, this updated code will probably break old language tags, as the code won't be able to properly identify which section to show to the user.  I am still working on this dilemma....

I've removed the default language option in the mod, as it doesn't REAAALLYY make sense to do it that way.  I've also implemented admin options to (1) show only installed languages to users, (2) remove sub-language options from view, and (3) use natural language names instead of RFC 1766 IDs.  This last option will line up properly with the changes that bravoure made, and should make dealing with language tags using that syntax easier.  Once I figure out how to map the old style tags onto the new form, I'll release the next version....

Advertisement: