LocalizationGame.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LocalizationGame.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System.Globalization;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Input;
  15. #endregion
  16. namespace Localization
  17. {
  18. /// <summary>
  19. /// Sample showing how to localize an XNA Framework game into different languages.
  20. /// </summary>
  21. public class LocalizationGame : Microsoft.Xna.Framework.Game
  22. {
  23. #region Fields
  24. GraphicsDeviceManager graphics;
  25. SpriteBatch spriteBatch;
  26. SpriteFont font;
  27. Texture2D currentFlag;
  28. #endregion
  29. #region Initialization
  30. public LocalizationGame()
  31. {
  32. Content.RootDirectory = "Content";
  33. graphics = new GraphicsDeviceManager(this);
  34. // Tell the resource manager what language to use when loading strings.
  35. Strings.Culture = CultureInfo.CurrentCulture;
  36. }
  37. /// <summary>
  38. /// Load your graphics content.
  39. /// </summary>
  40. protected override void LoadContent()
  41. {
  42. spriteBatch = new SpriteBatch(GraphicsDevice);
  43. font = Content.Load<SpriteFont>("Font");
  44. currentFlag = LoadLocalizedAsset<Texture2D>("Flag");
  45. }
  46. /// <summary>
  47. /// Helper for loading a .xnb asset which can have multiple localized
  48. /// versions for different countries. This allows you localize data such
  49. /// as textures, models, and sound effects.
  50. ///
  51. /// This uses a simple naming convention. If you have a default asset named
  52. /// "Foo", you can provide a specialized French version by calling it
  53. /// "Foo.fr", and a Japanese version called "Foo.ja". You can specialize even
  54. /// further by country as well as language, so if you wanted different assets
  55. /// for the United States vs. United Kingdom, you would add "Foo.en-US" and
  56. /// "Foo.en-GB".
  57. ///
  58. /// This function looks first for the most specialized version of the asset,
  59. /// which includes both language and country. If that does not exist, it looks
  60. /// for a version that only specifies the language. If that still does not
  61. /// exist, it falls back to the original non-localized asset name.
  62. /// </summary>
  63. T LoadLocalizedAsset<T>(string assetName)
  64. {
  65. string[] cultureNames =
  66. {
  67. CultureInfo.CurrentCulture.Name, // eg. "en-US"
  68. CultureInfo.CurrentCulture.TwoLetterISOLanguageName // eg. "en"
  69. };
  70. // Look first for a specialized language-country version of the asset,
  71. // then if that fails, loop back around to see if we can find one that
  72. // specifies just the language without the country part.
  73. foreach (string cultureName in cultureNames)
  74. {
  75. string localizedAssetName = assetName + '.' + cultureName;
  76. try
  77. {
  78. return Content.Load<T>(localizedAssetName);
  79. }
  80. catch (ContentLoadException) { }
  81. }
  82. // If we didn't find any localized asset, fall back to the default name.
  83. return Content.Load<T>(assetName);
  84. }
  85. #endregion
  86. #region Update and Draw
  87. /// <summary>
  88. /// Allows the game to run logic.
  89. /// </summary>
  90. protected override void Update(GameTime gameTime)
  91. {
  92. HandleInput();
  93. base.Update(gameTime);
  94. }
  95. /// <summary>
  96. /// This is called when the game should draw itself.
  97. /// </summary>
  98. protected override void Draw(GameTime gameTime)
  99. {
  100. string string1 = Strings.Welcome;
  101. string string2 = string.Format(Strings.CurrentLocale,
  102. CultureInfo.CurrentCulture.EnglishName,
  103. CultureInfo.CurrentCulture);
  104. string string3 = Strings.HowToChange;
  105. GraphicsDevice.Clear(Color.CornflowerBlue);
  106. spriteBatch.Begin();
  107. spriteBatch.DrawString(font, string1, new Vector2(100, 100), Color.White);
  108. spriteBatch.DrawString(font, string2, new Vector2(100, 130), Color.White);
  109. spriteBatch.DrawString(font, string3, new Vector2(100, 160), Color.White);
  110. spriteBatch.Draw(currentFlag, new Vector2(100, 250), Color.White);
  111. spriteBatch.End();
  112. base.Draw(gameTime);
  113. }
  114. #endregion
  115. #region Handle Input
  116. /// <summary>
  117. /// Handles input for quitting the game.
  118. /// </summary>
  119. private void HandleInput()
  120. {
  121. KeyboardState currentKeyboardState = Keyboard.GetState();
  122. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  123. // Check for exit.
  124. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  125. currentGamePadState.IsButtonDown(Buttons.Back))
  126. {
  127. Exit();
  128. }
  129. }
  130. #endregion
  131. }
  132. #region Entry Point
  133. /// <summary>
  134. /// The main entry point for the application.
  135. /// </summary>
  136. static class Program
  137. {
  138. static void Main()
  139. {
  140. using (LocalizationGame game = new LocalizationGame())
  141. {
  142. game.Run();
  143. }
  144. }
  145. }
  146. #endregion
  147. }