How to change language names?

Started by FishingManMatt, May 10, 2015, 01:41:06 PM

Previous topic - Next topic

FishingManMatt

Hello!

In the user's profile settings one can change the language for the board.
I have two language packs installed in my SMF (English and my native). The names of both languages are displayed in English in the list.
Is it possible to change the name of my native language in the list from English to my native language?

Best regards,
Matt

Steve

Does anyone know if he can do this? :)
DO NOT pm me for support!

Suki

#2
It has been requested before, to be able to show that language's name in the actual language, can't remember where is this discussion though.



The actual name comes straight from the php files and changing that may result in your forum getting all borked :(

So a quick and dirty hack is needed, open your Sources/Load.php file and find this:

// Remove any duplicates.
$language_directories = array_unique($language_directories);


And below add this:

// A list of languages.
$nativeName = array(
'English' => 'English',
'NativeLanguage' => 'NativeLanguage',
);


And replace the "Native Lnaguage" on the left with the "english" name of your language and replace the right entry with the "native" entry, for example, for Spanish it will look like this:

$nativeName = array(
'English' => 'English',
'Spanish' => 'Español',
);



Then find this:

$context['languages'][$matches[1]] = array(
'name' => $smcFunc['ucwords'](strtr($matches[1], array('_' => ' '))),
'selected' => false,
'filename' => $matches[1],
'location' => $language_dir . '/index.' . $matches[1] . '.php',
);


And replace it with this:

$name = $smcFunc['ucwords'](strtr($matches[1], array('_' => ' ')));

$context['languages'][$matches[1]] = array(
'name' => !empty($nativeName[$name]) ? $nativeName[$name] : $name,
'selected' => false,
'filename' => $matches[1],
'location' => $language_dir . '/index.' . $matches[1] . '.php',
);


That should do it.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

margarett

Note: I was hugely ninja'd but this was already written. So here it goes nevertheless ;D Use Suki's post, much better idea :)



'k, so........

It is definitely possible and really easy. But it comes with some... bumps :P on the road ;)

SMF recognizes the name of the language by the name of index.{language}.php.
The "locale", if existant, is the letters after the underscore. Eg: portuguese_br, portuguese_pt. Both are "portuguese", yet there are 2 different "locales".
After the language name is identified (though index........), all other files are required with the same "pattern".

So if you want to rename your language, just rename all your language-related files. BUT, BUT BUT BUT ;D
* You need to delete the cache file for the "known languages" so that SMF rebuilds it with the new names.
* Your user's language selection keeps the file names. Ohhhh problem... So if you changed your file names from, eg, "portuguese", to "português", your users will loose the language selection... So you should first remember how the file names were, then do the rename thang, and finally run the following query in phpmyadmin:
UPDATE smf_members SET lngfile = 'new_name' WHERE lngfile = 'old_name';
(where, of course, new_name and old_name are actually the proper names)
* Last but not least, if your language has "strange" characters (eg, "português" has that "ê"), you might be very careful when doing this... Your filesystem might or might not like these characters and be completely unable to open them.

Of course, you will perform a good backup before you start any of this :P
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

FishingManMatt

Thank you Suki and thank you margarett! :)
Seems both solutions should work.
Although it will probably be much easier for me to create a custom package/mod using the Suki's suggestion.

Once again, many thanks for your answers! :)

FishingManMatt

I've just implemented the solution described by Suki. Unfortunately, nothing changed :( All language names in the profile settings are in English as they were before.

Kindred

Did you clear your browser and your server cache?
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

FishingManMatt

Quote from: Kindred on May 13, 2015, 06:43:20 PM
Did you clear your browser and your server cache?
Yep, I cleared my browser cache and used the internal SMF tool for clearing the cache file.
Maybe I should clear something in the database too?

Suki

Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

FishingManMatt

Quote from: Suki on May 14, 2015, 07:46:33 AM
Post the code you changed.
Many thanks for your support Suki!

The whole code from the Load.php file (generated by the mod I created) looks as follows:


// We possibly have a base theme directory.
if (!empty($settings['base_theme_dir']))
$language_directories[] = $settings['base_theme_dir'] . '/languages';

// Remove any duplicates.
$language_directories = array_unique($language_directories);

// A list of languages.
$nativeName = array(
'English' => 'English',
'Polish' => 'Polski',
);

foreach ($language_directories as $language_dir)
{
// Can't look in here... doesn't exist!
if (!file_exists($language_dir))
continue;

$dir = dir($language_dir);
while ($entry = $dir->read())
{
// Look for the index language file....
if (!preg_match('~^index\.(.+)\.php$~', $entry, $matches))
continue;

$name = $smcFunc['ucwords'](strtr($matches[1], array('_' => ' ')));

$context['languages'][$matches[1]] = array(
'name' => !empty($nativeName[$name]) ? $nativeName[$name] : $name,
'selected' => false,
'filename' => $matches[1],
'location' => $language_dir . '/index.' . $matches[1] . '.php',
);

}
$dir->close();
}

// Favoring UTF8? Then prevent us from selecting non-UTF8 versions.
                ...

Suki

Are you using an utf8 language?  if so, add "-utf8" to the "English" name, like this:

// A list of languages.
$nativeName = array(
'English' => 'English',
'Polish-utf8' => 'Polski',
);
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

FishingManMatt

Thank you!
Indeed, I'm using UTF-8!
Now, after adding the "-utf8" i can see the correct name "Polski" in the list of languages in the account settings. But... after saving the settings it is still displayed as "Polish" on the Profile page.

Suki

yay for multiple places doing the same!

Open your Sources/Load.php find this:

'language' => $smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => ''))),


And change it to this:

'language' => !empty($context['nativeName'][$smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => '')))]) ? $context['nativeName'][$smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => '')))] : $smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => ''))),


Find this:

$nativeName = array(
'English' => 'lol',
'Spanish Latin' => 'Español Latino',
);



And change it to this:

$context['nativeName'] = array(
'English' => 'lol',
'Spanish Latin' => 'Español Latino',
);
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

FishingManMatt

Ah, thank you Suki :)

I'm not a PHP expert but to make it work I had to move the nativeName array up in the code so that it is declared before the first occurrence of this variable.

I also had to change this (from your first post):
'name' => !empty($nativeName[$name]) ? $nativeName[$name] : $name,

to this (I hope the code is correct :) ):
'name' => !empty($context['nativeName'][$name]) ? $context['nativeName'][$name] : $name,

-------------------

Now, the only problem is that when the nativeName array includes the "-uft8" the name is correct in the language list but incorrect on the profile page. When I remove the "-utf8" from the array then the name is correct on the profile page list but incorrect in the language list.

Suki

Yeah, forgot to modify that part.

Just add another entry on your array, one for utf8 and another without it.


$context['nativeName'] = array(
'English' => 'lol',
'Spanish Latin' => 'Español Latino',
'Spanish Latin-utf8' => 'Español Latino',
);
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

FishingManMatt

 :)
That's it!
Once again, many thanks for your advices!

FishingManMatt

I forgot to mention...
Not a big deal but...

When a user doesn't make any changes in his account settings and doesn't use the Save button, then there is no info about his preferred language (even though he then uses the default language). Only when he uses the Save button, the language code is saved to the database (smf_members -> langfile) and displayed on the profile page.

In other words, you may have no info about preferred language in your profile but when you use the Save button the info will always be there and you are not able to switch it off. So this is a kind of inconsistency.

IMHO the lang info should always be displayed. If the database cell is empty, SMF should display the default forum language.
Or the user should be able to decide whether he wants this info to be shown on his profile page. But this would probably be more complicated to implement.

Kindred

well, SMF - in order to conserve database space, amongst other reasons, does not record profile settings, if the user never changes them from the system default....   it only records the setting for users who have changed that specific setting at least once.

There will be some users who have changed, and then changed BACK to the options which is the system default... and THEY will have records... but any user who has never changed that specific setting will not have a record.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

braemt

I have tried using the mods in this thread.

These are the things I've added/edited in Load.php, but without effect:

// A list of languages.
$context['nativeName'] = array(
'English' => 'English',
'Dutch-utf8' => 'Nederlands',
'French-utf8' => 'Français',
'Dutch' => 'Nederlands',
'French' => 'Français',
);



'language' => !empty($context['nativeName'][$smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => '')))]) ? $context['nativeName'][$smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => '')))] : $smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => ''))),



$name = $smcFunc['ucwords'](strtr($matches[1], array('_' => ' ')));

$context['languages'][$matches[1]] = array(
'name' => !empty($context['nativeName'][$name]) ? $context['nativeName'][$name] : $name,
'selected' => false,
'filename' => $matches[1],
'location' => $language_dir . '/index.' . $matches[1] . '.php',
);


Am I doing something wrong?

Kind regards,
Braemt

FishingManMatt

Hi braemt!

Take a look at the mod I created. It works as expected in my CMS (2.0.11).

Advertisement: