Jump to content
Forgot your email address? ×
×
×
  • Create New...

PY Get Initial Lang


Recommended Posts

M2 Download Center

Hidden Content

    Give reaction to this post to see the hidden content.
( Internal )

 

Hello, i maked a simple script for get initial lang... the proposite is automate the choice of the languaje (for servers internationals).

Unknown if there will be something similar

Source:

import requests
 
class CurrentPositionForLang:

    # Code langs
    __LANGS_AVAILABLES = ("ES", "EN", "FR", "TR",)

    # County with lang
    __COUNTRY_CODES_WITH_LANG = (
        ("MX", __LANGS_AVAILABLES[0]),
        ("US", __LANGS_AVAILABLES[1]),
        ("FR", __LANGS_AVAILABLES[2]),
        ("ES", __LANGS_AVAILABLES[0]),
        ("TR", __LANGS_AVAILABLES[3]) 
    )

    @staticmethod
    def get_lang():
        lang = "US" # Default lang for errors
        try:
            r = requests.get("http://ip-api.com/json/")

            if r.status_code != 200:
                return lang

            countryCode = r.json()["countryCode"]

            for country, langCountry in CurrentPositionForLang.__COUNTRY_CODES_WITH_LANG:
                if countryCode == country:
                    return langCountry

            # If not finded return default
            return lang
        except requests.exceptions.ConnectionError:
            return lang
            

For utilicy this class only call this method:

CurrentPositionForLang.get_lang()

this return only String "US, ES..."

 

If you need add more langs, in this fields add the country and the code.

    __LANGS_AVAILABLES = ("ES", "EN", "FR", "TR",)

    # County with lang
    __COUNTRY_CODES_WITH_LANG = (
        ("MX", __LANGS_AVAILABLES[0]),
        ("US", __LANGS_AVAILABLES[1]),
        ("FR", __LANGS_AVAILABLES[2]),
        ("ES", __LANGS_AVAILABLES[0]),
        ("TR", __LANGS_AVAILABLES[3])
    )

This is the standar for code countrys:

https://www.ncbi.nlm.nih.gov/books/NBK7249/

  • Love 2
  • Confused 1
Link to comment

Who is the request.py for the libs? But nice idea 

 

import locale 

pprint({"Default locale": locale.getdefaultlocale(), "Default locale data": locale.localeconv()['int_curr_symbol']})
print("")
print({"Current locale": locale.getlocale(), "Current locale data": locale.localeconv()['int_curr_symbol']})
print("")

 

Link to comment
11 hours ago, mesterlum said:

i do not understand buddy, if you refer for "requests" is a module from python API. But, my idea is establish initial lang automatic.

Yes I have a Compiler for my phone and the app have many modules but not this sorry for my mistake :)

Link to comment
  • Moderator
Quote

If you need add more langs, in this fields add the country and the code.

    __LANGS_AVAILABLES = ("ES", "EN", "FR", "TR",)

    # County with lang
    __COUNTRY_CODES_WITH_LANG = (
        ("MX", __LANGS_AVAILABLES[0]),
        ("US", __LANGS_AVAILABLES[1]),
        ("FR", __LANGS_AVAILABLES[2]),
        ("ES", __LANGS_AVAILABLES[0]),
        ("TR", __LANGS_AVAILABLES[3])
    )

 

You can do something like that.

  • root/localeInfo.py
Spoiler
#Search for:
MODE_NAME_LIST = ( PVP_OPTION_NORMAL, PVP_OPTION_REVENGE, PVP_OPTION_KILL, PVP_OPTION_PROTECT, )
#Add after:
LOCALE_REGION_DICT = {
	'af' : ('za',),
	'am' : ('et',),
	'ar' : ('ae', 'bh', 'dz', 'eg', 'iq', 'jo', 'kw', 'lb', 'ly', 'ma', 'om', 'qa', 'sa', 'sy', 'tn', 'ye'),
	'be' : ('by',),
	'bg' : ('bg',),
	'bn' : ('bd',),
	'cs' : ('cz',),
	'da' : ('dk',),
	'de' : ('at', 'ch', 'de', 'li', 'lu'),
	'dv' : ('mv',),
	'el' : ('gr',),
	'en' : ('au', 'bz', 'ca', 'gb', 'ie', 'jm', 'my', 'nz', 'sg', 'tt', 'us', 'za', 'zw'),
	'es' : ('ar', 'bo', 'cl', 'co', 'cr', 'do', 'ec', 'es', 'gt', 'hn', 'mx', 'ni', 'pa', 'pe', 'pr', 'py', 'sv', 'us', 'uy', 've'),
	'et' : ('ee',),
	'fa' : ('ir',),
	'fi' : ('fi',),
	'fil' : ('ph',),
	'fo' : ('fo',),
	'fr' : ('be', 'ca', 'ch', 'fr', 'lu', 'mc'),
	'he' : ('il',),
	'hi' : ('in',),
	'hr' : ('ba', 'hr'),
	'hu' : ('hu',),
	'hy' : ('am',),
	'id' : ('id',),
	'ig' : ('ng',),
	'is' : ('is',),
	'it' : ('ch', 'it'),
	'ja' : ('jp',),
	'ka' : ('ge',),
	'kk' : ('kz',),
	'kl' : ('gl',),
	'km' : ('kh',),
	'ko' : ('kr',),
	'ky' : ('kg',),
	'lb' : ('lu',),
	'lo' : ('la',),
	'lt' : ('lt',),
	'lv' : ('lv',),
	'mi' : ('nz',),
	'mk' : ('mk',),
	'mn' : ('mn',),
	'ms' : ('bn', 'my'),
	'mt' : ('mt',),
	'nb' : ('no',),
	'ne' : ('np',),
	'nl' : ('be', 'nl'),
	'pl' : ('pl',),
	'prs' : ('af',),
	'ps' : ('af',),
	'pt' : ('br', 'pt'),
	'ro' : ('ro',),
	'ru' : ('ru',),
	'rw' : ('rw',),
	'si' : ('lk',),
	'sk' : ('sk',),
	'sl' : ('si',),
	'sq' : ('al',),
	'sv' : ('se',),
	'sw' : ('ke',),
	'th' : ('th',),
	'tk' : ('tm',),
	'tr' : ('tr',),
	'uk' : ('ua',),
	'ur' : ('pk',),
	'vi' : ('vn',),
	'wo' : ('sn',),
	'yo' : ('ng',),
	'zh' : ('cn', 'hk', 'mo', 'sg', 'tw'),
}

 

  • Add at end of line:
import requests
def GetLanguage(locale_region_default = 'en'):
	language = locale_region_default
	try:
		request = requests.get('http://ip-api.com/json/', params=None)
		if request.status_code is 200:
			for locale_region, country_code_tuple in LOCALE_REGION_DICT.iteritems():
				if request.json().get('countryCode').lower() in country_code_tuple:
					language = locale_region

		return language
	except requests.exceptions.ConnectionError:
		return language

# Initialized just one time
LANGUAGE = GetLanguage()

How to use it:

import localeInfo
print localeInfo.LANGUAGE

 

  • Love 2
Link to comment
hace 3 horas, VegaS™ dijo:

You can do something like that.

  • root/localeInfo.py
  Reveal hidden contents

#Search for:
MODE_NAME_LIST = ( PVP_OPTION_NORMAL, PVP_OPTION_REVENGE, PVP_OPTION_KILL, PVP_OPTION_PROTECT, )
#Add after:
LOCALE_REGION_DICT = {
	'af' : ('za',),
	'am' : ('et',),
	'ar' : ('ae', 'bh', 'dz', 'eg', 'iq', 'jo', 'kw', 'lb', 'ly', 'ma', 'om', 'qa', 'sa', 'sy', 'tn', 'ye'),
	'be' : ('by',),
	'bg' : ('bg',),
	'bn' : ('bd',),
	'cs' : ('cz',),
	'da' : ('dk',),
	'de' : ('at', 'ch', 'de', 'li', 'lu'),
	'dv' : ('mv',),
	'el' : ('gr',),
	'en' : ('au', 'bz', 'ca', 'gb', 'ie', 'jm', 'my', 'nz', 'sg', 'tt', 'us', 'za', 'zw'),
	'es' : ('ar', 'bo', 'cl', 'co', 'cr', 'do', 'ec', 'es', 'gt', 'hn', 'mx', 'ni', 'pa', 'pe', 'pr', 'py', 'sv', 'us', 'uy', 've'),
	'et' : ('ee',),
	'fa' : ('ir',),
	'fi' : ('fi',),
	'fil' : ('ph',),
	'fo' : ('fo',),
	'fr' : ('be', 'ca', 'ch', 'fr', 'lu', 'mc'),
	'he' : ('il',),
	'hi' : ('in',),
	'hr' : ('ba', 'hr'),
	'hu' : ('hu',),
	'hy' : ('am',),
	'id' : ('id',),
	'ig' : ('ng',),
	'is' : ('is',),
	'it' : ('ch', 'it'),
	'ja' : ('jp',),
	'ka' : ('ge',),
	'kk' : ('kz',),
	'kl' : ('gl',),
	'km' : ('kh',),
	'ko' : ('kr',),
	'ky' : ('kg',),
	'lb' : ('lu',),
	'lo' : ('la',),
	'lt' : ('lt',),
	'lv' : ('lv',),
	'mi' : ('nz',),
	'mk' : ('mk',),
	'mn' : ('mn',),
	'ms' : ('bn', 'my'),
	'mt' : ('mt',),
	'nb' : ('no',),
	'ne' : ('np',),
	'nl' : ('be', 'nl'),
	'pl' : ('pl',),
	'prs' : ('af',),
	'ps' : ('af',),
	'pt' : ('br', 'pt'),
	'ro' : ('ro',),
	'ru' : ('ru',),
	'rw' : ('rw',),
	'si' : ('lk',),
	'sk' : ('sk',),
	'sl' : ('si',),
	'sq' : ('al',),
	'sv' : ('se',),
	'sw' : ('ke',),
	'th' : ('th',),
	'tk' : ('tm',),
	'tr' : ('tr',),
	'uk' : ('ua',),
	'ur' : ('pk',),
	'vi' : ('vn',),
	'wo' : ('sn',),
	'yo' : ('ng',),
	'zh' : ('cn', 'hk', 'mo', 'sg', 'tw'),
}

 

  • Add at end of line:
import requests
def GetLanguage(locale_region_default = 'en'):
	language = locale_region_default
	try:
		request = requests.get('http://ip-api.com/json/', params=None)
		if request.status_code is 200:
			for locale_region, country_code_tuple in LOCALE_REGION_DICT.iteritems():
				if request.json().get('countryCode').lower() in country_code_tuple:
					language = locale_region

		return language
	except requests.exceptions.ConnectionError:
		return language

# Initialized just one time
LANGUAGE = GetLanguage()

How to use it:

import localeInfo
print localeInfo.LANGUAGE

 

good job my friend,  and thanks for complete the script for clients.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Announcements

  • -15% Discount in Metin2 Dev Store (01/10/2021 => 31/12/2021) => Code => 15_PCT_OCT_TO_DEC_2021


Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.