001/* 002 * Copyright 2021 Siroshun09 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.github.siroshun09.mcmessage.util; 018 019import org.jetbrains.annotations.Nullable; 020 021import java.util.Locale; 022 023public final class LocaleParser { 024 025 private LocaleParser() { 026 throw new UnsupportedOperationException(); 027 } 028 029 public static @Nullable Locale parse(String str) { 030 if (str == null || str.isEmpty()) { 031 return null; 032 } 033 034 String[] segments = str.split("_", 3); 035 int length = segments.length; 036 037 if (length == 1) { 038 return new Locale(str); // language 039 } 040 041 if (length == 2) { 042 return new Locale(segments[0], segments[1]); // language + country 043 } 044 045 if (length == 3) { 046 return new Locale(segments[0], segments[1], segments[2]); // language + country + variant 047 } 048 049 return null; 050 } 051}