You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script本地化开发:如何获取完整的语言及国家代码

Getting Full Language-Region Locale for Google Workspace Add-On Localization

Great question—this is a common pain point when building localized Google Workspace add-ons, since Session.getActiveUserLocale() only returns a language code (like en) instead of the full language-region identifier (like en-US) that matches the user's actual Sheets display language settings.

Here are three actionable solutions to get the complete locale you need:

1. Retrieve Full Locale via the Drive API

The Drive API's About resource returns a userLocale field that includes both language and region code (e.g., en-US, es-ES). This is the most reliable method because it pulls directly from the user's Google account settings.

To implement this:

  • First, enable the Drive API in your Apps Script project: Go to Services > Add a service > Select Drive API > Click Add.
  • Then use this function to fetch the full locale:
    function getFullUserLocale() {
      try {
        const driveAbout = Drive.About.get();
        return driveAbout.userLocale; // Returns format like "en-US"
      } catch (error) {
        // Handle authorization or API errors
        console.error("Failed to fetch locale:", error);
        return Session.getActiveUserLocale(); // Fallback to basic language code
      }
    }
    
    Note: Users will need to grant your add-on permission to access their Drive settings the first time this runs—make sure to explain this in your add-on's authorization prompt to avoid confusion.

2. Fallback to Session.getScriptLocale() (Context-Dependent)

If your add-on is bound to a specific Google Sheet (not a standalone web app), Session.getScriptLocale() sometimes returns the full language-region locale of the document's owner. However, this is not guaranteed to match the current user's settings, so it should only be used as a fallback if the Drive API method isn't feasible.

Example:

function getLocaleFallback() {
  const userLocale = Session.getActiveUserLocale();
  if (userLocale.length === 2) {
    return Session.getScriptLocale(); // May return "en-US" instead of just "en"
  }
  return userLocale;
}

3. Prompt the User for Region (Last Resort)

If the above methods don't work for your use case, you can build a simple UI prompt to ask users to select their region when you detect a 2-letter language code. Store their selection using PropertiesService so you don't have to ask every time.

Example flow:

  • Check if Session.getActiveUserLocale() is a 2-letter code (e.g., en).
  • If yes, show a dialog with region options (e.g., "English (US)", "English (UK)", "English (AU)").
  • Save the selected full locale (e.g., en-US) to PropertiesService.getUserProperties().
  • Use this saved value for all future localization.

Key Notes

  • The Drive API method is the most accurate, but requires additional authorization.
  • Always include a fallback to Session.getActiveUserLocale() in case of API errors or permission denials.
  • Test across different user settings to ensure your implementation works for locales like fr-CA, pt-BR, etc.

内容的提问来源于stack exchange,提问作者Putta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 15:32:43