@google/clasp
Version:
Develop Apps Script Projects locally
65 lines (64 loc) • 2.39 kB
JavaScript
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file sets up internationalization (i18n) for the clasp CLI using
// @formatjs/intl. It detects the user's locale and provides an `intl`
// object for formatting localized messages.
import { createIntl, createIntlCache } from '@formatjs/intl';
import Debug from 'debug';
const debug = Debug('clasp:intl');
function isDefined(item) {
return item !== null && item !== undefined;
}
function getLocale() {
const envLocales = [process.env.LC_ALL, process.env.LC_CTYPE, process.env.LANG].filter(isDefined);
for (const envLocale of envLocales) {
try {
// Attempt to normalize the locale string (e.g., "en_US" to "en-US")
const normalizedLocale = new Intl.Locale(envLocale).toString();
return normalizedLocale;
}
catch (_error) {
// Ignore invalid locale strings and try the next one
debug('Invalid locale string: %s', envLocale);
}
}
return 'en';
}
function loadMessages(_locale) {
// TODO - L10N not implemented yet.
return {};
}
const cache = createIntlCache();
const locale = getLocale();
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
debug('Using locale: %s', locale);
/**
* Internationalization instance configured with the user's detected locale
* (or 'en' as default). This object is used for formatting localized messages,
* dates, numbers, etc., throughout the application.
* It is created using `@formatjs/intl`.
*
* Example usage:
* ```
* import {intl} from './intl.js';
* const message = intl.formatMessage({defaultMessage: "Hello, world!"});
* ```
*/
export const intl = createIntl({
// Locale of the application
locale,
timeZone: localTimeZone,
defaultLocale: 'en',
messages: loadMessages(locale),
}, cache);