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