RE: Bet your money and achieve your goal.

You are viewing a single comment's thread from:

Bet your money and achieve your goal.

in hive-175254 •  5 years ago 

I checked out your site. It needs a bit more translation work. There is also the ability and standard for browsers to communicate the language preferences of its users.

You can use the following code to determine the language preferences of your users in a CGI -script . Although this could be adapted to Javascript. If you run Apache, you can bi-pass the need for the code completely.
Here is Python code for detecting language:

# Takes a supported_language tuple and an accepted_language string (or None) and determines the language to use. 
# If None is supplied as an accepted_language string, the environment variable HTTP_ACCEPT_LANGUAGE is used.
# Returns a two-character language string which is the best supported language found in the accepted_language string.
# The supported_language tuple must be a list of two character iso strings for the language.  Language codes in this variable with the country codes are not supported.
# The accept_language string may contain country codes such as 'en-ZA'
# 
# Exceptions:
# If nothing in the accepted_language string is supported, an Exception is raised. 
# If the HTTP_ACCEPT_LANGUAGE environment variable does not exist, a KeyError is raised. 
#
# Nota Bene: There is no differentiation between en-CA or en-ZA to this routine since it will only return the language part 'en'.
# Nota Bene: The Web server will set the HTTP_ACCEPT_LANGUAGE environment variable according to what the user-agent sets as a header if it exists.  Certain automated software will not set the header, so the caller of negotiate_language must handle the KeyError exception in a way that displays something to the robot rather than a 500 error. 
#
# Example supported_language vector are as follows:
#   *   ['en']
#   *   ['cn', 'en', 'ru']
#   *   ['en', 'es', 'fr', 'it']
#
# Example accept_language strings are as follows:
#
#   * 'en-US'
#   * 'ru, uk;q=0.8, be;q=0.8, en;q=0.7, *;q=0.01'
#   * 'en,fr-CA;q=0.8,fr;q=0.6,pl;q=0.4,en-ZA;q=0.2,es;q=0.2,de;q=0.2,en-GB;q=0.2,en-US;q=0.2'
#   * 'ru-RU'


def negotiate_language(supported_languages, accept_language = None):
    verbosity = 0
    if accept_language is None:
        accept_language = os.environ['HTTP_ACCEPT_LANGUAGE']
    best_lang = None
    best_score = None
    alh = {}
    for pair in accept_language.split(','):
        lang_pri_exp = pair.split(';')
        lang = lang_pri_exp[0].split('-')[0]
        lang = lang.replace(' ','')
        if len(lang_pri_exp) == 1:
           alh[lang] = decimal.Decimal(1)
        else:
           
           try:
               lang_pri = lang_pri_exp[1].split('=')
               pri = decimal.Decimal(lang_pri[1])
               if lang not in alh or alh[lang] < pri:
                   alh[lang] = pri
           except decimal.InvalidOperation as e:
               raise Exception("Unable to parse ", lang_pri_exp[1], ".  ",  e.args[0])
    if verbosity:
        print(accept_language)
        print(alh)
    best_lang = None
    
    priority_language_hash = {}
    
    for language in alh:
        if alh[language] in priority_language_hash:
            priority_language_hash[alh[language]] += [language]
        else:
            priority_language_hash[alh[language]] = [language]
    if verbosity:    
        print(priority_language_hash)
    
    for sl in supported_languages:
        if sl in alh:
            if best_score is None or alh[sl] > best_score:
                best_lang = sl
                best_score = alh[sl]
    if best_lang is None:
        raise Exception("Could not negotiate a language from the accepted language header")
    return best_lang
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you.
I will apply user language preference.