Package: localizationkit
localizationkitlocalizationkitis a toolkit for ensuring that your localized strings are the best that they can be.Included are tests for various things such as:Checking that all strings have commentsChecking that the comments don't just match the valueCheck that tokens have position specifiersCheck that no invalid tokens are includedwith lots more to come.Getting startedConfigurationTo use the library, first off, create a configuration file that is in the TOML format. Here's an example:default_language="en"[has_comments]minimum_comment_length=25minimum_comment_words=8[token_matching]allow_missing_defaults=true[token_position_identifiers]always=falseThis configuration file sets thatenis the default language (so this is the language that will be checked for comments, etc. and all tests will run relative to it). Then it sets various settings for each test. Every instance of[something_here]specifies that the following settings are for that test. For example, the testhas_commentswill now make sure that not only are there comments, but that they are at least 25 characters in length and 8 words in length.You can now load in your configuration:fromlocalizationkitimportConfigurationconfiguration=Configuration.from_file("/path/to/config.toml")Localization CollectionsNow we need to prepare the strings that will go in. Here's how you can create an individual string:fromlocalizationkitimportLocalizedStringmy_string=LocalizedString("My string's key","My string's value","My string's comment","en")This creates a single string with a key, value and comment, with its language code set toen. Once you've created some more (usually for different languages too), you can bundle them into a collection:fromlocalizationkitimportLocalizedCollectioncollection=LocalizedCollection(list_of_my_strings)Running the testsAt this point, you are ready to run the tests:importlocalizationkitresults=localizationkit.run_tests(configuration,collection)forresultinresults:ifnotresult.succeeded():print("The following test failed:",result.name)print("Failures encountered:")forviolationinresult.violations:print(violation)Not running the testsSome tests don't make sense for everyone. To skip a test you can add the following to your config file at the root:blacklist=["test_identifier_1","test_identifier_2"]Rule documentationMost tests have configurable rules. If a rule is not specified, it will use the default instead.Some tests are opt in only. These will be marked as such.Comment LinebreaksIdentifier:comment_linebreaksOpt-In:trueChecks that comments for strings do not contain linebreaks. Comments which contain linebreaks can interfere with parsing in other tools such asdotstrings.Comment SimilarityIdentifier:comment_similarityChecks the similarity between a comment and the string's value in the default language. This is achieved viadifflib'sSequenceMatcher. More details can be foundhereConfigurationParameterTypeAcceptable ValuesDefaultDetailsmaximum_similarity_ratiofloatBetween 0 and 10.5Set the maximum similarity ratio between the comment and the string value. The higher the value, the more similar they are. The longer the string the more accurate this will be.Duplicate KeysIdentifier:duplicate_keysChecks that there are no duplicate keys in the collection.ConfigurationParameterTypeAcceptable ValuesDefaultDetailsall_languagesbooleantrueorfalsefalseSet totrueto check that every language has unique keys, not just the default language.Has CommentsIdentifier:has_commentsChecks that strings have comments.Note: Only languages that have Latin style scripts are really supported for the words check due to splitting on spaces to check.ConfigurationParameterTypeAcceptable ValuesDefaultDetailsminimum_comment_lengthintAny integer30Set the minimum allowable length for a comment. Set the value to negative to not check.minimum_comment_wordsintAny integer10Set the minimum allowable number of words for a comment. Set the value to negative to not check.Has ValueIdentifier:has_valueChecks that strings have values. Since any value is enough for some strings, it simply makes sure that the string isn't None/null and isn't empty.ConfigurationParameterTypeAcceptable ValuesDefaultDetailsdefault_language_onlybooleantrueorfalsefalseSet to true to only check the default language for missing values. Otherwise all languages will be checked.Invalid TokensIdentifier:invalid_tokensChecks that all format tokens in a string are valid.Note: This check is not language specific. It only works very broadly.Key LengthIdentifier:key_lengthChecks the length of the keys.Note: By default this test doesn't check anything. It needs to have parameters set to positive values to do anything.ConfigurationParameterTypeAcceptable ValuesDefaultDetailsminimumintAny integer-1Set the minimum allowable length for a key. Set the value to negative to not check.maximumintAny integer-1Set the maximum allowable length for a key. Set the value to negative to not check.Objective-C Alternative TokensIdentifier:objectivec_alternative_tokensOpt-In:trueChecks that strings do not contain Objective-C style alternative position tokens.Objective-C seems to be allows positional tokens of the form%1@rather than%1$@. While not illegal, it is preferred that all tokens between languages are consistent so that tools don't experience unexpected failures, etc.Placeholder token explanationIdentifier:placeholder_token_explanationOpt-In:trueChecks that if a placeholder is used in a string, the comment explicitly explains what it is replaced with.Precondition: Each placeholder in the string and its explanation in comment is expected to followtoken_position_identifiersrule.Swift InterpolationIdentifier:swift_interpolationOpt-In:trueChecks that strings do not contain Swift style interpolation values since these cannot be localized.Token MatchingIdentifier:token_matchingChecks that the tokens in a string match across all languages. e.g. If your English string is "Hello %s" but your French string is "Bonjour", this would flag that there is a missing token in the French string.ConfigurationParameterTypeAcceptable ValuesDefaultDetailsallow_missing_defaultsbooleantrueorfalsefalseDue to the way that automated localization works, usually there will be a default language, and then other translations will come in over time. If a translation is deleted, it isn't always deleted from all languages immediately. Setting this parameter to true will allow any strings in your non-default language to be ignored if that string is missing from your default language.Token Position IdentifiersIdentifier:token_position_identifiersCheck that each token has a position specifier with it. e.g.%sis not allowed, but%1$sis. Tokens can move around in different languages, so position specifiers are extremely important.ConfigurationParameterTypeAcceptable ValuesDefaultDetailsalwaysbooleantrueorfalsefalseIf a string only has a single token, it doesn't need a position specifier. Set this totrueto require it even in those cases.ContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visithttps://cla.opensource.microsoft.com.When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor
contactopencode@microsoft.comwith any additional questions or comments.