OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / johngb / langreg / langreg.go
1 // Copyright 2014 John G. Beckett. All rights reserved.
2 // Use of this source code is governed by the MIT
3 // license that can be found in the LICENSE file.
4
5 // Package langreg is a library for validating ISO 639-1 language
6 // and ISO 1366-1_alpa-2 region codes.
7 //
8 // ISO 639-1 language codes are two charcters long and use only lowercase ASCII
9 // character a-z. E.g.:
10 //
11 //      "en", "es", "ru"
12 //
13 // ISO 1366-1_alpa-2 region codes are two charcters long and use only uppercase
14 // ASCII character A-Z. E.g.:
15 //
16 //      "US", "UK", "ZA"
17 //
18 // When combined as a composite language and region code, they are concatented
19 // with an underscore. E.g.:
20 //
21 //      "en_US", "en_ZA", "fr_FR"
22 //
23 // Any codes not meeting these formatting requirement will fail validation.
24 package langreg
25
26 // IsValidLangRegCode returns true if the string s is a valid ISO 639-1 language
27 // and ISO1366-1_alpa-2 region code separated by an underscore.  E.g. "en_US".
28 func IsValidLangRegCode(s string) bool {
29
30         // all valid codes are 5 characters long
31         if len(s) != 5 {
32                 return false
33         }
34
35         // the middle (third) character must be a '_' char
36         if s[2] != '_' {
37                 return false
38         }
39
40         // check the language code, which should be the first two characters in s
41         if !IsValidLanguageCode(s[:2]) {
42                 return false
43         }
44
45         // check the region code, which should be the last two characters in s
46         if !IsValidRegionCode(s[3:]) {
47                 return false
48         }
49         return true
50 }