Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions RevitGenTest/LanguageManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Globalization;
using System.IO;
using System.Threading;

namespace RevitGenTest
{
/// <summary>
/// Manages language/locale preferences for the addin.
/// Supports zh-CN (Chinese), en (English), and ja (Japanese).
/// </summary>
public static class LanguageManager
{
private static readonly string SettingsDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"RevitGenTest");

private static readonly string SettingsFilePath = Path.Combine(
SettingsDirectory,
"language.txt");

/// <summary>
/// The language codes supported by this addin.
/// </summary>
public static readonly string[] SupportedLanguages = { "zh-CN", "en", "ja" };

/// <summary>
/// Gets the current UI language code.
/// </summary>
public static string CurrentLanguage => Thread.CurrentThread.CurrentUICulture.Name;

/// <summary>
/// Loads the saved language preference and applies it to the current thread.
/// Falls back to the default culture if no preference is saved or if it cannot be loaded.
/// </summary>
public static void LoadLanguagePreference()
{
try
{
if (File.Exists(SettingsFilePath))
{
var language = File.ReadAllText(SettingsFilePath).Trim();
ApplyLanguage(language);
}
}
catch (IOException)
{
// Fall back to system default if loading fails
}
catch (UnauthorizedAccessException)
{
// Fall back to system default if access is denied
}
}

/// <summary>
/// Saves the language preference to disk so it persists across sessions.
/// </summary>
/// <param name="languageCode">The BCP 47 language code to save (e.g. "zh-CN", "en", "ja").</param>
public static void SaveLanguagePreference(string languageCode)
{
try
{
if (!Directory.Exists(SettingsDirectory))
Directory.CreateDirectory(SettingsDirectory);

File.WriteAllText(SettingsFilePath, languageCode);
}
catch (IOException)
{
// Ignore save failures silently
}
catch (UnauthorizedAccessException)
{
// Ignore save failures silently
}
}

/// <summary>
/// Switches the UI language to the specified culture code and saves the preference.
/// </summary>
/// <param name="languageCode">The BCP 47 language code to apply (e.g. "zh-CN", "en", "ja").</param>
public static void SwitchLanguage(string languageCode)
{
ApplyLanguage(languageCode);
SaveLanguagePreference(languageCode);
}

private static void ApplyLanguage(string languageCode)
{
try
{
var culture = new CultureInfo(languageCode);
Thread.CurrentThread.CurrentUICulture = culture;
Properties.Resources.Culture = culture;
}
catch (CultureNotFoundException)
{
// Fall back to default culture if the specified code is not recognized
}
}
}
}
27 changes: 27 additions & 0 deletions RevitGenTest/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions RevitGenTest/Properties/Resources.en.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NoWallsFound" xml:space="preserve">
<value>No walls found in the project.</value>
</data>
<data name="SuccessTitle" xml:space="preserve">
<value>Success</value>
</data>
<data name="FoundWallsMessage" xml:space="preserve">
<value>Found {0} wall(s).</value>
</data>
</root>
70 changes: 70 additions & 0 deletions RevitGenTest/Properties/Resources.ja.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NoWallsFound" xml:space="preserve">
<value>プロジェクトに壁が見つかりませんでした。</value>
</data>
<data name="SuccessTitle" xml:space="preserve">
<value>成功</value>
</data>
<data name="FoundWallsMessage" xml:space="preserve">
<value>{0} 個の壁が見つかりました。</value>
</data>
</root>
9 changes: 9 additions & 0 deletions RevitGenTest/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,13 @@
<data name="CodeList_32px" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\CodeList_32px.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="NoWallsFound" xml:space="preserve">
<value>项目中没有找到任何墙。</value>
</data>
<data name="SuccessTitle" xml:space="preserve">
<value>成功</value>
</data>
<data name="FoundWallsMessage" xml:space="preserve">
<value>找到了 {0} 面墙。</value>
</data>
</root>
8 changes: 4 additions & 4 deletions RevitGenTest/RevitAddin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ private void Run()
if (walls.Count == 0)
{
// 如果出现问题,只需设置属性即可
this.ErrorMessage = "项目中没有找到任何墙。";
this.ErrorMessage = Properties.Resources.NoWallsFound;
this.Result = Result.Failed;
return; // 提前返回
}

TaskDialog.Show("成功", $"找到了 {walls.Count} 面墙。");
TaskDialog.Show(Properties.Resources.SuccessTitle, string.Format(Properties.Resources.FoundWallsMessage, walls.Count));
}
}
[RevitCommand("我的第一个命令", ToolTip = "这是一个自动生成的酷炫命令!", PanelName = "核心功能", Icon = "Resources/CodeList_32px.png")]
Expand All @@ -43,12 +43,12 @@ private void Run()
if (walls.Count == 0)
{
// 如果出现问题,只需设置属性即可
this.ErrorMessage = "项目中没有找到任何墙。";
this.ErrorMessage = Properties.Resources.NoWallsFound;
this.Result = Result.Failed;
return; // 提前返回
}

TaskDialog.Show("成功", $"找到了 {walls.Count} 面墙。");
TaskDialog.Show(Properties.Resources.SuccessTitle, string.Format(Properties.Resources.FoundWallsMessage, walls.Count));
}
}
}
8 changes: 8 additions & 0 deletions RevitGenTest/RevitGenTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.en.resx">
<ManifestResourceName>RevitGenTest.Properties.Resources</ManifestResourceName>
<Culture>en</Culture>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.ja.resx">
Comment on lines +32 to +36
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In SDK-style .csproj projects, .resx files are already included as EmbeddedResource by default. Adding them again with Include can trigger MSBuild “Duplicate EmbeddedResource items” errors. Prefer using EmbeddedResource Update="Properties\Resources.en.resx" / Update="Properties\Resources.ja.resx" to attach Culture/ManifestResourceName metadata without duplicating the items (similar to how Resources.resx is handled above).

Suggested change
<EmbeddedResource Include="Properties\Resources.en.resx">
<ManifestResourceName>RevitGenTest.Properties.Resources</ManifestResourceName>
<Culture>en</Culture>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.ja.resx">
<EmbeddedResource Update="Properties\Resources.en.resx">
<ManifestResourceName>RevitGenTest.Properties.Resources</ManifestResourceName>
<Culture>en</Culture>
</EmbeddedResource>
<EmbeddedResource Update="Properties\Resources.ja.resx">

Copilot uses AI. Check for mistakes.
<ManifestResourceName>RevitGenTest.Properties.Resources</ManifestResourceName>
<Culture>ja</Culture>
</EmbeddedResource>
</ItemGroup>
</Project>
Loading