001/*
002 * Copyright 2017-2022 Product Mog LLC, 2022-2026 Revetware LLC.
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.lokalized;
018
019import org.jspecify.annotations.NonNull;
020import org.jspecify.annotations.Nullable;
021
022import java.util.Locale;
023import java.util.Map;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * Contract for localized string providers - given a key and optional placeholders, return a localized string.
029 * <p>
030 * Format is {@code "You are missing {{requiredFieldCount}} required fields."}
031 *
032 * @author <a href="https://revetkn.com">Mark Allen</a>
033 */
034public interface Strings extends LocaleMatcher {
035        /**
036         * Gets a localized string for the given key.
037         * <p>
038         * If no localized string is available, the key is returned.
039         *
040         * @param key the localization key, not null
041         * @return a localized string for the key, not null
042         */
043        @NonNull
044        String get(@NonNull String key);
045
046        /**
047         * Gets a localized string for the given key.
048         * <p>
049         * If no localized string is available, the key is returned.
050         *
051         * @param key          the localization key, not null
052         * @param placeholders the placeholders to insert into the string, may be null
053         * @return a localized string for the key, not null
054         */
055        @NonNull
056        String get(@NonNull String key, @Nullable Map<@NonNull String, @Nullable Object> placeholders);
057
058        /**
059         * Vends a {@link Strings} instance builder for the specified fallback locale.
060         * <p>
061         * <pre>{@code  Strings strings = Strings.withFallbackLocale(Locale.forLanguageTag("en"))
062         *              .localizedStringSupplier(() -> LocalizedStringLoader.loadFromClasspath("strings"))
063         *              .localeSupplier(() -> Locale.forLanguageTag("en-GB"))
064         *              .build();
065         * }</pre>
066         *
067         * @param fallbackLocale the fallback locale, not null
068         * @return a builder for a {@link Strings} instance, not null
069         */
070        static DefaultStrings.@NonNull Builder withFallbackLocale(@NonNull Locale fallbackLocale) {
071                requireNonNull(fallbackLocale);
072                return new DefaultStrings.Builder(fallbackLocale);
073        }
074}