Giter VIP home page Giter VIP logo

Comments (8)

justvanrossum avatar justvanrossum commented on August 12, 2024 1

A warning sounds useful. Are these strings case sensitive or not? If not: make sure to take that into account when checking.

from drawbot.

typemytype avatar typemytype commented on August 12, 2024

for now there is already a warning when a language is set which is not available: *** DrawBot warning: Language 'foo' has no hyphenation available. *** (only while drawing text)

language("foo")
hyphenation(True)
text("bar", (10, 10))

a check is maybe a bit more complex then just looking if the language tag is in that list, fe: nl-be or fr-CA, this is the full spec: language-extlang-script-region-variant-extension-privateuse see

so language() could draw some dummy text somewhere that produce this warning..

from drawbot.

typemytype avatar typemytype commented on August 12, 2024

had to look it up, this could be added while setting language:

def _checkLanguageHyphenation(self):

from drawbot.

justvanrossum avatar justvanrossum commented on August 12, 2024

had to look it up, this could be added while setting language:

But that checks whether there is hyphenation for that language, which may be distinct from "we have a valid language".

from drawbot.

typemytype avatar typemytype commented on August 12, 2024

this list all possible options for language(..)

import AppKit
print(AppKit.NSLocale.availableLocaleIdentifiers())

from drawbot.

justvanrossum avatar justvanrossum commented on August 12, 2024

It may have to be something in this direction:

import AppKit

def isLanguageCodeOk(langCode):
    parsedLoc = AppKit.NSLocale.componentsFromLocaleIdentifier_(langCode)
    lang = parsedLoc[AppKit.kCFLocaleLanguageCodeKey]
    country = parsedLoc.get(AppKit.kCFLocaleCountryCodeKey)
    return lang in AppKit.NSLocale.ISOLanguageCodes() and (
        country is None or country in AppKit.NSLocale.ISOCountryCodes()
    )

for langCode in ["ab", "abk", "abz", "AB", "en-us", "en_us", "EN-US", "en-zz"]:
    print(langCode, isLanguageCodeOk(langCode))

Output:

ab True
abk True
abz False
AB True
en-us True
en_us True
EN-US True
en-zz False

from drawbot.

typemytype avatar typemytype commented on August 12, 2024

looks good!

strange all en_us, en-us and EN-US are valid

from drawbot.

justvanrossum avatar justvanrossum commented on August 12, 2024

Or maybe something like this after all:

import AppKit


def canonicalLocaleCode(localeCode):
    parsedLoc = AppKit.NSLocale.componentsFromLocaleIdentifier_(localeCode)
    parts = [
        parsedLoc[AppKit.kCFLocaleLanguageCode],
        parsedLoc.get(AppKit.kCFLocaleScriptCode),
        parsedLoc.get(AppKit.kCFLocaleCountryCode),
    ]
    return "_".join(part for part in parts if part)


def isLanguageCodeOk(localeCode):
    localeCode = canonicalLocaleCode(localeCode)
    return localeCode in AppKit.NSLocale.availableLocaleIdentifiers()


for langCode in [
    "ab",
    "abk",
    "abz",
    "AB",
    "en-us",
    "en_us",
    "EN-US",
    "en-zz",
    "sr-cyrl-me",
    "en",
]:
    print(langCode, isLanguageCodeOk(langCode))

Output:

ab False
abk False
abz False
AB False
en-us True
en_us True
EN-US True
en-zz False
sr_Cyrl_ME True
en True

from drawbot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.